0

I have 2 Forms,FormA and FormB,for FormA have a button,in FormB I have to test if this button in formB is cliked,I tried this code:

FormA://in which I have the button

public bool button6WasClicked = false;

private void button6_Click(object sender, EventArgs e)
{
  ............   
  button6WasClicked = true;
}

FormB://in which I have to test if the button in FormA is cliked

FormA nv;

private void button3_Click(object sender, EventArgs e)
{
    if (nv.button6WasClicked) //the error is at this line
    { 
        ..........
        button6WasClicked = false;
    }
}

I have this problem : enter image description here

the Translation is:Object reference not set to an instance of an object

thanks for Help :)

Sayse
  • 42,633
  • 14
  • 77
  • 146
Lina
  • 451
  • 1
  • 8
  • 23

1 Answers1

1

I guess below code should work :-

in constrctor you have to pass your FormA object as shown below :-

public FormA nv;

    Public FormB(FormA formA)
    {
        nv = formA;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (nv.button6WasClicked)
        { 
            ..........
            nv.button6WasClicked = false;
        }
    }
Neel
  • 11,625
  • 3
  • 43
  • 61
  • 1
    "button6" is on a different form, it looks like op has two fields with the same name, your edit still won't ever make `button6WasClicked` to be true – Sayse Jul 16 '14 at 09:55
  • I guess he has Boolean in FormA only @Sayse – Neel Jul 16 '14 at 09:59
  • And creating a second `formA` isn't going to help that – Sayse Jul 16 '14 at 10:01
  • I have made some update can it work now @Sayse? – Neel Jul 16 '14 at 10:03
  • thanks guys it works for FormB,but now if I try to add a new FormC in which I have to test if the button in Forma is cliked I got the same error – Lina Jul 16 '14 at 10:33
  • 1
    whatever you have done for FormB can be done for FormC also @Lina – Neel Jul 16 '14 at 10:36
  • yes @Neel I did the same thing for C,and I think I have a problem with the "if statement" thanks for Help to this problem,I will try to solve it :) – Lina Jul 16 '14 at 10:51