System.Nullreferenceexception error when trying to pass Value from one Form to another Winforms C#

Asked

Viewed 463 times

-2

I have in my application a customer registration form and a customer search form. To send values from one form to another I use the code below, I have used this same code in several other forms and works perfectly, but this registration is not working. I did several tests and realized it’s because I’m using a tabControl.

Error occurring: System.Nullreferenceexception Hresult=0x80004003
Message=Undefined object reference for an instance of a object.

I can’t figure out how to solve this problem, any idea?

I am sending by reference, because by constructor does not work with the two open beams.

 Form form1 = Application.OpenForms["FormCadastro"];
    Label LbTeste = (Label)form1.Controls["LbTeste"];
     LbTeste.Text = "SIM";
  • 1

    Lbteste is where? in Tabcontrol? if you are first searching, then you search for the Label!

  • I don’t even know if you don’t need to tell me which tab is Tabcontrol... But that’s the way

  • Yes, he’s in tabControl.

  • then on Form1 search form1.Controls["tbControl"] and check if the return is tbControl ai inside tbControl searches for the components and so on!

  • I don’t know if I get it, is it something like that? Form Form1 = Application.Openforms["Formcadastro"]; Tabcontrol tabControl1 = (Tabcontrol)Form1.Controls["tbControl1"]; Label Lbteste = (Label)Form1.Controls["Lbteste"]; Lbteste.Text = "SIM";

  • This will all depend on whether this is the sequence of components ... maybe it is but, that’s right there .... you need to identify which elements you need to look for until you reach the innermost.

  • Virgilio Novic, I followed the hint and it worked, I had to get Tabcontrol, then Tabpage and then yes Label. Thank you so much for the help. Hug

Show 2 more comments

2 answers

0

I was able to solve it this way:

Form form1 = Application.OpenForms["FormCadastro"]; 
TabControl tabControl1 = (TabControl)form1.Controls["tbControl1"]; 
TabPage  tabPage1 = ( TabPage )tabControl1 .Controls["tabPage1 "]; 
Label LbTeste = (Label)tabPage1 .Controls["LbTeste"]; 
LbTeste.Text = "SIM";

You need to inform Tabcontrol and Tabpage that the values will be passed.

0

Forms are classes like any other, just create properties or make the controls public to access.

The method you used works because all the controls are stored in the collection Controls form and are accessible in the same way as an array, informing the key (in this case the name) of the control. But in addition to being a messy code, try changing the name of one of the controls, not even the refactoring of the IDE will update the changes and will bring a terrible problem.

There are other ways to do what you need, as I said above, you can create public properties and access from another object. Example:

In Form1:

public partial class Form1 : Form
{

    public string PropriedadeForm1 { get; set; }

    public string LabelForm1 { get { return labelForm1.Text; } set { labelForm1.Text = value; } }

    public Form1()
    {
        InitializeComponent();
    }

    private void buttonAbrirForm2_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show(this); //Passa a instância do Form1 (this) como Owner da instância do Form2
    }

    private void buttonSetPropriedadeForm2_Click(object sender, EventArgs e)
    {
        Form2 form2 = this.OwnedForms.OfType<Form2>().FirstOrDefault(); //Seleciona a instância do Form2 que é 'Owned' por esta instância do Form1

        if (form2 != null) //Se há um Form2
        {
            form2.PropriedadeForm2 = textBoxForm1.Text; //Define a propriedade
        }
    }
}

Notice the public properties PropriedadeForm1 and LabelForm1, they will be accessible from outside the Form.

Note also that, when instilling Form2, it was passed this as a method parameter Show. This causes the instance of Form2 to be owned by this instance of Form1. Subsequently, we can access them by the properties OwnedForms of Form1, and Owner form2.

Now an example of Form2:

public partial class Form2 : Form
{
    public string PropriedadeForm2 { get; set; }


    public Form2()
    {
        InitializeComponent();
    }

    private void buttonMostraLabelForm1_Click(object sender, EventArgs e)
    {
        Form1 form = this.Owner as Form1;
        if (form != null)
            MessageBox.Show(form.LabelForm1);
    }

    private void buttonSetLabelForm1_Click(object sender, EventArgs e)
    {
        Form1 form = this.Owner as Form1; //Pega a instância do Form1 que é dona desta instância do Form2

        if (form != null)
            form.LabelForm1 = textBoxForm2.Text;
    }

    private void buttonPropriedadeForm2_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.PropriedadeForm2);
    }
}

Upshot:

inserir a descrição da imagem aqui


There is also the possibility to make the fields public, and access them from outside the Form:

inserir a descrição da imagem aqui

Thus, to access the labelForm1 is enough:

private void buttonMostraLabelForm1_Click(object sender, EventArgs e)
{
    Form1 form = this.Owner as Form1;
    if (form != null)
        MessageBox.Show(form.labelForm1.Text);
}

If you use this form in your code instead of:

TabControl tabControl1 = (TabControl)form1.Controls["tbControl1"]; 

would be:

form1.tbControl1;

or

form1.LbTeste.Text = "Teste Label";

Browser other questions tagged

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