Keeping the image in form

前端 未结 3 953
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 09:43

I made 2 forms. Form2 has button which set background image. I got this :

this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 50         


        
3条回答
  •  囚心锁ツ
    2021-01-29 10:17

    If I understand you correctly, you have a form form2 which contains buttons to change the background image on the form form1.

    I wonder why you use this.BackgroundImage in form2 to change the background image? This would change the background image on form2, not on form1.

    You should actually be passing a reference to form1 to form2 and then use form1.BackgroundImage = ....

    This is all a bit vague - I suggest you edit your question to actually add some information on how the relation between form1 and form2 really is.


    First of all, you need to pass an instance of form1 to form2. This could be done in the constructor, for example, assuming that form2 is opened in form1. For example like this:

    Code in form2

    // In form2, there's a member variable that holds the form1 reference
    private form1 m_form1Instance;
    
    // The constructor of form2 could look like this
    public form2(form1 form1Instance)
    {
        ...
    
        // Remember the form1-instance
        m_form1Instance = form1Instance;
    }
    
    // When a button is clicked in form2, the background image in form1 is changed
    private void button1_Click(object sender, EventArgs e)
    {
        m_form1Instance.BackgroundImage = ...;
    }
    

    Code in form1

    // When form2 is opened, pass "this" to the constructor. The following
    // sample opens form2 as a modal dialog
    using (form2 f2 = new form2(this))
    {
        f2.ShowDialog(this);
    }
    

    That should change the background image on form1, and not on form2.

    If you want to save, which background image is displayed across sessions (i.e. you want the same background image to appear, when the program is restarted), use Wink's answer. This saves the background image used to the settings.


    OK, so it seems you want to globally change the background image on ALL the forms in your application. You have to take another way than the one I described above.

    What I'd do is create a singleton class that describes the current background image and has events to notify subscribers when the image has been changed. Like this, for example:

    public class BackgroundImageHolder
    {
        private static BackgroundImageHolder m_instance;
        private Bitmap m_backgroundImage;
    
        public event EventHandler BackgroundImageChanged;
    
        private BackgroundImageHolder() { }
    
        public static BackgroundImageHolder Instance
        {
            get
            {
                if (m_instance == null) m_instance = new BackgroundImageHolder();
                return m_instance;
            }
        }
    
        public Bitmap BackgroundImage
        {
            get { return m_backgroundImage; }
            set {
               m_backgroundImage = value;
               OnBackgroundImageChanged();
            }
        }
    
        protected void OnBackgroundImageChanged()
        {
            if (BackgroundImageChanged != null)
                BackgroundImageChanged(this, EventArgs.Empty);
        }
    }
    

    Whenever you open a form (i.e. in the constructor) you query the BackgroundImageHolder for the current image and set it for your form:

    this.BackgroundImage = BackgroundImageHolder.Instance.BackgroundImage;
    

    Then you subscribe to the notification, so the form can change the background image whenever necessary:

    BackgroundImageHolder.Instance.BackgroundImageChanged += BackgroundImageHolder_Changed;
    

    You need to implement the event handler:

    private void BackgroundImageHolder_Changed(object sender, EventArgs e)
    {
        this.BackgroundImage = BackgroundImageHolder.Instance.BackgroundImage;
    }
    

    Then, to globally change the image, it's only necessary to tell the BackgroundImageHolder about the new image:

    BackgroundImageHolder.Instance.BackgroundImage = ...;
    

    Done.

提交回复
热议问题