1 form listening for event in another form?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have 2 forms and i would like to make the 2nd form listen for a change in
the first form. is there a way to do this? i'm already passing "this" to the
2nd forms constructor so it will update when it has focus again, but would
like it to change as form1 changes.

thanks in advance
 
oscar said:
i have 2 forms and i would like to make the 2nd form listen for a change in
the first form. is there a way to do this? i'm already passing "this" to
the
2nd forms constructor so it will update when it has focus again, but would
like it to change as form1 changes.

You can make your form raise an event when something changes on the form and
add a handler to this event in the other form.
 
any recommendations on how the form1 handler and form2 listener would look?
i've tried a few things and i think i'm just going about it the wrong way.
making the listener handler on form2 is the part that i'm sort of confused
on. how do i get it to listen for changes in form 1?
 
There are two ways to go about this whole thing.

The first way is the one I like best because it is symmetrical (you
can, if you wish have form1 listening for event A from form2 and form2
listening for event B from form1, if you ever need to do that) and
because it fits nicely into a design pattern called
MVC--model-view-controller--brought to us by our friends in the Java
community.

All you have to do is make a property or a method of your form that is
going to be listening. Use a property if the listening form already
knows which event it wants to listen to. You'll need to use a method
and Reflection if you want to specify the name of the event at run
time. I'll use a property here because it's more common, and simpler:

public class Form2 : Form
{
Form1 otherForm = null;
....
public Form1 OtherForm
{
get { return this.otherForm; }
set
{
if (this.otherForm != null) { this.otherForm.theEvent -=
new EventHandler(this.handleForm1Event); }
this.otherForm = value;
if (this.otherForm != null) { this.otherForm.theEvent +=
new EventHandler(this.handleForm1Event); }
}
}

private void handleForm1Event(object sender, System.EventArgs e)
{
....
}
}

The second way is to pass a reference to Form1 to Form2's constructor
as you mentioned. This presumes that Form2 will always be listening to
the same Form1 for the duration of its existence (until another Form2
is created using "new", at which point you can pass a different Form1
to that new instance). This is less flexible, but it will still work:

public class Form2 : Form
{
public Form2(Form1 otherForm)
{
otherForm.theEvent += new EventHandler(this.handleForm1Event);
}

private void handleForm1Event(object sender, System.EventArgs e)
{
....
}
}

As I said, I prefer the first method, because it allows you to change
which Form1 the Form2 is listening to.

In both of these cases I've been a bit sloppy: when the form closes you
really ought to remove the listener from the other form's event
handler, or the garbage collector will not clean up the memory used by
Form2 until Form1 is ready to be garbage collected, too. However,
that's a bit esoteric and nothing to worry about in a simple
application.
 
thank you very much. i ended up using the second one, since the first form
doesn't need to listen. although i'm sure i'll end up using the first one
again some time.

thank you again for your help
 
Back
Top