C# .NET - Call Key Press Event In C#.net - Asked By Caleb 05 on 09-Apr-09 03:18 AM

Hi Frnd's,

Can anyone say how to call a key press event of a textbox in a button click event in c#.net windows application


Tk's In Adv's

Caleb....

reply - Perry replied to Caleb 05 on 09-Apr-09 03:36 AM

Use below code to get the key press event. let me know if you need anything else.

using System; 
 
// Derive a custom EventArgs class that holds the key. 
class KeyEventArgs : EventArgs 
  public char ch; 

 
// Declare a delegate for an event.  
delegate void KeyHandler(object source, KeyEventArgs arg)
 
// Declare a key-press event class. 
class KeyEvent 
  public event KeyHandler KeyPress; 
 
  // This is called when a key is pressed. 
  public void OnKeyPress(char key) { 
    KeyEventArgs k = new KeyEventArgs()
   
    if(KeyPress != null) { 
      k.ch = key; 
      KeyPress(this, k)
    
  

 
// A class that receives key-press notifications. 
class ProcessKey 
  public void keyhandler(object source, KeyEventArgs arg) { 
    Console.WriteLine("Received keystroke: " + arg.ch)
  

 
// Another class that receives key-press notifications. 
class CountKeys 
  public int count = 0
 
  public void keyhandler(object source, KeyEventArgs arg) { 
    count++; 
  

 
// Demonstrate KeyEvent. 
public class KeyEventDemo 
  public static void Main() {  
    KeyEvent kevt = new KeyEvent()
    ProcessKey pk = new ProcessKey()
    CountKeys ck = new CountKeys()
    char ch; 
 
    kevt.KeyPress += new KeyHandler(pk.keyhandler)
    kevt.KeyPress += new KeyHandler(ck.keyhandler)
 
    Console.WriteLine("Enter some characters. " 
                      "Enter a period to stop.")
    do 
      ch = (charConsole.Read()
      kevt.OnKeyPress(ch)
    while(ch != '.')
    Console.WriteLine(ck.count + " keys pressed.")
  
}


code for in textbox key press event - Perry replied to Caleb 05 on 09-Apr-09 03:37 AM

There is also key down event. Have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

 This event occurs after the KeyDown event and can be used
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
    Handles textBox1.KeyPress
    ' Check for the flag being set in the KeyDown event.
    If nonNumberEntered = True Then
        ' Stop the character from being entered into the control since it is non-numerical.
        e.Handled = True
    End If
End Sub 'textBox1_KeyPress

Tks - Asked By Caleb 05 on 09-Apr-09 03:48 AM

But i want to use in Windows application ...

Tks in Adv

Caleb..
Tks - Asked By Caleb 05 on 09-Apr-09 03:49 AM
But i want to use in Windows application ...

Tks in Adv

Caleb..
Santhosh N replied to Caleb 05 on 09-Apr-09 05:11 AM

Just use this line in your button click event

private void button1_Click(object sender, EventArgs e)

{

    textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

}

and implement the textBox1_KeyPress event for the keypress of textbox1...

re - Web Star replied to Caleb 05 on 09-Apr-09 05:16 AM

use this

private void txtLocation_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)13)
e.Handled = true //filtering out all other keys except “Enter” key

else //if “enter” key is pressed
btnInstall_Click( ); //what arguments must be passed here?
}

private void btnInstall_click(object sender, EventArgs e)
{
}

tks frnd - Caleb 05 replied to Web Star on 09-Apr-09 05:39 AM
But its not invoking key press event when i click button

Tks in Adv

Caleb..
reply - Perry replied to Caleb 05 on 10-Apr-09 04:03 AM
Hello you cannot invoke the key press event without pressing key and just clicking the button.
PINTU replied to Perry on 29-May-10 03:24 PM
thanks for nice post