2

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1583208
  • 21
  • 1
  • 2
  • A paint program doesn't use a control for every single pixel. Your program is just a paint program with fat pixels. Stop using controls, the rest is simple. – Hans Passant Aug 07 '12 at 23:31
  • One problem you are going to have with the way you are trying this is the `MouseLeave` Event will not be fired while the Mouse Button is Down. The Control that receives the MouseDown Event captures the mouse till the Button is realeased. You really need to look into Hans's suggestion. – Mark Hall Aug 08 '12 at 00:41

2 Answers2

0

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.

Adrian
  • 2,825
  • 1
  • 22
  • 32
  • good thinkin... sounds like that would work, yeah i was wrong to be using the leave event, enter makes much more sense. Part of my problem is that I dont understand how to whip up a MouseDown handler out of nowhere, or the proper syntax, and I dont know what you mean by 'bind'. You definitely seem to be on the right page, thanks for your help – user1583208 Aug 09 '12 at 22:06
0

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.

MouseLeave wiring

You can then use this code as a reference for the sort of events you want to use going forwards.

Jack
  • 871
  • 1
  • 9
  • 17