Alons
Alons

Reputation: 31

system warning sound on text box keyup event C#

I Have a login form. In the KeyUp event of txtUserName textbox have this,

private void txtUserID_KeyUp(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
        {
            //Next control when Press Enter key
            SelectNextControl((Control)sender, true, true, true, true);
        }
    }

But everytime i press Enter Key,Focus goes to next contol and produce a System warning sound.

How can i avoid this or What is the wrong with this?

Upvotes: 0

Views: 267

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

When you press enter key, along side your code, the default event fires too. Add e.Handled = true, to your method:

private void txtUserID_KeyUp(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
        {
            //Next control when Press Enter key
            SelectNextControl((Control)sender, true, true, true, true);
            e.Handled = true
        }
    }

This tells that event is handled.

Upvotes: 1

Related Questions