Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 4th, 2004, 06:06 AM
Registered User
Join Date: Mar 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hi,HuhOic.(How to use keydown event)

Hi,HuhOic! Thanks for your help!
this is the code of a windows program .
private void button1_Click(object Sender,System.EventArgs e)
{ button1.Text="Pressed";
private void Form1_KeyDown(object Sender,System.KeyEventArgs e)
{label1.Text=e.KeyData.Tostring( );
In this form,I add one button and one label. But the keydown
event dosen't work.If I deleted the button and it's event,
the keydown event will work.
What's wrong whith it?

Old September 20th, 2004, 07:17 PM
Authorized User
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to HuhOiC

Hey what's up.

Oh no that's wrong. You can't do it that way.

When the Form's keydown event get's executed is each time there is a key pressed down.

When the key/character is pressed down on the keyboard the event is fired calling the form's event handler which sets the label1.text property to whatever the character is pressed down to.

If you would like it to accumulate all the previous state then you would need to do label1.tex += e.KeyData.ToString();

And if for this to work you would need the form to have focus when the keydown event occurs.

Here is what you are looking for:

To handle keyboard events only at the form level and not allow other controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true. Certain keys, such as the TAB, RETURN, ESCAPE, and arrow keys are handled by controls automatically. In order to have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true.

Huh O i C

Huh O i C

Program or Be Programmed!!!

Similar Threads
Thread Thread Starter Forum Replies Last Post
DatagridView Keydown Event senpark15 C# 0 June 28th, 2008 12:34 AM
capture return key in keydown event kawak_zx7 .NET Framework 1.x 1 April 23rd, 2007 11:21 AM
keydown/press r.parolari BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 March 17th, 2006 09:34 AM
How to use KeyDown event in Form zhangxujun1981 C# 5 July 6th, 2004 02:07 AM
About Button event and Keydown event zhangxujun1981 XSLT 1 March 6th, 2004 04:59 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.