none
Mousemove Delta

    Question

  • Hey.

    I am trying to detect when the mousewheel is moved over a control. So, I open Visual Studio 2008 C# and create a new WindowsFormsApplication and drag a Panel onto the form. I then create the method for Panel_MouseMove(object sender, MouseEventArgs e). The code is as follows.

    public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
          if (ModifierKeys == Keys.Control)
          {(BREAKPOINT)
            if (e.Delta != 0)
            { 
    
            }
          }
        }
      }
    

    The first 'if' checks to see if the CTRL is held down (this allows me to only check for movement when I want to and prevent the breakpoint being thrown when it is not only when the MouseWheel is moved). However, e.Delta never changes. Is there something I am doing wrong?

    Thanks

    Matt.

    Monday, August 30, 2010 11:55 AM

Answers

  • Hi Codeeater,

    Glad to see your feedback.

     

    As the above code snippet, you could be focused on the Control firstly.

    Then, you could rotate the Wheel forward/backward.

    I update my code snippet and you could refer it.

    Event:

    button1.MouseHover += new EventHandler(button1_MouseHover);

    button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);      

     

    Button1_MouseHover:

    void button1_MouseHover(object sender, EventArgs e)

    {

         button1.Focus();

    }

     

    Button1_MouseWheel:

    void button1_MouseWheel(object sender, MouseEventArgs e)

    {

         StringBuilder sb = new StringBuilder();

         if (e.Delta == 120)

         {

             sb.Append("positive");

             textBox1.Text = sb.ToString();

         }

         if (e.Delta == -120)

         {

             sb.Append("negative");

             textBox1.Text = sb.ToString();

         }

    }

     

    You could move your mouse over the Button. Then you could rotate your wheel forward/backward.

     

     

    Best Regards

    Larcolais Gong


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, September 02, 2010 1:35 AM

All replies

  • hi,

     

    maybe attach to MouseWheel event. for this event delta works fine


    BR, Karol. mark as answer/vote as helpful if it helped you
    Monday, August 30, 2010 12:10 PM
  • Thanks. I tried that, but the event never seems to be thrown. Even when I put the cursor over the panel and rotate the mouse wheel.
    Monday, August 30, 2010 12:44 PM
  • hi,

     

    i'm guessing the control is not active. MouseWheel works for active controls. so try to select any combobox, textbox on the panel and then scroll. or attach with event to parent form


    BR, Karol. mark as answer/vote as helpful if it helped you
    Monday, August 30, 2010 12:46 PM
  • Here it works if the control is not active. It works even if the application is not active.
    Monday, August 30, 2010 1:14 PM
  • Even if I try it for something like a textbox it doesn't seem to throw it. I can't understand why.

     

    Monday, August 30, 2010 3:58 PM
  • Hi Codeeater,

    Welcome to MSDN forums.

    Some clarification with you

    Would you like to update/show/change some information when the wheel was rotated forward/backward?

    If yes, I would provide you a solution, which you could refer.

    Step 1, you could use MouseWheel Event to achieve your goal.

    Step 2, MouseEventArgs.Delta is 120. The positive value indicates that the wheel was rotated forward; a negative value indicates that the wheel was rotated backward.

    Step3, when the wheel was rotated forward/backward, textbox’s value is changed.

    See: http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.delta.aspx for more information about this event.

    I would provide some code snippet as follows. Hope this helps you!

    Event:

    button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);    

     

    Button1_MouseWheel:

    void button1_MouseWheel(object sender, MouseEventArgs e)

    {

        StringBuilder sb = new StringBuilder();

        if (e.Delta == 120) //this value indicates that the wheel was rotated forward

        {

            sb.Append("positive");

            textBox1.Text = sb.ToString();

        }

        if (e.Delta == -120) //this value indicates that the wheel was rotated backward

        {

            sb.Append("negative");

            textBox1.Text = sb.ToString();

        }

    }

     

    Best Regards

    Larcolais Gong

    Wednesday, September 01, 2010 6:02 AM
  • I have been running code very similar to that. Basically, what I am aiming for, is on a control I have made which displays data about a prohect, when you scroll the mousewheel, it wither zooms in or out (depending on the way you scroll it).

    Unfortunately that code still doesn't work for me. It compiles and runs without error but when I put the mouse over the button (button1) and scroll the mouse wheel, nothing happens at all. It doesn't even throw the event "button1.MouseWheel". This doesn't seem to make sense. The mouse is definately working as I can use it in other programs.

     

    Thanks

    Matt

    Wednesday, September 01, 2010 4:45 PM
  • Hi Codeeater,

    Glad to see your feedback.

     

    As the above code snippet, you could be focused on the Control firstly.

    Then, you could rotate the Wheel forward/backward.

    I update my code snippet and you could refer it.

    Event:

    button1.MouseHover += new EventHandler(button1_MouseHover);

    button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);      

     

    Button1_MouseHover:

    void button1_MouseHover(object sender, EventArgs e)

    {

         button1.Focus();

    }

     

    Button1_MouseWheel:

    void button1_MouseWheel(object sender, MouseEventArgs e)

    {

         StringBuilder sb = new StringBuilder();

         if (e.Delta == 120)

         {

             sb.Append("positive");

             textBox1.Text = sb.ToString();

         }

         if (e.Delta == -120)

         {

             sb.Append("negative");

             textBox1.Text = sb.ToString();

         }

    }

     

    You could move your mouse over the Button. Then you could rotate your wheel forward/backward.

     

     

    Best Regards

    Larcolais Gong


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, September 02, 2010 1:35 AM