Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Detect Input Key, Convert value to DWORD

Posted on 2011-04-26
5
Medium Priority
?
295 Views
Last Modified: 2012-05-11
Hello.
I'm trying to enable user input to configure hotkeys for an application, and I'm struggling with trying to detect when a key is pressed and subsequently retrieve the DWORD value pertaining to said pressed key.

I'm trying something like this:
 
        private void textBox1_TextChanged(object sender, KeyEventArgs e)
        {
            string result = String.Format("%d", e.KeyValue);
            MessageBox.Show(result);
        }

Open in new window


When I type in textBox1, nothing happens at all.

Ideally, I'd like the user to be able to click a button, and then enter in their input, which will be recorded as a DWORD value.

Any help would be great, thanks.
0
Comment
Question by:JoeD77
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:brutaldev
ID: 35473106
The TextChanged event only fires when you leave focus of the text box not when you are actually typing each key. For that you should use KeyDown, KeyUp or KeyPress events.

That is not the correct syntax for String.Format. You just use place holders like string result = String.Format("{0}", e.KeyValue); The conversion of what you pass in will get converted to a string automatically (using the ToString() method of object internally)

0
 

Author Comment

by:JoeD77
ID: 35473134
Thanks for your help, brutaldev.

Changed code to:
private void textBox1_KeyPress(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.KeyCode.ToString());
        }

Open in new window


Still displays nothing. _KeyDown has the same result.
0
 
LVL 5

Assisted Solution

by:sameer_goyal
sameer_goyal earned 200 total points
ID: 35473293
Hey Joe,

I assume you have properly generated the KeyPress for the TextBox and not merely changed the name of the TextChanged event?

This link might help you further:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/b1f8a08e-a419-44f5-837b-31369dc6ac63

Let me know.
0
 
LVL 11

Accepted Solution

by:
brutaldev earned 1800 total points
ID: 35473823
Yup, looks like you just renamed the method (because the event for KeyPress does not contain the second argument KeyEventArgs, it should be KeyPressEventArgs).

Either attach the event (generate the method) from the front-end by double clicking it:
 textBox1 properties from VS designer.
Or add the following type of code in the constructor of your Form to manually create the event code (won't work in .NET 2.0):
 
public Form1()
{
  InitializeComponent();
  textBox1.KeyPress += (s, e) => MessageBox.Show(e.KeyChar.ToString());
}

Open in new window

Note: KeyChar is the property you want to look at.
0
 

Author Closing Comment

by:JoeD77
ID: 35477492
Wonderful, thanks for noticing my ignorance :)!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Screencast - Getting to Know the Pipeline

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question