471,348 Members | 1,186 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

neater way to phrase this

hey all,
i'm trying to capture only numeric keys and was wondering if there was a
neater or compact way of writing the following snippet?

thanks,
rodchar

private void LayOutMain_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key.ToString())
{
case "D0":
case "NumPad0":
TxbResults.Text += "0";
break;
case "D1":
case "NumPad1":
TxbResults.Text += "1";
break;
case "D2":
case "NumPad2":
TxbResults.Text += "2";
break;
case "D3":
case "NumPad3":
TxbResults.Text += "3";
break;
case "D4":
case "NumPad4":
TxbResults.Text += "4";
break;
case "D5":
case "NumPad5":
TxbResults.Text += "5";
break;
case "D6":
case "NumPad6":
TxbResults.Text += "6";
break;
case "D7":
case "NumPad7":
TxbResults.Text += "7";
break;
case "D8":
case "NumPad8":
TxbResults.Text += "8";
break;
case "D9":
case "NumPad9":
TxbResults.Text += "9";
break;
default:
break;
}
BtnEnter.Focus();
}

Aug 19 '08 #1
5 1193
Well, I haven't worked with .NET key routines (I mostly write Websites).

But why not use the KeyPress event instead? Then test if the key is a digit,
and if so convert it to an integer.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:9E**********************************@microsof t.com...
hey all,
i'm trying to capture only numeric keys and was wondering if there was a
neater or compact way of writing the following snippet?

thanks,
rodchar

private void LayOutMain_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key.ToString())
{
case "D0":
case "NumPad0":
TxbResults.Text += "0";
break;
case "D1":
case "NumPad1":
TxbResults.Text += "1";
break;
case "D2":
case "NumPad2":
TxbResults.Text += "2";
break;
case "D3":
case "NumPad3":
TxbResults.Text += "3";
break;
case "D4":
case "NumPad4":
TxbResults.Text += "4";
break;
case "D5":
case "NumPad5":
TxbResults.Text += "5";
break;
case "D6":
case "NumPad6":
TxbResults.Text += "6";
break;
case "D7":
case "NumPad7":
TxbResults.Text += "7";
break;
case "D8":
case "NumPad8":
TxbResults.Text += "8";
break;
case "D9":
case "NumPad9":
TxbResults.Text += "9";
break;
default:
break;
}
BtnEnter.Focus();
}
Aug 19 '08 #2
Console.WriteLine((byte)e.KeyChar);

this gives u the byte value of the Eventargs e.keyChar

0-9 = 48-57

if you only want numbers try the above

DaveL

"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:9E**********************************@microsof t.com...
hey all,
i'm trying to capture only numeric keys and was wondering if there was a
neater or compact way of writing the following snippet?

thanks,
rodchar

private void LayOutMain_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key.ToString())
{
case "D0":
case "NumPad0":
TxbResults.Text += "0";
break;
case "D1":
case "NumPad1":
TxbResults.Text += "1";
break;
case "D2":
case "NumPad2":
TxbResults.Text += "2";
break;
case "D3":
case "NumPad3":
TxbResults.Text += "3";
break;
case "D4":
case "NumPad4":
TxbResults.Text += "4";
break;
case "D5":
case "NumPad5":
TxbResults.Text += "5";
break;
case "D6":
case "NumPad6":
TxbResults.Text += "6";
break;
case "D7":
case "NumPad7":
TxbResults.Text += "7";
break;
case "D8":
case "NumPad8":
TxbResults.Text += "8";
break;
case "D9":
case "NumPad9":
TxbResults.Text += "9";
break;
default:
break;
}
BtnEnter.Focus();
}

Aug 19 '08 #3
Look at char.IsDigit method.

Also consider using the argument propery Handled enabling you to cancel
further key processing according to your criteria.

So something along the lines of:

void OnKeyDown(KeyEventArgs e)
{
e.Handled = !char.IsDigit((char)e.KeyValue);
}

"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:9E**********************************@microsof t.com...
hey all,
i'm trying to capture only numeric keys and was wondering if there was a
neater or compact way of writing the following snippet?

thanks,
rodchar

private void LayOutMain_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key.ToString())
{
case "D0":
case "NumPad0":
TxbResults.Text += "0";
break;
case "D1":
case "NumPad1":
TxbResults.Text += "1";
break;
case "D2":
case "NumPad2":
TxbResults.Text += "2";
break;
case "D3":
case "NumPad3":
TxbResults.Text += "3";
break;
case "D4":
case "NumPad4":
TxbResults.Text += "4";
break;
case "D5":
case "NumPad5":
TxbResults.Text += "5";
break;
case "D6":
case "NumPad6":
TxbResults.Text += "6";
break;
case "D7":
case "NumPad7":
TxbResults.Text += "7";
break;
case "D8":
case "NumPad8":
TxbResults.Text += "8";
break;
case "D9":
case "NumPad9":
TxbResults.Text += "9";
break;
default:
break;
}
BtnEnter.Focus();
}

Aug 19 '08 #4
On Tue, 19 Aug 2008 07:45:01 -0700, rodchar
<ro*****@discussions.microsoft.comwrote:
hey all,
i'm trying to capture only numeric keys and was wondering if there was a
neater or compact way of writing the following snippet?
Far be it from me to dissuade you from taking complete control over the
user input :), but...

Have you considered using the MaskedTextBox control for this particular
need? You can provide a format mask that allows only numeric input.

Pete
Aug 19 '08 #5
thanks all for great feedback,
rod.

"rodchar" wrote:
hey all,
i'm trying to capture only numeric keys and was wondering if there was a
neater or compact way of writing the following snippet?

thanks,
rodchar

private void LayOutMain_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key.ToString())
{
case "D0":
case "NumPad0":
TxbResults.Text += "0";
break;
case "D1":
case "NumPad1":
TxbResults.Text += "1";
break;
case "D2":
case "NumPad2":
TxbResults.Text += "2";
break;
case "D3":
case "NumPad3":
TxbResults.Text += "3";
break;
case "D4":
case "NumPad4":
TxbResults.Text += "4";
break;
case "D5":
case "NumPad5":
TxbResults.Text += "5";
break;
case "D6":
case "NumPad6":
TxbResults.Text += "6";
break;
case "D7":
case "NumPad7":
TxbResults.Text += "7";
break;
case "D8":
case "NumPad8":
TxbResults.Text += "8";
break;
case "D9":
case "NumPad9":
TxbResults.Text += "9";
break;
default:
break;
}
BtnEnter.Focus();
}
Aug 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ash | last post: by
2 posts views Thread by Ross Clement (Email address invalid - do not use) | last post: by
4 posts views Thread by =?Utf-8?B?d3VtcGx5?= | last post: by
9 posts views Thread by C#_Help_needed | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.