Question

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

}
Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top