简体   繁体   中英

KeyDown event foreach textbox in form and detect enter

I want to add an KeyDown Event to each Textbox in my Form and this is working actually but how can i detect the key then in my event?

This is the Code to create it for each Textbox

for (int i = 0; i < 5; i++)
{
    foreach (Control control in tabControl_1.TabPages[i].Controls)
    {
        if (control.GetType() == typeof(TextBox))
        {
            control.KeyDown += new KeyEventHandler(this.TextBoxes_Enter);
        }
    }
}

private void TextBoxes_Enter(object sender, EventArgs e)
{
   ((TextBox)sender).KeyDown = ?
}

I would know how to do it if i would have one Textbox but how do i detect the Key Enter foreach one?

This is for one:

if(e.KeyCode == Keys.Enter)
{

}

It doesn't matter for how many handlers your TextBoxes_Enter method will be assigned - it will behave exactly the same way for each one of them. If it doesn't work, it means you're talking about 2 different methods. I almost missed the fact, that your:

private void TextBoxes_Enter(object sender, EventArgs e)

should be

private void TextBoxes_Enter(object sender, KeyEventArgs e)

BTW, you can assign them just like this:

control.KeyDown += TextBoxes_Enter;

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