0

I have a program which I am using to manage a database. My database table contents are displayed in a listview on form1. I am using form2 to edit the database.
When form2 is open form1 is still visible.

However, when form2 is closed, after updating the database, I would like form1 to refresh its listview to show the newly updated information.

I have a method which refreshes the listview on form1. How can I execute this from form2?

I have set the method to public but I still cannot access it from form2.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Russell
  • 15
  • 2

2 Answers2

1

Pass an instance of form1 to form2's constructor:

Form2 form2 = new Form2(this);

In form2's constructor, store the reference to the form1 object as a member variable.

Then, from the relevant part of form2, just call the method on form1:

form1.RefreshListView();
Gigi
  • 28,163
  • 29
  • 106
  • 188
1

If I understand the problem correctly, one way would be to register an event when you create Form2:

var form2 = new Form2();
form2.Closed += (sender, args) => this.RefreshListView();

The Form.Closed event will be fired when form2 closes. One of the advantages of doing this with events is that Form2 doesn't need to know anything about Form1 reducing code coupling.

Anthony
  • 9,451
  • 9
  • 45
  • 72
  • Thanks for that, worked a charm. Here is my code: private void btnAddHomeworker_Click(object sender, EventArgs e) { AddHomeworker ah = new AddHomeworker(); ah.FormClosing += (sender1, args) => this.AllHomeworkers(); ah.Show(); } – Russell Mar 27 '14 at 15:48