Reputation: 21
The purpose of this program is to allow a user to light up a number of LEDs on a panel by choosing them on screen. I have an array of buttons built where currently, a user has to click all of the buttons individually. The array holds 1536 LEDs, thats a lot of clicks. I'd like the user to be able to hold down the mouse button and light up any LEDs they cross. I want to say something like:
buttonArray[row, column].MouseLeave += new System.Windows.Forms.MouseEventHandler(ClickButton);
And then have a routine like:
private void ClickButton(Object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
//blah blah
}
}
That will trigger any time the users mouse goes over a button, and then i will check to see if they were holding down the button. Right now I am getting this error:
Error 1 Cannot implicitly convert type 'System.Windows.Forms.MouseEventHandler' to 'System.EventHandler'.
Seems like I don't have the right match of delegate and handler, or that I'm not understanding the namespace thing... kinda new to C#. I've stumbled around on the MSDN for a while now and am finding plenty of examples that look just like mine that work.
Upvotes: 2
Views: 7474
Reputation: 951
Just a note here:
If you can't get your signatures to match for a ui event that you're trying to rig up, you can work out how this should look by using the Visual studio designer.
To do so, create a button, and hit alt+enter to view properties. Then you can view events by clicking the little lightning bolt icon, and double click on an event (e.g. MouseLeave) to get Visual Studio to fill in the event wiring for you.
You can then use this code as a reference for the sort of events you want to use going forwards.
Upvotes: 0
Reputation: 2875
The problem there is one of type. The Click event passes in System.EventArgs
. To use System.MouseEventArgs
you need to bind to the MouseClick event.
What I would do in this scenario is create a MouseDown handler that sets a private Boolean flag to say that the mouse is down to True
, and a MouseUp handler to set it back to False
. You would then use a MouseEnter handler and examine if the flag is set and, if it's set, toggle or set the state of the LED representation on your form. Then you can bind all of these to those three handlers, and it should allow you to tell whether or not your mouse is down as you enter the control space.
Edit: Going to edit this one, as the example I was putting together doesn't work like I imagined it would. Go figure.
Upvotes: 0