c# - Keeping the image in form -


i made 2 forms. form2 has button set background image. got :

this.backgroundimage = new bitmap(properties.resources._1334821694552, new size(800, 500)); //_1334821694552 name of image 

how change background in forms button click stay way until picture chosen?

if understand correctly, have form form2 contains buttons change background image on form form1.

i wonder why use this.backgroundimage in form2 change background image? change background image on form2, not on form1.

you should passing reference form1 form2 , use form1.backgroundimage = ....

this bit vague - suggest edit question add information on how relation between form1 , form2 is.


first of all, need pass instance of form1 form2. done in constructor, example, assuming form2 opened in form1. example this:

code in form2

// in form2, there's member variable holds form1 reference private form1 m_form1instance;  // constructor of form2 public form2(form1 form1instance) {     ...      // remember form1-instance     m_form1instance = form1instance; }  // when button clicked in form2, background image in form1 changed private void button1_click(object sender, eventargs e) {     m_form1instance.backgroundimage = ...; } 

code in form1

// when form2 opened, pass "this" constructor. following // sample opens form2 modal dialog using (form2 f2 = new form2(this)) {     f2.showdialog(this); } 

that should change background image on form1, , not on form2.

if want save, background image displayed across sessions (i.e. want same background image appear, when program restarted), use wink's answer. saves background image used settings.


ok, seems want globally change background image on forms in application. have take way 1 described above.

what i'd create singleton class describes current background image , has events notify subscribers when image has been changed. this, example:

public class backgroundimageholder {     private static backgroundimageholder m_instance;     private bitmap m_backgroundimage;      public event eventhandler backgroundimagechanged;      private backgroundimageholder() { }      public static backgroundimageholder instance     {                 {             if (m_instance == null) m_instance = new backgroundimageholder();             return m_instance;         }     }      public bitmap backgroundimage     {         { return m_backgroundimage; }         set {            m_backgroundimage = value;            onbackgroundimagechanged();         }     }      protected void onbackgroundimagechanged()     {         if (backgroundimagechanged != null)             backgroundimagechanged(this, eventargs.empty);     } } 

whenever open form (i.e. in constructor) query backgroundimageholder current image , set form:

this.backgroundimage = backgroundimageholder.instance.backgroundimage; 

then subscribe notification, form can change background image whenever necessary:

backgroundimageholder.instance.backgroundimagechanged += backgroundimageholder_changed; 

you need implement event handler:

private void backgroundimageholder_changed(object sender, eventargs e) {     this.backgroundimage = backgroundimageholder.instance.backgroundimage; } 

then, globally change image, it's necessary tell backgroundimageholder new image:

backgroundimageholder.instance.backgroundimage = ...; 

done.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -