How to disable a panels vertical scroll to the mouse wheel
-
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 :)
-
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 :)
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:
-
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 :)
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
-
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:
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;
} -
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
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...?
-
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...?
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
-
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
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
-
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
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.
-
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.
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