-1

i want ; When form2 is closed, form1 method is called

Form 1;

 public partial class Form1 : Form
    {

           public Form1()
            {
                InitializeComponent();
    }
     public void MyMethod()
            {
                MessageBox.Show("hi");
     DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;  
    }

Form 2

 public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    private Form1 Form1Instance { get; set; }

     private void Form2_FormClosed(object sender, FormClosedEventArgs e)
            {
                    Form1Instance.MyMethod();
            }

error :

Object reference not set to an instance of an object.

Where do I make mistakes

thanks

peace
  • 9
  • 7

1 Answers1

1

When you create an instance of Form2 to display, you need to assign the Form1Instance property to an actual instance of Form1. Something like this:

Form2 form2 = new Form2();
form2.Form1Instance = this;
form2.Show()

Which should happen somewhere in your Form1 class.

PoweredByOrange
  • 6,525
  • 9
  • 39
  • 77