generating events in user defined controls

  • Thread starter Thread starter Liz - Newbie
  • Start date Start date
L

Liz - Newbie

I need to generate an event in a user defined control
when I have changed a variable, so that I can pick it up
in my main form and take it from there.

Can someone explain the SIMPLEST way of doing this.

Liz (newbie to OO)
 
Liz,

Assuming that your variable is exposed as a property, you can do the
following:

// Assume the property is named MyProperty. Define your event like this:
public event EventHandler MyPropertyChanged;

This will define your event as type EventHandler. Then, when the value
changes, you can make the following call:

// Check to see if there are any events to fire.
EventHandler pobjHandlers = MyPropertyChanged;

// If there are handlers, then fire the event.
if (pobjHandlers != null)
// Fire the event.
pobjHandlers(this, EventArgs.Empty);

Then, your form would pick up the event like any other event. Also, the
name of the event and the type are important here. The reason I suggested
this is because naming the event like this makes your class fit more into
the data binding model (the data grid and property grid will pick up on the
property change, for example).

Hope this helps.
 
Bless you - it worked! I'm not certain quite how it all
works but it worked. I can now use this as a basis for
passing events back to my form. Thanks a million

Liz


-----Original Message-----
Liz,

Assuming that your variable is exposed as a property, you can do the
following:

// Assume the property is named MyProperty. Define your event like this:
public event EventHandler MyPropertyChanged;

This will define your event as type EventHandler. Then, when the value
changes, you can make the following call:

// Check to see if there are any events to fire.
EventHandler pobjHandlers = MyPropertyChanged;

// If there are handlers, then fire the event.
if (pobjHandlers != null)
// Fire the event.
pobjHandlers(this, EventArgs.Empty);

Then, your form would pick up the event like any other event. Also, the
name of the event and the type are important here. The reason I suggested
this is because naming the event like this makes your class fit more into
the data binding model (the data grid and property grid will pick up on the
property change, for example).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I need to generate an event in a user defined control
when I have changed a variable, so that I can pick it up
in my main form and take it from there.

Can someone explain the SIMPLEST way of doing this.

Liz (newbie to OO)


.
 
Back
Top