Hi i was just wondering if theres a way to give classes there own click event. For instance i have a card class, and is there a way of knowing when the user clicks on the rectangle (that displays the picture of the card) from that class?
or better yet, how do i go about knowing when the cards rectangle is clicked?
You can easily add events to your class, but the only way the class can know when something was clicked is when it is, or has a component that is, based on a window.
That's what controls are for. If you need to write a class that can detect such events, I would recommend creating a control.
There are actually two parts of your problem: first is creating the event, and second is hooking it up to something.
Creating the event is the easy part:
Then elsewhere you can set up subscribers for that event, like usual:
(Note that we're attaching an event handler manually, but this is pretty much exactly the same code the Visual Studio designer would generate if you used the GUI to attach an event handler to a control.)
And then you need to somewhere capture those click events (actually, this seems like the crux of your issue):
Notice that there are normally two ways of responding to an event:
OnEvent
methodAs other answers have pointed out, the Windows operating system takes care of delivering events to objects that are windows (that is, objects that have a window handle). Within .NET, this means objects that are subclasses of
Control
.It's easy enough to create such classes yourself, either by subclassing
Control
directly or by subclassing something likeUserControl
, which then allows you to create controls that can be containers for other controls that you create in the design view (the same way you create forms).So, your choices are the following:
Card
a control itself; then it can be positioned on forms and receive click events (and other events) directly. The drawback is that this can use a lot of resources if you have a lot of these elements active at one time, since each allocates a window handle from Windows.Card
s that is a control. This container control can be positioned on forms and receive click events (and other events) directly. The drawback is that you have to figure out manually how to delegate those click events on to the individual cards. Since the actualClick
event doesn't carry coordinates with it, you'll probably have to handle theMouseUp
event instead.Card
s into some other container (like aForm
) and attach a handler to that container'sClick
(orMouseUp
) event. In that handler, figure out if any of yourCard
s have been clicked on, and delegate the click events accordingly.Note that all of this is independent of whether or not the
Card
itself has aClick
event that users of theCard
can subscribe to. If you look at the first code sample in this answer, there's no reason why theCheckIfClicked
method onCard
has to actually fire a real event, unless you have classes likeCardWatcher
which are interested in knowing about click events onCard
s. If only theCard
itself ever cares about the click event, then you can simply create a method likeCard.HasBeenClicked(MouseEventArgs e)
, put your click-handling code there, and letCheckIfClicked
call it directly.To get "mouse was clicked here" messages from Windows, you need to have a window handle; in WinForms, anything deriving from Windows.Forms.Control will have a window handle, and it will get mouse messages. Those messages will be translated automatically into invocations of the .NET MouseDown, MouseUp, MouseClick etc. events.
So probably your card should be a control. If it's not (e.g. if you have a "Hand" control that is responsible for managing and drawing lots of cards) then that control needs to be the one that gets mouse events (e.g. MouseClick) and figures out which card actually got clicked based on the context and the coordinates of the mouse event.