Custom Control Events
-
HI. I'm just learning c# and need a little help with events. (the following scenario is just an example...hopefully to simplify) Lets say I have a custom control called Cars which has three states: drive, park and reverse Now let's say i have a racetrack control that can contain any number of Cars controls, but does not NEED to know anything about those Cars, like their state or even how many of them there are. Lets add a button to the racetrack control called Go. When the user clicks Go, I would like an event to be fired that all the Cars can hear and process accordingly. (The ones in park ignore it, those in reverse start moving backwards, those in drive move forward...) I would also like the Cars controls to be able to fire their own custom events (like HornHonked...) that can be heard by anything, like other Cars, the racetrack, or other controls (pedestrians, pigeons...). I don't know how events work in C# .NET, so I'm not sure where to start. I came across Instrumentation, but don't know if this is appropriate for such a task. I don't know if I can use existing events somehow, or inherit from events or just create my own. I don't know how you tell parents and children what to listen for and how to make sure they hear it. I thought there might be a better solution than: User hits the Go button. In Go_OnClick(), the racetrack iterates through a collection of Cars controls (updated as Cars are added to, removed from the racetrack) and calls a Cars.Go() method. Like I said, I'm just learning C# from scratch, so I'm not aware of all the features. Sorry about the dumb Cars stuff, it just seemed like an easy example. Thnaks, Tym!
-
HI. I'm just learning c# and need a little help with events. (the following scenario is just an example...hopefully to simplify) Lets say I have a custom control called Cars which has three states: drive, park and reverse Now let's say i have a racetrack control that can contain any number of Cars controls, but does not NEED to know anything about those Cars, like their state or even how many of them there are. Lets add a button to the racetrack control called Go. When the user clicks Go, I would like an event to be fired that all the Cars can hear and process accordingly. (The ones in park ignore it, those in reverse start moving backwards, those in drive move forward...) I would also like the Cars controls to be able to fire their own custom events (like HornHonked...) that can be heard by anything, like other Cars, the racetrack, or other controls (pedestrians, pigeons...). I don't know how events work in C# .NET, so I'm not sure where to start. I came across Instrumentation, but don't know if this is appropriate for such a task. I don't know if I can use existing events somehow, or inherit from events or just create my own. I don't know how you tell parents and children what to listen for and how to make sure they hear it. I thought there might be a better solution than: User hits the Go button. In Go_OnClick(), the racetrack iterates through a collection of Cars controls (updated as Cars are added to, removed from the racetrack) and calls a Cars.Go() method. Like I said, I'm just learning C# from scratch, so I'm not aware of all the features. Sorry about the dumb Cars stuff, it just seemed like an easy example. Thnaks, Tym!
so typical, as soon as i posted, i made some headway in my search. I think I have some options: - have my custom child controls subscribe to the OnClick event of the Go button on the parent Racetrack control. So, when the onclick event is fired, all Cars controls who subscribe will receive the mesasge. This, however, ties the parent and child controls together, ie, I couldn't plop the Cars controls into, say, a driveway control, without, at a minimum, having the driveway control send an identical OnClick event... - have the parent control publish a custom event that the child controls can subscribe to, and fire this event when the go button is clicked, or as necessary. this has the same problem as above, a new container/parent control must publish the same event - this would be wonderful, but not sure if it's legal. in the namespace of the Cars control, but not in the Cars class itself, publish a custom event:
public event EventHandler GoEvent;
then, in the parent control, fire the event when necessary, as in the OnClick event of the Go button:if( GoEvent != null ) //if we have subscribers GoEvent( this, new EventArgs() ) //fire the event
and, in the Cars control Class, subscribe to the event:CarsNamespace.GoEvent += new EventHandler(OnGoEvent);
Then when a Cars control receives a GoEvent, it will call OnGoEvent(). So, that is what I'd like, but it seems like just throwing it in the namespace like that wouldn't be enough, or is it? I feel like I am close, but just barely missing something. Like, would I need to declare the GoEvent in the parent?? ok, sorry if you read this far and want the last 3 minutes of your life back... but I just found a little more out and it seems that the namespace thing is a bust, but the second method looks like the way it really works. And my "fix" for the problem of tying the parent to the child would apparently be to create a kind of CarsManager class that handles adding and removing cars, and publishes the events and the event would be triggered by a CarsManager method call. Sorry to make you suffer through my learning process. but, if i'm still missing something or if i made some bad choices, assumptions, I'll always listen to anything... Thanks, tym! PS thanks to moredip who posted this stuff here.