简体   繁体   中英

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

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM