473,232 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 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 1229
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ash | last post by:
I want search phrase in html file and mark (like Google when I click on "cached") it (phrase). Does somebody know any class, that can help me? Maybe somebody know how could I make this? thanks
0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
6
by: Rob Meade | last post by:
Lo all, I was just running through some code I was writing for a site and when it came to the 'exact phrase' search type I wasn't sure whether that should run through and ignore the words in the...
2
by: Robert W. | last post by:
In my current work I noticed that I have several circumstances where I need to create little if/else constructs to handle the phrasing of a message. This typically involves a ternary situation like...
2
by: Ross Clement (Email address invalid - do not use) | last post by:
Hi. I would like to make a typedef for a structure which can be joined up into a linked list. I include my code below. My code works, but I have a vague suspicion that I could have declared my...
4
by: =?Utf-8?B?d3VtcGx5?= | last post by:
I read this recently: "The indexing service in XP indexes your files presumably to shorten the time needed to search your hard drive if you are looking for a specific file or part of a phrase...
2
by: cptuser | last post by:
Hi, I'm a novice and I have the following code, but I can't get it to work. All i want to be able to do is a simple form validation for a single field, so that if the user enters a particular...
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
1
by: chromis | last post by:
Hi, I've been trying to create a carousel class which takes an array of phrases and then creates a textfield for each one positioning it vertically based on the order it was added. The next stage...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.