Hi friends
so time in my winform base project, I get scenario where one to assign a click event handler to all child/dynamic Controls contained in a User Control. This is a good method but I found there was no proper docs for this.
so time in my winform base project, I get scenario where one to assign a click event handler to all child/dynamic Controls contained in a User Control. This is a good method but I found there was no proper docs for this.
hence to demostrate this, I mad a simple Win Form app and added a new WinForms User Control to the project. than named as AutoLabelClickControl.now put a label right on the control called ‘Control Label’. then put a panel with a label on it called ‘Panel Label’ and then placed a panel contained in the first panel and put a label on it called ‘Panel in a Panel Label’.
The trciks is that when the control named 'AutolblClickControl" has been loaded, then code should attached to a common click event handler for all label control. teh Add a _Load event handler to Control and do required coding:
// This is the Common Event Handler for all Label Clicks
private void LabelClickEventHandler(object sender, EventArgs e)
{
if (sender is Label)
{
// do proper type casting
// put ur funcationality
Label templbl = sender as Label;
MessageBox.Show(templbl.Name);
}
}
// Walk the collection recursively and find Label Controls and wire them up
private void FindLabelControlsInContainerAndAssignEvent(Control.ControlCollection aCollection)
{
foreach (Control aControl in aCollection)
{
if (aContr.Controls.Count > 0)
{
// this will be called recursively!
GetlblconrolsInContainerAndsetEvnt(aControl.Controls);
}
// wire up our Common Event Handler
if (aContr is System.Windows.Forms.Label)
{
aContr.Click += new System.EventHandler(LabelClickEventHandler);
}
}
}
private void AutolabelClickControl__Load(
object sender, EventArgs e)
{
GetlblconrolsInContainerAndsetEvnt(this.Controls);
}
And we will test it now ..
Although you may think you may not have a label contained in a label control, you can actually do this if you edit in Designer code file and that's why dont have an ‘else’ statement between these:
// does this control contain other controls?
if (aContr.Controls.Count > 0)
{
// call this function recursively!
GetlblconrolsInContainerAndsetEvnt(aContr.Controls);
}
/// to wireup Common Event Handler
if (aContr is System.Windows.Forms.Label)
{
aContr.Click += new System.EventHandler(LblClickEventHandler);
}
The trciks is that when the control named 'AutolblClickControl" has been loaded, then code should attached to a common click event handler for all label control. teh Add a _Load event handler to Control and do required coding:
// This is the Common Event Handler for all Label Clicks
private void LabelClickEventHandler(object sender, EventArgs e)
{
if (sender is Label)
{
// do proper type casting
// put ur funcationality
Label templbl = sender as Label;
MessageBox.Show(templbl.Name);
}
}
// Walk the collection recursively and find Label Controls and wire them up
private void FindLabelControlsInContainerAndAssignEvent(Control.ControlCollection aCollection)
{
foreach (Control aControl in aCollection)
{
if (aContr.Controls.Count > 0)
{
// this will be called recursively!
GetlblconrolsInContainerAndsetEvnt(aControl.Controls);
}
// wire up our Common Event Handler
if (aContr is System.Windows.Forms.Label)
{
aContr.Click += new System.EventHandler(LabelClickEventHandler);
}
}
}
private void AutolabelClickControl__Load(
object sender, EventArgs e)
{
GetlblconrolsInContainerAndsetEvnt(this.Controls);
}
And we will test it now ..
Although you may think you may not have a label contained in a label control, you can actually do this if you edit in Designer code file and that's why dont have an ‘else’ statement between these:
// does this control contain other controls?
if (aContr.Controls.Count > 0)
{
// call this function recursively!
GetlblconrolsInContainerAndsetEvnt(aContr.Controls);
}
/// to wireup Common Event Handler
if (aContr is System.Windows.Forms.Label)
{
aContr.Click += new System.EventHandler(LblClickEventHandler);
}
I hope you find it usefull..!!
No comments:
Post a Comment