How to access Form1 members from Form2?

Asked

Viewed 262 times

0

I’m looking to access members of Form1 using the Form2. For example, I want to change the color of "Panel1" that is inside "Form1" to black:

This is the way I’m doing it

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

   private void Form2_Load(object sender, EventArgs e)
   {
      Form1 form1 = new Form1();
      form1.panel1.BackColor = Color.Black;
   }
}

However, it is impossible to do this, because the control "Panel1" does not appear within the class Form1 "instantiated".

  • 1

    If "Form1" is already open, it will not work because you are instantiating a new Form1 object

  • Really, was doing wrong, instantiating will create a new object.

3 answers

2

The easiest way to do it is to Create a form1change the color of the panel.

Then create a property on form2 who receives the form1 and pass the form1 when opening the form2 So, when opening the form2, access this property and call the method.

/////
//form1
////
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void btn_abrir_form2_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.form1 = this;
        form2.Show();
    }
    public void Mudar_BackColor()
    {
        this.BackColor = Color.Aquamarine;
    }
}      

/////
//form2
////
public partial class Form2 : Form
{
    public Form1 form1 { get; set; }
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        form1.Mudar_BackColor();
    }
}
  • I tested this way, but when I debugged the code, ran the normal method line and nothing happened.

  • You created the method that changes in Form1?

  • Yes, I created the method that changes the properties of a control in Form1. But when calling it, nothing happens

  • When you open form2, you are passing Form1 to the property?

  • Yes, in Form1 I only created the method

  • edited with the full code, worked here

  • If I just put the "Form" -> public Form form1 { get; set; }, cannot find the method, if I put "Form1" it finds the method, but when it executes it gives "Undefined object reference to an instance of an object."

  • I forgot the form2.form1 = this; add this line. I also added in the sample code.

  • 1

    It worked!! only changed the -> ( public "Form" Form1 { get; set; } ) to ( public "Form1" Form1 { get; set; } )

  • 1

    I edited to be right like I said. There you can already mark as reply.

Show 5 more comments

1


The controls you add to the form are not directly accessible because they do not represent properties of the object. For this is there is a "Controls" property in the form class for you to make these changes.

        Form2 frm = new Form2();
        frm.Controls["panel1"].BackColor = Color.Blue;
        frm.Show();

Second guardian:

    public Form mudaCor { get; set; }

    private void Form2_Load(object sender, EventArgs e)
    {
        mudaCor.Controls["panel1"].BackColor = Color.Blue;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.mudaCor = this;
        frm.Show();
    }
  • It worked from Form1 to Form2, but from Form2 to Form1 it didn’t work.

  • So you want to change the property of Form1 from Form1. For that you will have to create a property to pass it inside, and then apply the same logic.

  • I tested it this way, but it gives "Undefined object reference to an instance of an object."

  • Example here: https://www.dropbox.com/sh/inpqjt93ecovtbw/ABB4hPmhCMfHQZXLoZRsa1la?dl=0

  • In this part -> mudaCor.Controls["Panel1"]. Backcolor = Color.Blue; is that it gives "Undefined object reference to an instance of an object." but when I run the Form1 method, the code works normal.

  • With the example I shared? Makes a mistake.

  • So, the example only has an empty form

Show 2 more comments

1

The shared example code.

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

    private void button1_Click(object sender, EventArgs e)
    {
        //Nova instancia.
        Form2 frm = new Form2();

        //Passar form1 para o form2
        frm.parentForm = this;
        frm.Show();
    }
}

For the FORM2

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

    /// <summary>
    /// 
    /// </summary>
    public Form parentForm { get; set; }

    private void button1_Click(object sender, EventArgs e)
    {
        parentForm.Controls["panel1"].BackColor = Color.Blue;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.