Within our application, there is a usercontrol, which allows to enter a number (inside a numericupdown
) followed by a combobox
which allows to select the SI-Prefix ([p, n, µ, m, -, k, M, G, T]
small to big)
Now, for easier usage, I thought it would be nice to capture the KeyPress
-Event on the numericUpDown
and set the combobox
accordingly. (If m
is pressed, select mili (m)
, if G
is pressed, select Giga (G)
)
This works flawless with the following handler / selector:
private void numericUpDown1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsLetter(e.KeyChar))
{
//Check, if its valid for si prefixes.
if (this.siSelector1.TrySelect(e.KeyChar)
{
e.Handled = true;
}
}
}
Where TrySelect
does nothing but the following:
public Boolean TrySelect(Char chr)
{
var entry = this.comboBox_siPrefix.Items.Cast<KeyValuePair<String,Double>>().Where(e => e.Key.Contains("(" + chr + ")")).FirstOrDefault();
if (!entry.Equals(new KeyValuePair<String, Double>()))
{
this.comboBox_siPrefix.SelectedItem = entry;
return true;
}
return false;
}
That's fine, but everytime the user hears a "BING" whenever a non-numeric Key is pressed on the numericupdown
.
I read about e.SuppressKeyPress
, which unfortunately isn't available with KeyPressEventArgs
- it's only available for KeyEventArgs
.
So, trying the whole thing with the KeyDown
-Event works. (No "BING") - but I wasn't able to capture capital keys, as every KeyDown
will fire the Event immediately...
private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
KeysConverter kc = new KeysConverter();
if (char.IsLetter(kc.ConvertToString(e.KeyCode)[0]))
{
//Check, if its valid for si prefixes.
if (this.siSelector1.TrySelect(kc.ConvertToString(e.KeyCode)[0]))
{
e.Handled = true;
e.SuppressKeyPress = true;
}
}
}
Any Ideas?