views:

367

answers:

6

Hey all,

I've been making this login form in C# and I wanted to 'submit' all the data as soon as the user either clicks on submit or presses the enter/return key.

I've been testing a bit with KeyEvents but nothing so far worked.

void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show(e.KeyChar.ToString());
}

The above code was to test if the event even worked in the first place. It works perfectly, when I press 'd' it shows me 'd' when I press '8' it shows me '8' but pressing enter doesn't do anything.

So I though this was because enter isn't really bound to a character but it did show backspace, it worked just fine so it got me confused about why it didn't register my enter key.

So the question is: How do I log the enter/return key? and why doesn't it log the key press right right now like it should?

note: I've put the event in a textbox

tbPassword.KeyPress += new KeyPressEventHandler(tbPassword_KeyPress);

So it fires when the enter button is pressed WHILE the textbox is selected (which is was the whole time of course) maybe that has something to do with the execution of the code.

+3  A: 

Do you have a button defined as the default action?

If so then that control will gobble up the Enter key.

And maybe that is your answer. You need to set the DefaultAction property to true on your submit button.

Bill
Sounds just like what I need. But how do I assign the DefaultAction to the button?
Pieter888
Thank you, I've figured it out now.I made the button my default action but it didn't work.But then I figured out it will trigger the button's "Click" event and I was working with the "MouseClick" event.I guess that's the only difference between Click and MouseClick...
Pieter888
A: 

Try the KeyDown event instead.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        MessageBox.Show("Enter");
    }
}
Anthony Pegram
Still doesn't work for me...
Pieter888
Did you subscribe to the event?textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
Anthony Pegram
Yes, I did do that, once again it works on all keys but the enter button.Could this be a mistake of my pc instead of the code?
Pieter888
A: 

Perhaps you should use the "AcceptButton" of the form to set it to the submit button. Think that is what you what really...

Oskar Kjellin
A: 

You have left out a vital bit, you must set the Handled property to true or false depending on the condition...

void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show(e.KeyChar.ToString());
    if (e.KeyCode == Keys.Enter){
      // This is handled and will be removed from Windows message pump
      e.Handled = true; 
    }
}
tommieb75
Indeed I tend to forget adding that to my event handlers from time to time, thanks but it doesn't solve my problem...
Pieter888
A: 

Try this

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
    {
        MessageBox.Show("Enter Key Pressed", "Enter Key Pressed", MessageBoxButtons.OK);
    }
}
galford13x
Also you can add submitButton.PerformClick();If you wish to trigger a button click event on your submitButton
galford13x
Sorry, doesn't work...Is there a way to check if the enter key is being somehow 'blocked' or interrupted somehow?
Pieter888
A: 

//trying to make the Enter key work when pressed to execute the calculation void calculateButton_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\r') MessageBox.Show("Enter Key Pressed", "Enter Key Pressed", MessageBoxButtons.OK); }

Hey all, tried this but it does not work for me. I can tab along until I am on the calculationButton and hit Enter but it does not execute.

It is declared like this in ....designer.cs:

this.calculateButton.KeyPress += new System.Windows.Forms.KeyPressEventHandler(calculateButton_KeyPress);

Also have set the calculateButton KeyPress Properties to calculateButton_KeyPress, and its MouseClick Properties to button1_MouseClick

NewGuru