1

I need to update a control only whenever the mouse moves over it with the left mouse button pressed. I would normally simply check for the e.Button property, but it is unavailable in the MouseEnter event.

void MyControl_MouseEnter(object sender, EventArgs e)
    {
        // MouseEventArgs needed to check this

        // if (e.Button == MouseButtons.Left)
        // {
        //     update MyControl
        // }
    }

How would you accomplish this?

  • 2
    Could you detech when the mouse button is pressed/released in MouseDown & MouseUp, then set/clear a flag that get's checked in your code above? – jklemmack Oct 28 '11 at 22:10

5 Answers5

4

Use the static Control.MouseButtons property. For example:

    private void panel1_MouseEnter(object sender, EventArgs e) {
        if (Control.MouseButtons == MouseButtons.Left) {
            // etc...
        }
    }

This is very difficult to get going, whatever the user clicks on to get the mouse button pressed is going to capture the mouse, preventing the control's MouseEnter event from firing. It is also UI that's completely undiscoverable to the user. Do consider a better mouse trap.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Here's one (crude) way to do it. It will change the form's title text to whatever mouse button was pressed as you dragged your mouse on to button1. You can reference Control.MouseButtons to see which button is in a pressed state. Here's some more info on MSDN.

 public partial class Form1 : Form
    {
        MouseButtons _buttons;

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_buttons != System.Windows.Forms.MouseButtons.None)
            {
                this.Text = _buttons.ToString();
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            _buttons = Control.MouseButtons;
        }
    }
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
0

I just implemented something like this today, tested only in Chrome but works rather nicely. Basic concept is that you capture mousemove only between mousedown and mouseup, as follows:

var image = document.getElementById('my_image');
image.addEventListener('mousedown', function(e) {
    e.currentTarget.addEventListener('mousemove', doMyStuff);
});
image.addEventListener('mouseup', function(e) {
    e.currentTarget.removeEventListener('mousemove', doMyStuff);
});

function doMyStuff(e) {
    // do what you want, the mouse is moving while the user is holding the button down
}
RobP
  • 9,144
  • 3
  • 20
  • 33
  • you could just as easily replace the e.currentTarget.addEventListener with image.addEventListener, but I'm not sure it matters much; seems like pulling from the parameter that's handy might be more efficient than using a closure, though. – RobP Feb 14 '13 at 07:49
0

I found the answer in another question here on SO:

How can I detect a held down mouse button over a PictureBox?

You will need to use a message filter. Implement the PreFilterMessage of the IMessageFilter interface, and assign an instance using Application.AddMessageFilter.

You will have to interpret windows messages yourself... that is not kind of difficult, but it will require some work.

The implementation may look like this:

        if (m.Msg == 0x200)
        {
            int x, y;
            x = m.LParam.ToInt32() & 0xFFFF;
            y = m.LParam.ToInt32() >> 16;
            if ((m.WParam.ToInt32() & 2) != 0)
            {
                // here, the left mouse button is pressed, and you can use the coords
                // and see if the mouse is over the control you want.
            }
        }
Community
  • 1
  • 1
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
-1
   Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles 
        Button1.MouseDown
        If Control.MouseButtons = MouseButtons.Left Then
            Label1.Text = "Left"
        ElseIf Control.MouseButtons = MouseButtons.Right Then
            Label1.Text = "Right"
        ElseIf Control.MouseButtons = MouseButtons.Middle Then
            Label1.Text = "Middle"
        Else
            Label1.Text = "lelse"
        End If
    End Sub