Control.KeyDown , C#.NET
I have a user control with several controls on it. I had the same code in all the controls to call the Winhelp or DO SOME STUFF. The code basically captured the F1 Key when the control had the focus. All worked fine, but I had up to 20 controls on a form and the same snippet of code below had to basically be repeated 20 times
1. Create an event for Control in IntitializeComp onent()
this.MyTextBox. KeydDown += new System.Windows. Fomrs.KeyEventH andler(this.MyT extBox_EventNam e);
2. Create the Event Handler for For the Above Control.KeyDown event
private void MyTextBox_KeyDo wn(object sender, System.Windows. Forms.KeyEventA rgs e)
{
if (e.KeyCode == Keys.F1)
{
DO SOME STUFF HERE;
}
}
I then made the code generic see the snippet below and used one routine for all the controls. Now the Event Handler is never called, nothing happens. What is missing or what else is needed for a simple solution?
Below is the Scenario
1.Create a generic event for all controls on the form in IntializeCompon ents()
this.KeydDown += new System.Windows. Forms.KeyEventH andler(this.MyG eneric_EventNam e_KeyDown);
2 .Create a generic event handler for all The Controls that we are interested in.
private void MyGeneric_Event Name_KeyDown(ob ject sender, System.Windows. Forms.KeyEventA rgs e)
{
foreach (Control c in this.Controls)
{
if (c is TextBox || c is ComboBox || c is CheckBox)
{
if (e.KeyCode == Keys.F1)
{
DO SOME STUFF HERE;
}
}
}
}
I have a user control with several controls on it. I had the same code in all the controls to call the Winhelp or DO SOME STUFF. The code basically captured the F1 Key when the control had the focus. All worked fine, but I had up to 20 controls on a form and the same snippet of code below had to basically be repeated 20 times
1. Create an event for Control in IntitializeComp onent()
this.MyTextBox. KeydDown += new System.Windows. Fomrs.KeyEventH andler(this.MyT extBox_EventNam e);
2. Create the Event Handler for For the Above Control.KeyDown event
private void MyTextBox_KeyDo wn(object sender, System.Windows. Forms.KeyEventA rgs e)
{
if (e.KeyCode == Keys.F1)
{
DO SOME STUFF HERE;
}
}
I then made the code generic see the snippet below and used one routine for all the controls. Now the Event Handler is never called, nothing happens. What is missing or what else is needed for a simple solution?
Below is the Scenario
1.Create a generic event for all controls on the form in IntializeCompon ents()
this.KeydDown += new System.Windows. Forms.KeyEventH andler(this.MyG eneric_EventNam e_KeyDown);
2 .Create a generic event handler for all The Controls that we are interested in.
private void MyGeneric_Event Name_KeyDown(ob ject sender, System.Windows. Forms.KeyEventA rgs e)
{
foreach (Control c in this.Controls)
{
if (c is TextBox || c is ComboBox || c is CheckBox)
{
if (e.KeyCode == Keys.F1)
{
DO SOME STUFF HERE;
}
}
}
}