Tech Off Thread

13 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

KeyPress Enter

Back to Forum: Tech Off
  • User profile image
    Cybermagell​an

    I have a textbox that I'm trying to have an action perform on enter. The code I currently have is. in C# of course.

    private void searchBox_KeyDown(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == (char)Keys.Enter)
    {
    //action happens here
    }
    }

    I have AcceptsReturn set as true...yet nothing happens? Ideas?

  • User profile image
    littleguru

    I tried this:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            MessageBox.Show("foo");
        }
    }

    and it worked without problems (with and without AcceptReturn enabled).

    You should also add

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            MessageBox.Show("foo");

            e.Handled = true;
        }
    }

    to avoid the "beep" sound... You can set the AcceptReturn to false then!

  • User profile image
    Cybermagell​an

    Hmm, still not working, and if I doubleclick on the textbox to enter code in there it shows the void as searchbox_textchanged. However if I also enter a seperate public void searchbox_KeyPress nothing happens still yet :-\

  • User profile image
    littleguru

    I could send you my test project... Could you send me an e-mail.

  • User profile image
    littleguru

    I have seen that you are handling the keydown event in your first post. You need that or keypress?

  • User profile image
    r_taduri

    I tried this:

    I have written one handler to set all text boxes....

            private void InputValidator(object sender, KeyPressEventArgs e)
            {
                TextBox t;
                if(sender is TextBox)
                {
                    t = (TextBox)sender;
                    if (t.Name == textBox1.Name)
                    {
                        //If it is not a letter then disallow the character
                        if(e.KeyChar == (char)13 )
                        {
                            MessageBox.Show("foo");
                            e.Handled = true;
                        }
                    }
                }
            }

    In public Form1()
    add the following code

    textBox1.KeyPress  += new KeyPressEventHandler(this.InputValidator);



  • User profile image
    Minh

    This should work


  • User profile image
    TommyCarlier

    Minh, why does your 3rd eye not have a brow? Isn't it annoying when sweat pours into it?

  • User profile image
    Cannot​Resolve​Symbol

    I have:

    private void addressTextBox_KeyDown(object sender, KeyEventArgs e) {
       if (e.KeyCode == Keys.Enter) {
          navigate(addressTextBox.Text);
       }
    }

    and it works fine.  Maybe you need KeyEventArgs instead of KeyPressEventArgs when dealing with a KeyDown event?

  • User profile image
    Minh

    TommyCarlier wrote:
    Minh, why does your 3rd eye not have a brow? Isn't it annoying when sweat pours into it?

    Everybody knows that 3rd eyes have Sweat membranes built-in

  • User profile image
    Cybermagell​an

    What wound up happening is I didn't have

    this.searchBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.searchBox_KeyPress);

    set

    Which BTW is it possible to do this with a comboBox as well?

  • User profile image
    Cybermagell​an

    Can I do the same thing with a comboBox, doesn't look like it works the same as a textbox

  • User profile image
    phreaks

    Pretty sure you can.

    Cybermagellan wrote:
    Can I do the same thing with a comboBox, doesn't look like it works the same as a textbox


    private void InitializeComponent()
    {
       this.cboBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.cboBox_KeyPress);
    }

    private void cboBox_KeyPress(object sender, KeyPressEventArgs e)
    {
          //Your Combo Key Press Handling
    }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.