-1

what is cause of error;

{
    private Form2 testc;
    public Form1()
    {
        InitializeComponent();
       
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Visible = true;
        button2.Visible = true;
        button3.Visible = false;
        button4.Visible = true;
    }

    private void button4_Click(object sender, EventArgs e)
    {
        button1.Visible = true;
        button2.Visible = true;
        button3.Visible = true;
        button4.Visible = true;
    }

    public void button2_Click(object sender, EventArgs e)
    {
        button1.PerformClick();
    }

    public void pictureBox2_Click(object sender, EventArgs e)
    {
        button1.Visible = false;
        button2.Visible = false;
        button3.Visible = false;
        button4.Visible = true;
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox2_Click(this, null);
        testc = new Form2() ;
        testc.Show();
    }
}

I want active on button has picture box from form 1 using form 2 that all i need , but i face off problem .

Severity Code Description Project File Line Suppression State

Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.pictureBox2_Click(object, EventArgs)' WindowsFormsApplication7

 public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
 private Form1 test;
    private void button5_Click(object sender, EventArgs e)
    {
        Form1.pictureBox2_Click(this, null);
    }
}

IF I change a code like this , in Main Project(not for testing)

 private void button1_Click(object sender, EventArgs e)  
    {
        test = new MainForm();
        test.Refill_sy_Click(this, null);
        test.Show();

give this error enter image description here

Community
  • 1
  • 1

2 Answers2

2

Form1 is a type, not a variable. Your variable is called test:

private Form1 test;

In order to use that variable, you'd need to initialize it to something. For example:

test = new Form1();

Then you can use it:

test.pictureBox2_Click(this, null);

Though it's highly unusual to directly invoke an event handler like this, and probably an indication of a problem in the design. It's more likely that you want to abstract the logic in that handler to its own method. Then both of these handlers would call that method.

It's also of course worth noting that you'll need to show your instance of Form1 in order to see whatever it's doing on its interface.

David
  • 208,112
  • 36
  • 198
  • 279
  • private MainForm test; private void button1_Click(object sender, EventArgs e) { test = new MainForm(); test.Refill_sy_Click(this, null); test.Show();} it give me object refernce not set an instance of an object.@David – Kokako Kokako Jan 26 '20 at 09:58
  • @KokakoKokako: For that you’d need to find what’s null and where it’s being referenced. The exception stack trace will help with that. See also: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – David Jan 26 '20 at 12:49
  • thank you for helping , I found the solution and write it below, thank you again . – Kokako Kokako Jan 26 '20 at 13:42
1

I found this way is the best way to handler ; button in form to another do the same funcation

var y = Application.OpenForms["MainForm"] as MainForm;
            y.Refill_sy_Click(this, null);