'Winform PictureBox dynamic update

In my winform control I am adding picture box and assigning an image from the Resources. This works great, however I need to change the image based on business logic and this is where the issue begins. I am setting up the Image to the new one but it refuses to get updated. I have also tried to use Refresh, Update or Invalidate on a picturebox again without any success. How to change the picturebox image dynamically ? Am I using the right control?

Below is Designer autogenerated code which works fine:

 /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // pictureBox1
        // 
        this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pictureBox1.Image = global::UnumIDOutlookAddIn.Properties.Resources.MyImage;
        this.pictureBox1.Location = new System.Drawing.Point(0, 0);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(616, 86);
        this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
        this.pictureBox1.TabIndex = 1;
        this.pictureBox1.TabStop = false;
        // 
        // WinformComponent
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.pictureBox1);
        this.Name = "WinformComponent";
        this.Size = new System.Drawing.Size(616, 86);
        (this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
    }

However, later on I am trying to update the Image, and it never updates. I have tried to differnt order on the last three lines without any success as well.

 mycomponent.pictureBox1.Image = Resources.AnotherImage;
 mycomponent.pictureBox1.Invalidate();     <-- Tried many configuratons
 mycomponent.pictureBox1.Update();
 mycomponent.pictureBox1.Refresh();
          


Solution 1:[1]

** I'm completely guessing as to what your problem is. **

Most likely you're doing something like this:

Form1 mycomponent = new Form1(); // Form1 is just an example
mycomponent.pictureBox1.Image = Resources.AnotherImage;

You're making a NEW form and updating the pictureBox on that Form, a Form that is never actually displayed.

If you want to update the Form that is ALREADY VISIBLE on your screen, then you need a reference to that specific form.

Getting a reference to the visible form can be done in various ways. Understanding the CONTEXT of your program will allow us to give you the easiest method to do this.

What forms are involved? Which forms create which ones, in what order? These things matter...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idle_Mind