473,407 Members | 2,306 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,407 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 1232
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.