473,462 Members | 1,128 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to convert KeyCode to a string?

BD
Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD
Mar 6 '07 #1
4 61058
<BDwrote in message news:gq********************************@4ax.com...
Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD
Just cast KeyEventArgs.KeyValue to a char and assign it to the string, for
example:

private string s = string.Empty;
//
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
s += (char)e.KeyValue;
}

Regards,
Anne
Mar 6 '07 #2
BD
On Tue, 6 Mar 2007 11:16:14 +0100, <AvdPwrote:
><BDwrote in message news:gq********************************@4ax.com...
>Hi,

I need to convert a KeyCode from event KeyDown to a string. For
example Keys.Decimal that's 110 to ".", but it converts to char 'n'
that's the code ascii of 110. I'm a little confuse with this.

[]'s
BD

Just cast KeyEventArgs.KeyValue to a char and assign it to the string, for
example:

private string s = string.Empty;
//
private void SomeKeyDown(object sender, KeyPressEventArgs e)
{
s += (char)e.KeyValue;
}

Regards,
Anne
But I'm using the Form_KeyDown(object sender, KeyEventArgs e)
and the string representation of the decimal key "." is a "n".

s += (char)e.KeyValue; // s = "n" and I click the decimal key

Why it's so different?
How can I get the decimal character from the Regional Settings?

[]'s
BD
Mar 7 '07 #3
But I'm using the Form_KeyDown(object sender, KeyEventArgs e)
and the string representation of the decimal key "." is a "n".

s += (char)e.KeyValue; // s = "n" and I click the decimal key

Why it's so different?
How can I get the decimal character from the Regional Settings?
KeyEventArgs contains the key number of the keyboard , while
KeyPressEventArgs contains the key pressed (see
http://msdn2.microsoft.com/en-us/lib...eventargs.aspx
for detailed information). So you better use Form_KeyPress instead of
Form_KeyDown;

The decimal character (and more info) can be obtained from class
NumberFormatInfo (using System.Globalization). In the next example all keys
will be send to the control that has focus (like a textbox), but not if the
decimal seperator is pressed:

private void Form_KeyPress(object sender, KeyPressEventArg e)
{
if (NumberFormatInfo.CurrentInfo.NumberDecimalSeperat or ==
e.KeyChar.ToString())
{
// Decimal seperator pressed
e.Handled = true; // don't send the character to the focused control
}
}

Regards,
Anne
Mar 7 '07 #4
BD
>KeyEventArgs contains the key number of the keyboard , while
>KeyPressEventArgs contains the key pressed (see
http://msdn2.microsoft.com/en-us/lib...eventargs.aspx
for detailed information). So you better use Form_KeyPress instead of
Form_KeyDown;

The decimal character (and more info) can be obtained from class
NumberFormatInfo (using System.Globalization). In the next example all keys
will be send to the control that has focus (like a textbox), but not if the
decimal seperator is pressed:

private void Form_KeyPress(object sender, KeyPressEventArg e)
{
if (NumberFormatInfo.CurrentInfo.NumberDecimalSeperat or ==
e.KeyChar.ToString())
{
// Decimal seperator pressed
e.Handled = true; // don't send the character to the focused control
}
}

Regards,
Anne
Understood. Thank you very much Anne.

[]'s
BD
Mar 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
12
by: aatif | last post by:
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string. hex string: 5000005355.... ASCII:...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.