0

I am having two forms (A and B). In form B there are many buttons having different background image. On clicking any of the button I want to change the background image of form A to the background image of the button which was clicked instantly as it is always open behind the form.

formA mai = new formA();

private void button1_Click(object sender, EventArgs e)
    {
        mai.BackgroundImage = button1.BackgroundImage;
    }

This is the code I am using although it changes the background image it doesn't change instantly but if I will open and close the form the background image will be changed. I don't need like that I need it to change instantly.

3 Answers3

0

Add this.Refresh()

formA mai = new formA();

private void button1_Click(object sender, EventArgs e)
{
    mai.BackgroundImage = button1.BackgroundImage;
    mai.BringToFront();
    mai.Refresh();
}
J3soon
  • 3,013
  • 2
  • 29
  • 49
0

Call mai.Invalidate() after setting the new image.

Radin Gospodinov
  • 2,313
  • 13
  • 14
0

Add a field in formB to refer to the formA instance which you want to change its BackgroundImage; and initialize it when you call formB

formB's code-behind:

public partial class formB : Form
{
    public formA owner;

    public formB()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (owner != null)
            owner.BackgroundImage = button1.BackgroundImage;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (owner != null)
            owner.BackgroundImage = button2.BackgroundImage;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (owner != null)
            owner.BackgroundImage = button3.BackgroundImage;
    }
}

formA's code-behind:

public partial class formA : Form
{
    public formA()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        formB b = new formB();
        b.owner = this;
        b.ShowDialog();
    }
}
Aly Elhaddad
  • 1,913
  • 1
  • 15
  • 31
  • could you also tell me how to save the change of image – James Philip Dec 29 '15 at 11:22
  • Take a look at this.. [Best practice to save application settings in a Windows Forms Application](http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application) – Aly Elhaddad Dec 29 '15 at 11:44