Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to link/set the ENTER to a function in winforms without relation to a Textbox control?

Tags:

c#

winforms

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).

like image 896
ethem Avatar asked Dec 07 '25 08:12

ethem


1 Answers

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);
}
like image 62
Vignesh.N Avatar answered Dec 08 '25 21:12

Vignesh.N