473,403 Members | 2,293 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,403 software developers and data experts.

keyboard response

I have a windows from which I want to react only when the user presses one of
the digits keys (0-9). And every digit key should enter the string "str"
variable. If the user presses the enter key I want the program to make some
additional oparations and when the backspace is pressed I want the last digit
to be removed from the string str.
Unfortunately this doesn't work.
Here is the code:

private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==Convert.ToChar(Keys.Decimal) &&
e.KeyChar!=Convert.ToChar(Keys.Back))
str=str+e.KeyChar.ToString();
if(e.KeyChar==Convert.ToChar(Keys.Back))
str.Remove(str.Length-1,1);
if(e.KeyChar==Convert.ToChar(Keys.Enter))
{
if(Convert.ToDecimal(str)==y*x)
{
MessageBox.Show("Correct");
StopTime();
ChangeStatus();
}
else
MessageBox.Show("WRONG!!!");
}
}
I used the Convert class because it would say it cannot convert a char to a
Keys in the if statements
What should I do?
Nov 16 '05 #1
2 1843
Hi Michael,

In your keypress event, use

if(!Char.IsDigit(e.KeyChar) || e.KeyChar != '\b' || e.KeyChar != '\n' ||
e.KeyChar != dot)
e.Handled = true;

where dot is the decimal point obtained using

char dot =
Thread.CurrentThread.CurrentCulture.NumberFormat.N umberDecimalSeparator[0];

.. for US numbers , for at least some European numbers. Then again, if a
decimal point isn't needed, drop this.

Your code isn't working because the if statement only accepts backspace
that is also a numeric character, which never happens.

Note also that any control that accepts input will get focus, so
Form.KeyPress might not be called.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
thanks a lot!

"Morten Wennevik" wrote:
Hi Michael,

In your keypress event, use

if(!Char.IsDigit(e.KeyChar) || e.KeyChar != '\b' || e.KeyChar != '\n' ||
e.KeyChar != dot)
e.Handled = true;

where dot is the decimal point obtained using

char dot =
Thread.CurrentThread.CurrentCulture.NumberFormat.N umberDecimalSeparator[0];

.. for US numbers , for at least some European numbers. Then again, if a
decimal point isn't needed, drop this.

Your code isn't working because the if statement only accepts backspace
that is also a numeric character, which never happens.

Note also that any control that accepts input will get focus, so
Form.KeyPress might not be called.

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3

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

Similar topics

2
by: Christoph Brüser | last post by:
Hi, in my application I want to react to certain keys when a context menu is showing. So I installed a keyboard hook, but now whenever a key is pressed when the menu is showing, the application...
5
by: nx-2000 | last post by:
I've got a very large C# forms app and now that its being used in bigger environments we're getting a steady stream of "why does it do this?" problems. The most nagging of which right now is that...
3
by: cc | last post by:
hi, how do I make keyboard & mouse temporary not response to desktop
2
by: Yandos | last post by:
Hello php gurus, maybe it sounds crazy, but I'd would like to use php for some system administration, backups, etc, but sometimes user interaction is needed. Till now i have been using windows...
3
by: Paulers | last post by:
Hello, I need to emulate keyboard strokes from a console application. The console application monitors a textfile and when something is matched in a text file I need the matched string outputted...
5
by: Sagaert Johan | last post by:
Hi Can someone point me the good direction to start a project that injects keycodes, or (scancodes ) into the system as if the came from the keyboard. (like barcodescanners etc.. do) Johan
8
by: RJ45 | last post by:
Hello, I am writing a shell in C. I need to intercept Signals like CTRL+C or CTRL+D and set to ignore them. This is on Unix, using gcc. my goal is to avoid users escaping the shell with SIGINT...
5
by: patrick | last post by:
hi all, i am looking for a way to break a while True: when pressing "s" on my keyboard. how can i do this? pat
11
by: vbguy2008 | last post by:
Hi, I am coding a Windows Form Application in VB.NET 2008. I would like to clear the keyboard buffer or at least empty all outstanding key presses queued up for my application at certain points...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.