sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
=?Utf-8?B?cm9kY2hhcg==?='s Avatar

neater way to phrase this


Question posted by: =?Utf-8?B?cm9kY2hhcg==?= (Guest) on August 19th, 2008 03:55 PM
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();
}

5 Answers Posted
Jonathan Wood's Avatar
Guest - n/a Posts
#2: Re: neater way to phrase this

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" <rodchar@discussions.microsoft.comwrote in message
news:9EEFF1E8-FF50-4CE5-B7D2-4AB19417DA55@microsoft.com...
Quote:
Originally Posted by
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();
}
>


DaveL's Avatar
Guest - n/a Posts
#3: Re: neater way to phrase this

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" <rodchar@discussions.microsoft.comwrote in message
news:9EEFF1E8-FF50-4CE5-B7D2-4AB19417DA55@microsoft.com...
Quote:
Originally Posted by
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();
}
>



Clive Dixon's Avatar
Guest - n/a Posts
#4: Re: neater way to phrase this

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" <rodchar@discussions.microsoft.comwrote in message
news:9EEFF1E8-FF50-4CE5-B7D2-4AB19417DA55@microsoft.com...
Quote:
Originally Posted by
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();
}
>



Peter Duniho's Avatar
Guest - n/a Posts
#5: Re: neater way to phrase this

On Tue, 19 Aug 2008 07:45:01 -0700, rodchar
<rodchar@discussions.microsoft.comwrote:
Quote:
Originally Posted by
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
=?Utf-8?B?cm9kY2hhcg==?='s Avatar
=?Utf-8?B?cm9kY2hhcg==?= August 20th, 2008 01:15 PM
Guest - n/a Posts
#6: Re: neater way to phrase this

thanks all for great feedback,
rod.

"rodchar" wrote:
Quote:
Originally Posted by
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();
}
>

 
Not the answer you were looking for? Post your question . . .
197,041 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,041 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors