I will tell my requirement. I need to have a keydown
event for each control in the Windows Forms form. It\'s better to do so rather than manually doing it for all c
This is the same as Magnus' correct answer but a little more fleshed out. Note that this adds the handler to every control, including labels and container controls. Those controls do not appear to raise the event, but you may want to add logic to only add the handler to controls that accept user input.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RegisterKeyDownHandlers(this);
}
private void RegisterKeyDownHandlers(Control control)
{
foreach (Control ctl in control.Controls)
{
ctl.KeyDown += KeyDownFired;
RegisterKeyDownHandlers(ctl);
}
}
private void KeyDownFired(object sender, EventArgs e)
{
MessageBox.Show("KeyDown fired for " + sender);
}
}