Windows Develop Bookmark and Share   
 index > Windows Forms Designer > how to use keypress events
 

how to use keypress events

hi, can please say me how to use key press events.
thank u in advance
xplorsoftware  Monday, August 03, 2009 2:09 PM
If you want tosuppresssome key , to write in a text box you can use key press event tosuppressthe keys.

For e.g if you needs to write only Digits chracter in a text box use following code on Key press event.
private void txtText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar))
                e.Handled = false;
            else 
                e.Handled = true;
        }         
e.Handled = false; mean you don't want tosuppressthe event , if it is true you are capturing the Handle and suppress the key.

vis versa if you write only Alpha Characters use the following code.
private void txtText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar))
                e.Handled = false;
            else 
                e.Handled = true;
        }


Malik M.Shahid  Tuesday, August 04, 2009 9:32 AM
Hi ,
If you areusing the Microsoft Visual Studio IDE, write click on text box and then properties , and events choose the key press event and double click on it , it will create the automatically the method and register the key press event in initialize componets , that will be created on design time.

In other case if you want to register the the key press method programmatically.
First register the key press method with following code , keyPressEventHandler it is multicast delegate so you add the key press event for registering purpose.

this.txtBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBox_KeyPress);
this.txtBox_KeyPress is a method , accepting two arguments sender name mean focued text box whose register with the above eventand KeyPressEventArgs that contains the pressed key information.

for further call the above method as described earlier.

good luck.
Malik M.Shahid  Thursday, August 06, 2009 7:40 PM
If you want tosuppresssome key , to write in a text box you can use key press event tosuppressthe keys.

For e.g if you needs to write only Digits chracter in a text box use following code on Key press event.
private void txtText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar))
                e.Handled = false;
            else 
                e.Handled = true;
        }         
e.Handled = false; mean you don't want tosuppressthe event , if it is true you are capturing the Handle and suppress the key.

vis versa if you write only Alpha Characters use the following code.
private void txtText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar))
                e.Handled = false;
            else 
                e.Handled = true;
        }


Malik M.Shahid  Tuesday, August 04, 2009 9:32 AM
hi, 
how did u set the keypress event for the text box........? for eg: the the line below denotes
the keypress event for the text box. ........ how did u set it.?
private
void txtText_KeyPres s (object sender, KeyPressEventArgs e)















xplorsoftware  Thursday, August 06, 2009 12:20 PM
Hi ,
If you areusing the Microsoft Visual Studio IDE, write click on text box and then properties , and events choose the key press event and double click on it , it will create the automatically the method and register the key press event in initialize componets , that will be created on design time.

In other case if you want to register the the key press method programmatically.
First register the key press method with following code , keyPressEventHandler it is multicast delegate so you add the key press event for registering purpose.

this.txtBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtBox_KeyPress);
this.txtBox_KeyPress is a method , accepting two arguments sender name mean focued text box whose register with the above eventand KeyPressEventArgs that contains the pressed key information.

for further call the above method as described earlier.

good luck.
Malik M.Shahid  Thursday, August 06, 2009 7:40 PM

hi,thank u so much
xplorsoftware  Saturday, August 08, 2009 4:24 AM

You can use google to search for other answers

Custom Search

More Threads

• BindingSource designer databinding
• [C#]Is it possible adding a web menu inside a windows form?
• Override "lockdown" of protected, inherited ToolStrip controls?
• How do I get the accesskey functionality to work in a Toolstrip container containing my custom component?
• Combo box value?
• Forms.Label overlapping
• Control Borders
• Potential Bug: Form Scroll Bar + Build
• ComponentsCreated & ComponentsCreating events not firing from IToolboxService Drag & Drop
• ShowUI method crashes when coupled with Form.Close