0

I have a simple button which will open up the AddStation form (this button is placed on the MainForm form):

var AddStation = new AddStation();
AddStation.Show();

It shows the form fine, however the form AddStation has a save button. When this button is pressed the AddStation form is closed, but I want to run a method which is in the MainForm's class (to update a listbox which is present on MainForm).

This isn't possible the way I do it right now, since the form AddStation doesn't have a reference to the MainForm, but how will I do this? How can I run the method MainForm.UpdateListBox from the AddStation form?

Devator
  • 3,686
  • 4
  • 33
  • 52

5 Answers5

3

You could subscribe to FormClosing on Main:

 AddStation.FormClosing += new FormClosingEventHandler(AddStation_FormClosing);

And then on Main do something like:

void AddStation_FormClosing(object sender, FormClosingEventArgs e)
{
  UpdateListBox ();
}

This will, of course, fire when the form is closing.

Edited: You could also declare your own event on AddStation and have Main subscribe to it:

On AddStation:

public event EventHandler TimeToUpdateListBox;

And whenever you think it is appropriate (maybe when the button to close AddStation has been clicked):

if (TimeToUpdateListBox != null)
  TimeToUpdateListBox(this, new EventArgs());

On Main:

void AddStation_TimeToUpdateListBox(object sender, EventArgs e)
{
  UpdateListBox ();
}
Ulises
  • 13,229
  • 5
  • 34
  • 50
1

Is there a reason you are using a var for AddStation instead of the actual class object?

Typically what I do is do something like this:

AddStation frmAddStation = new AddStation();
if (frmAddStation.ShowDialog() == DialogResult.OK) {
    //<call your update listbox function here>
}

Then in the function called from your Save button on AddStation make sure you do this:

this.DialogResult = DialogResult.OK;

The benefit of this is that if you have a cancel button on your form, if you set

this.DialogResult = DialogResult.Cancel;

Then your code doesn't execute the ListBox update.

  • In a method scope, `var` is an implicitly typed variable. Using `var` instead of `AddStation` is simply another way of declaring the type, not a "class object". The "class object" is what is declared on the right hand side of the declaration. The left side is the `type`. [var (C# Reference)](http://msdn.microsoft.com/en-us/library/bb383973.aspx) – Metro Smurf Apr 27 '12 at 14:34
  • Other than less typing, why would you use var versus explicitly declaring type for strongly typed objects? I understand why you use var when constructing LINQ statements but not sure why you would use var here, is this more of a preference thing or is Microsoft pushing devs to use this syntax? – milestogofromhere May 03 '12 at 20:40
  • 1
    The use of `var` has been discussed at length. A good discussion: [Use of var keyword in C#](http://stackoverflow.com/q/41479/9664). IMO, the general rule-of-thumb is to use `var` when the right hand side explicitly declares the type; otherwise, use the type. – Metro Smurf May 03 '12 at 22:07
0

Set DialogResult of Save button to DialogResult.OK. Then show second form this way:

using(var AddStation = new AddStation())
{
   if (AddStation.ShowDialog() == DialogResult.OK)
      // update listbox
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

You may use the overloaded Show mthod and pass the main form as the owner & then invoke the appropriate method on the Main Form - http://msdn.microsoft.com/en-us/library/szcefbbd.aspx

Angshuman Agarwal
  • 4,796
  • 7
  • 41
  • 89
0

I believe it was Dunsten who deleted his answer, but worked perfectly.

I added

public MainForm mainForm;

on top of my AddStation class, then when calling the form I used this:

var AddStation = new AddStation();
AddStation.mainForm = this;
AddStation.Show();

and now I'm able to access methods / objects from AddStation (which I was after)!

Devator
  • 3,686
  • 4
  • 33
  • 52