user3733598
user3733598

Reputation: 29

Allow second form to change the visibly of a picturebox on form 1?

Form1: I have set the modifiers to public for the image -> Home_picturebox1

Form2:

    public Form1 Firstform = new Form1(); // This is above the following code

    private void PS3IP_Confirm_Click(object sender, EventArgs e)
    {
        //PS3.ConnectTarget(PS3IP_Textbox1.Text); // Update the IP
        Firstform.Home_picturebox1.Show();
        //this.Close();
    }

It compiles fine but the image isn't shown once the event has been called?

Any ideas? ://

Upvotes: 0

Views: 64

Answers (3)

Raging Bull
Raging Bull

Reputation: 18747

Problem:

When you declare

public Form1 Firstform = new Form1();

It is actually creating a new form object.

Solution:

You need to send the object of Form1 to Form2 as a parameter and then change the visibility of Home_picturebox1.

In Form1:

private void btnGoToForm2_Click(object sender, EventArgs e)
{
    PS3IP obj= new PS3IP(this);
    obj.Show();
}

In Form2:

public Form1 Firstform;
public PS3IP(Form1 ParentForm)
{
     InitializeComponent();
     FirstForm=ParentForm;
} 
private void PS3IP_Confirm_Click(object sender, EventArgs e)
{
    //PS3.ConnectTarget(PS3IP_Textbox1.Text); // Update the IP
    Firstform.Home_picturebox1.Show();
    //this.Close();
}

Upvotes: 1

apomene
apomene

Reputation: 14389

You not reference the current instance of form1 but you create a new one, try:

   Form1 form1;
    public Form6(Form1 form1)
    {
        InitializeComponent();
        this.form1=form1;
    }

private void PS3IP_Confirm_Click(object sender, EventArgs e)
    {

        form1.Home_picturebox1.Show();
        //this.Close();
    }

Upvotes: 0

Sayse
Sayse

Reputation: 43320

new Form1 does exactly what it says, creates a new instance of Form1, you need to pass a reference to your original form, one way of doing this is in the constructor.

private Form1 FirstForm;
public Form2(Form1 myForm)
{
    FirstForm = myForm;
}

Upvotes: 2

Related Questions