Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to disable a panels vertical scroll to the mouse wheel

How to disable a panels vertical scroll to the mouse wheel

Scheduled Pinned Locked Moved C#
questionhelptutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    I have a panel (win forms) that has scroll bars, the problem is that when the scroll button on the mouse is scrolled it is meant to just execute x, instead it is executing x and scrolling vertically! How do I disable mouse scrolling only for the scroll panel and still capture the mouse scroll? If I have confused you: I would like the following: 1:Execute x command when mouse is scrolled What it is doing (the problem): 1:Execute x command when mouse is scrolled 2:Scrolling vertically (DO NOT WANT) Thanks :)

    M P 2 Replies Last reply
    0
    • V venomation

      I have a panel (win forms) that has scroll bars, the problem is that when the scroll button on the mouse is scrolled it is meant to just execute x, instead it is executing x and scrolling vertically! How do I disable mouse scrolling only for the scroll panel and still capture the mouse scroll? If I have confused you: I would like the following: 1:Execute x command when mouse is scrolled What it is doing (the problem): 1:Execute x command when mouse is scrolled 2:Scrolling vertically (DO NOT WANT) Thanks :)

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      This works to disable mousewheel scroll for a listbox, so I'm guessing it may work for a panel as well.

             listBox1.MouseWheel += new MouseEventHandler(listBox1\_MouseWheel);
      
          ...
      
          void listBox1\_MouseWheel(object sender, MouseEventArgs e)
          {
               // Execute x command here...
      
              // Mark event as handled so no scrolling happens
              (e as HandledMouseEventArgs).Handled = true;
          }
      

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      V 1 Reply Last reply
      0
      • V venomation

        I have a panel (win forms) that has scroll bars, the problem is that when the scroll button on the mouse is scrolled it is meant to just execute x, instead it is executing x and scrolling vertically! How do I disable mouse scrolling only for the scroll panel and still capture the mouse scroll? If I have confused you: I would like the following: 1:Execute x command when mouse is scrolled What it is doing (the problem): 1:Execute x command when mouse is scrolled 2:Scrolling vertically (DO NOT WANT) Thanks :)

        P Offline
        P Offline
        Philippe Mori
        wrote on last edited by
        #3

        To avoid confusing the user, you should not do that... The best thing to do would be to uses a modifier key (like Shift + Mouse wheel) for your action. Anyway, you would need to mark the event as handled to avoid default execution. For that, you probably have to handle to WM_MOUSEWHEEL message... or uses something else than a scrollable panel. One possibility would be have have a regular panel and scrollbar controls and handle yourself the scrollbar. Another possibility might be to have a control that handle the mouse wheel inside the panel and require the user to be over that control for you special function. It could then works similary to nested panel inside Internet Explorer... The inner most one would handle the event.

        Philippe Mori

        V 1 Reply Last reply
        0
        • M Mark Salsbery

          This works to disable mousewheel scroll for a listbox, so I'm guessing it may work for a panel as well.

                 listBox1.MouseWheel += new MouseEventHandler(listBox1\_MouseWheel);
          
              ...
          
              void listBox1\_MouseWheel(object sender, MouseEventArgs e)
              {
                   // Execute x command here...
          
                  // Mark event as handled so no scrolling happens
                  (e as HandledMouseEventArgs).Handled = true;
              }
          

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          V Offline
          V Offline
          venomation
          wrote on last edited by
          #4

          Thanks, I tried it but it did help :( Still scrolls + executes instead of just execute.

          MapRender.MouseWheel += MapRender_MouseWheel;
          ...
          void MapRender_MouseWheel(object sender, MouseEventArgs e)
          {
          if (e.Delta > 0)
          _presenter.IncreaseTileSize();
          else _presenter.DecreaseTileSize();
          (e as HandledMouseEventArgs).Handled = true;
          }

          1 Reply Last reply
          0
          • P Philippe Mori

            To avoid confusing the user, you should not do that... The best thing to do would be to uses a modifier key (like Shift + Mouse wheel) for your action. Anyway, you would need to mark the event as handled to avoid default execution. For that, you probably have to handle to WM_MOUSEWHEEL message... or uses something else than a scrollable panel. One possibility would be have have a regular panel and scrollbar controls and handle yourself the scrollbar. Another possibility might be to have a control that handle the mouse wheel inside the panel and require the user to be over that control for you special function. It could then works similary to nested panel inside Internet Explorer... The inner most one would handle the event.

            Philippe Mori

            V Offline
            V Offline
            venomation
            wrote on last edited by
            #5

            Thanks for the reply, I have already tried handling WM_MOUSEWHEEL messages but does not work...

            Philippe Mori wrote:

            The best thing to do would be to uses a modifier key (like Shift + Mouse wheel) for your action.

            This scenario seems like it would also fit the same problem im currently facing? I would still have to sent the MouseWheel event and then check... Such as:

                    MapRender.MouseWheel += MapRender\_MouseWheel;
                    MapRender.KeyDown += MapRender\_KeyDown;
                    MapRender.KeyUp += MapRender\_KeyUp;
            

            ...
            void MapRender_KeyUp(object sender, KeyEventArgs e)
            {
            if(_current == e.KeyCode)
            {
            _current = Keys.None;
            }
            }

                private Keys \_current = Keys.None;
                void MapRender\_KeyDown(object sender, KeyEventArgs e)
                {
                    \_current = e.KeyCode;
                }
            
                void MapRender\_MouseWheel(object sender, MouseEventArgs e)
                {
                    if(\_current != Keys.ControlKey) return;
                    if (e.Delta > 0)
                        \_presenter.IncreaseTileSize();
                    else \_presenter.DecreaseTileSize();
                
                }
            

            Still have to pass through the same event handler which calls the scroll bar to change...?

            P 1 Reply Last reply
            0
            • V venomation

              Thanks for the reply, I have already tried handling WM_MOUSEWHEEL messages but does not work...

              Philippe Mori wrote:

              The best thing to do would be to uses a modifier key (like Shift + Mouse wheel) for your action.

              This scenario seems like it would also fit the same problem im currently facing? I would still have to sent the MouseWheel event and then check... Such as:

                      MapRender.MouseWheel += MapRender\_MouseWheel;
                      MapRender.KeyDown += MapRender\_KeyDown;
                      MapRender.KeyUp += MapRender\_KeyUp;
              

              ...
              void MapRender_KeyUp(object sender, KeyEventArgs e)
              {
              if(_current == e.KeyCode)
              {
              _current = Keys.None;
              }
              }

                  private Keys \_current = Keys.None;
                  void MapRender\_KeyDown(object sender, KeyEventArgs e)
                  {
                      \_current = e.KeyCode;
                  }
              
                  void MapRender\_MouseWheel(object sender, MouseEventArgs e)
                  {
                      if(\_current != Keys.ControlKey) return;
                      if (e.Delta > 0)
                          \_presenter.IncreaseTileSize();
                      else \_presenter.DecreaseTileSize();
                  
                  }
              

              Still have to pass through the same event handler which calls the scroll bar to change...?

              P Offline
              P Offline
              Philippe Mori
              wrote on last edited by
              #6

              You don't really have to hook keyboard events as it it possible to check the current keyboard state at the time MouseWheel is called. But effectively, you still have to prevent the event... Maybe one possibility would be to derive a control from the panel and override OnMouseWheel method. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousewheel.aspx Otherwise, the trick to uses a non scrollable panel and manual scrollbar controls would works... Also, you have to be carefull about which control has the focus... Typically the inner control (the one inside the scrollable panel) should be focusable and handle the mouse wheel.

              Philippe Mori

              V 1 Reply Last reply
              0
              • P Philippe Mori

                You don't really have to hook keyboard events as it it possible to check the current keyboard state at the time MouseWheel is called. But effectively, you still have to prevent the event... Maybe one possibility would be to derive a control from the panel and override OnMouseWheel method. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmousewheel.aspx Otherwise, the trick to uses a non scrollable panel and manual scrollbar controls would works... Also, you have to be carefull about which control has the focus... Typically the inner control (the one inside the scrollable panel) should be focusable and handle the mouse wheel.

                Philippe Mori

                V Offline
                V Offline
                venomation
                wrote on last edited by
                #7

                Thanks for your help :-D Gave me a dirty idea... I have solved my problem in the form of (what I call) a hack :cool: The main form receives and handles the "MouseScroll" so the map render panel does not need to. Now for the hack part:

                    void MapRender\_MouseEnter(object sender, EventArgs e)
                    {
                        this.ActiveControl = null;
                    }
                

                When the map is entered instead of it becoming the focused form (thus any scroll will screw up the vertical scroll), nothing is - yet the scroll behaviour still works! Thanks for your help, I know my solution sucks but the map render is too tightly coupled for me to apply any radical changes to the control ;P

                G 1 Reply Last reply
                0
                • V venomation

                  Thanks for your help :-D Gave me a dirty idea... I have solved my problem in the form of (what I call) a hack :cool: The main form receives and handles the "MouseScroll" so the map render panel does not need to. Now for the hack part:

                      void MapRender\_MouseEnter(object sender, EventArgs e)
                      {
                          this.ActiveControl = null;
                      }
                  

                  When the map is entered instead of it becoming the focused form (thus any scroll will screw up the vertical scroll), nothing is - yet the scroll behaviour still works! Thanks for your help, I know my solution sucks but the map render is too tightly coupled for me to apply any radical changes to the control ;P

                  G Offline
                  G Offline
                  gavindon
                  wrote on last edited by
                  #8

                  I think you should keep in mind what one of the other posters said, Users expect the default behavior of a mouse wheel to handle a scroll in any application they use. Taking that away to make the worldwide default behavior do something different is annoying and can put users off especially if you do more things of that nature. Users as a rule do NOT like to relearn how things work.

                  Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning.

                  V 1 Reply Last reply
                  0
                  • G gavindon

                    I think you should keep in mind what one of the other posters said, Users expect the default behavior of a mouse wheel to handle a scroll in any application they use. Taking that away to make the worldwide default behavior do something different is annoying and can put users off especially if you do more things of that nature. Users as a rule do NOT like to relearn how things work.

                    Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning.

                    V Offline
                    V Offline
                    venomation
                    wrote on last edited by
                    #9

                    Good advice and I would generally follow such advice however I am quite familiar with accessibility, the app is just a small tool for me and a friend :) We needed a quick way to change the selection size fast, the mouse scroll felt like the best candidate for such operation. Otherwise key presses or other controls will be used for the same affect, but surely wont be as seamless? ;P

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups