3

I'm using Winforms.

I've a screen approx. 10 fields. and a Update button. But I don't want to use neither show on screen a button (btnUpdate).

I just want to show the the fields, they can change some values and by pressing the enter it should execute a function in code behind.

I googled and find some solutions like KeyPress on TextBox or whatever, but I don't want to link this to a TextBox. Then I found form.Acceptbutton = btnUpdate... but then I have to use a button on my designer.

so how can I make a situtation by not USING a Button control to do an update (in other words executing function in code-behind by pressing the Enter Key).

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
ethem
  • 2,888
  • 9
  • 42
  • 57
  • Take a look in here http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application – Agustin Meriles Mar 26 '13 at 14:21
  • Do you have an adversion to making the button not visible and using it in that sense? If not, you would have to create a button programmatically, but never add it to then controls of the form and then link to the acceptbutton property. I have never done this my self, so I could be wrong... – Justin Mar 26 '13 at 14:22
  • Well, you know that you are supposed to press Enter. Exactly how would a regular user discover this? He'll just click the Close button in despair. Create usable UI, an OK button is boilerplate. – Hans Passant Mar 26 '13 at 17:16

1 Answers1

1

Try overriding the ProcessCmdKey

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Return)
    {
        //Raise Update Event
        return true;
    }
    else if (keyData == Keys.Escape)
    {
        //Raise Cancel Event
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}
Vignesh.N
  • 2,618
  • 2
  • 25
  • 33