Newbie: Handling Events Help ?

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I have a simple application that consists of two forms.

On the main form, I have a button that when clicked triggers an event.
I have a listener on the main form that detects a button click and then
updates a value X.


I have a second form, (Form2), that i also need to listen for X being
changed in value. The question is, how can i do this.

I've looked at delegates and event driven programming, but I'm getting
confused.

How can one form listen in for values on another form being changed?

Many thanks.
 
Steve said:
I have a simple application that consists of two forms.

On the main form, I have a button that when clicked triggers an event.
I have a listener on the main form that detects a button click and then
updates a value X.


I have a second form, (Form2), that i also need to listen for X being
changed in value. The question is, how can i do this.

I've looked at delegates and event driven programming, but I'm getting
confused.

How can one form listen in for values on another form being changed?

Many thanks.

First of all, you may be wondering how to access one form from another.
There are many ways to do this, depending on how your code is
structured. If one form creates another, then you're already there.
Otherwise you could use public variables or properties, or use static
(class) variables.

As for have the second form watch for when X changes in the first form,
there's more than one way to do this too. The wrong way (IMO) would be
to make the button public, then from your second form, add to the
delegate, ie form1.button1.Click += new EventHandler(ButtonClick)

Or you could have a public function in the second form, and then when
you add the delegate for the first form, also add one to the second form, ie
button1.Click += new EventHandler(ButtonClick)
button1.Click += new EventHandler(form2.ButtonClick)

Or you could create you own delegate type and class event, turn X into a
property, and then execute the event when you detect a change in the
"set" code of the property.

I'm sure there are many other ways to do it. Just make sure to clean up
when the forms close.

John
 
Back
Top