Is there a way for me to make it so that all objects on my form (no matter how many) will have a link to an even handler?
For example, if I have a function that I want to call everything the user pushes a key within any object on the form... is there a way I can do that w/o manually setting the KeyDown event on each object individually? Thank you!
AlanPosted Apr 21, 2008, 10:21 AM
Another way to approach this is to set the Form's KeyPreview property to true and then handle all key presses within the Form's KeyDown eventhandler. Please see this link:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx
Matthew CochranPosted Apr 21, 2008, 10:16 AM
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach(Control c in this.Controls)
{
c.Click += c_Click;
}
}
void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Click");
}
}