Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shift+Scrollwheel not working in numeric updown

Status
Not open for further replies.

gdrenfrew

Programmer
Aug 1, 2002
227
GB
I'm trying to allow users to change the increment of a numeric updown but holding down shift (increment = 5) or ctrl (increment = 10). Default increment is 0.5

It works fine for ctrl but not for shift - even though my code is showing that I'm capturing the shift key. The numbers don't go up or down on the scrollwheel. Clicking the up-down arrows on the numeric while holding shift works fine though (increment = 5)

.
.
.
switch (e.KeyCode)
{
case Keys.ControlKey:
numPrice.Increment = 10;
break;
case Keys.ShiftKey:
numPrice.Increment = 5;
break;
}

etc. e.KeyCode equals Keys.ShiftKey when I step through, but in practise it doesn't have any effect. Is there a bug, or is this standard Windows behavious? I've written similar in VB6 and shift+scrollwheel worked fine...

Any advice greatly appreciated!
thanks
 
Try using following code, I tried this and it works fine for me. You have to use the combination of two events as below:

private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.ControlKey:
numericUpDown1.Increment = 10;
break;
case Keys.ShiftKey:
numericUpDown1.Increment = 5;
break;
default:
numericUpDown1.Increment = 1;
break;
}
}

private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.ControlKey:
case Keys.ShiftKey:
numericUpDown1.Increment = 1;
break;
}
}

Hope this helps

Ch Saj
 
Thanks for that.

I found the Control.ModifierKeys can be used to check if the user is pressing shift,alt or ctrl which works in my MouseWheel event.

However, am having difficulty recognising the 'shift'. Ctrl works fine. Weird one...
 
If anyone cares, I managed to work around this.

For some reason, Shift+Mousewheel wasn't firing the UpButton / DownButton events. So I manually wrote this in the OnMouseWheel event

(I'm storing the wheelDelta as a variable on the form so I can tell if it's changed by a certain number blah blah).

In addition, I set a variable mIsShiftPressed on the OnKeyPress event so I can tell if shift was pressed.

If anyone has any idea why Shift+MouseWheel didn't really work, I'd love to know. Framework 2.0

Anyway:

private void numPrice_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{

bool up = true;

wheelDelta += e.Delta;

//Somehow 'Shift' stops the mousewheel working correctly, so fire this
//manually if mShiftIsPressed
if (mShiftIsPressed)
{
if (wheelDelta < 0)
{
up = false;
}

if (up)
{
numPrice.UpButton();
}
else
{
numPrice.DownButton();
}
}
wheelDelta = 0;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top