"KeyPress" event for WinForms textbox is missing?
Asked Answered
S

4

7

I am trying to add an "KeyPress" event in a textbox (WinForm)

this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);

and here's inside the 'CheckKeys':

private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        // Enter is pressed - do something

    }
}

The idea here is that once a textbox is in focus and the 'Enter' button was pressed, something will happen...

However, my machine cannot find the 'KeyPress' event. Is there something wrong with my codes?

UPDATE:

I also tried putting KeyDown instead of KeyPress:

private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{

    if (e.Key == Key.Return)

        // Enter is pressed - do something
    }
}

Still not working though...

Shilling answered 3/4, 2010 at 12:58 Comment(5)
Have you tried looking for KeyDown?Sort
This should work - are you sure you are subscribing to your CheckKeys handler in the right place? What happens if you put MessageBox.Show(e.KeyChar.ToString()) in your handler - does it catch any events?Worrisome
@Chris: not working. I was trying to create an event similar to clicking a button by pressing enter.Shilling
@pduncan: Error came up: 'System.Windows.Controls.TextBox' does not contain a definition for 'KeyPress' and no extension method 'KeyPress' accepting a first argument of type 'System.Windows.Controls.TextBox' could be found (are you missing a using directive or an assembly reference?)Shilling
You've got the wrong TextBox somehow. You are using the WPF one (System.Windows.Controls), not the WinForms one. Check your using directives, or force your TextBox to be System.Windows.Forms.TextBoxWorrisome
M
11

You are mixing class libraries, don't use Windows Forms classes in a WPF project. Make it look like this:

  public partial class Window1 : Window {
    public Window1() {
      InitializeComponent();
      this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e) {
      if (e.Key == Key.Enter) {
        MessageBox.Show("Enter!");
        e.Handled = true;
      }
    }
  }
Million answered 3/4, 2010 at 14:49 Comment(0)
C
5

Have you looked at the documentation on KeyPress? It states specifically that The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events. Using one of those events instead should work.

Cyzicus answered 3/4, 2010 at 13:19 Comment(1)
@Anders: did tried using KeyDown instead. I was trying to call a Storyboard (from WPF) by pressing enter, but nothing happened.Shilling
C
0

Follow this way--->>

  1. Search controls under the Form / panel / groupbox/ parent Control
  2. then find child control who is type of Textbox
  3. NOW adding to a KeyPressEventHandler to that found Text Box control
  4. pls refer image screenshot for how to call the KeyPressEventHandler
  5. code as below:
foreach (Control c in this.Controls[0].Controls) //this must be the container of the textboxes,(form,panel,groupbox)
                  {
                      if (c.GetType() == typeof(System.Windows.Forms.TextBox))
                      {
                          c.KeyPress += new KeyPressEventHandler(textboxes_KeyPress);
                      }
                  }

search controls under the Form / panel / groupbox/ parent Control
then find child control who is type of Textbox
NOW adding to a KeyPressEventHandler to that found Text Box control

Contemplate answered 8/7 at 8:51 Comment(0)
K
-4

try following steps it will work, bcoz i have tested it.

  1. select textbox, right click on it, then click on properties.
  2. click on event, then double click on KeyPress
  3. then type the following code.

    private void textBox2_KeyPress(object sender, KeyPressEventArgs e)  
    {  
        if (e.KeyChar == (char)13)  
        {            
            //press Enter do Something Like i have messagebox below to show "wow"
            MessageBox.Show("wow"); 
        }
        else
        {
        }
    }
    
Kamal answered 29/8, 2011 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.