Devator
Devator

Reputation: 3904

Accessing method from another class

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?

Upvotes: 0

Views: 155

Answers (5)

Devator
Devator

Reputation: 3904

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)!

Upvotes: 0

milestogofromhere
milestogofromhere

Reputation: 51

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.

Upvotes: 1

Ulises
Ulises

Reputation: 13419

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 ();
}

Upvotes: 3

Angshuman Agarwal
Angshuman Agarwal

Reputation: 4856

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

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

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
}

Upvotes: 0

Related Questions