By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,910 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,910 IT Pros & Developers. It's quick & easy.

generating events in user defined controls

P: n/a
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)
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com
"Liz - Newbie" <ec******@cornwall.gov.uk> wrote in message
news:08****************************@phx.gbl...
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)

Nov 15 '05 #2

P: n/a
Liz
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 thefollowing:

// 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 valuechanges, 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, thename of the event and the type are important here. The reason I suggestedthis is because naming the event like this makes your class fit more intothe data binding model (the data grid and property grid will pick up on theproperty change, for example).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Liz - Newbie" <ec******@cornwall.gov.uk> wrote in messagenews:08****************************@phx.gbl...
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)

.

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.