¿Permitir que el segundo formulario cambie la visibilidad de un cuadro de imagen en el formulario 1?

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

Formulario 2:

    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? ://

preguntado el 12 de junio de 14 a las 11:06

3 Respuestas

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;
}

Respondido el 12 de junio de 14 a las 11:06

I get this: 'PS3_RTM_CP.Form1' is a type but is used like a variable' - user3733598

@user3733598 - how are you calling it? - Sayse

Problema:

Cuando declaras

public Form1 Firstform = new Form1();

It is actually creating a nueva form object.

Solución:

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

En Form1:

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

En 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();
}

Respondido el 12 de junio de 14 a las 13:06

This just gives me the error: "The name 'This' does not exist in the current context? - user3733598

Gives me these: i.gyazo.com/2dee6201581b10d7b5828e7c89bf694c.png - Thanks for the help <3 - user3733598

What is the name of Form2? - Toro salvaje

PS3IP.cs <- That is my second form. - user3733598

@user3733598: Then use it instead of 'Form2' as shown in my answer. - Toro salvaje

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();
    }

Respondido el 12 de junio de 14 a las 11:06

This gives me the error: 'NullReferenceException was unhandled - Object reerence not set to an instance of an object'. - user3733598

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.