Wednesday, 21 August 2013

Stop a key from firing an event in C# using ProcessCmdKey?

Stop a key from firing an event in C# using ProcessCmdKey?

So I'm making a form, and I want the left and right keys to ONLY
correspond to a numericUpDown box that I have on the form. So the code I
wrote is the following :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Right)
{
numericUpDown1.Value = Convert.ToDecimal(numericUpDown1.Value
+ 1);
}
if (keyData == Keys.Left)
{
try
{
numericUpDown1.Value =
Convert.ToDecimal(numericUpDown1.Value - 1);
}
catch { }
}
return base.ProcessCmdKey(ref msg, keyData);
}
However it seems to still do the default action of moving between
different objects on the form if that's what the selected view is
currently. How do I stop the default action?

No comments:

Post a Comment