C#递归遍历窗体所有textbox控件并设置textbox事件的方法
本文实例讲述了c#递归遍历窗体所有textbox控件并设置textbox事件的方法。分享给大家供大家参考。具体实现方法如下:
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void settextboxonenterstyle(object sender, eventargs e)
{
if (sender is textbox)
{
textbox tbox = sender as textbox;
if (!tbox.readonly)
{
tbox.backcolor = color.yellow;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void settextboxonleavestyle(object sender, eventargs e)
{
if (sender is textbox)
{
textbox tbox = sender as textbox;
if (!tbox.readonly)
{
tbox.backcolor = color.white;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="frm"></param>
public virtual void setformtextboxcontrolstyle(form frm)
{
iteratecontrolssettextbox(frm.controls);
}
/// <summary>
///
/// </summary>
/// <param name="ctls"></param>
public virtual void iteratecontrolssettextbox(control.controlcollection ctls)
{
foreach (control control in ctls)
{
if (control is textbox)
{
(control as textbox).enter += new eventhandler(settextboxonenterstyle);
(control as textbox).leave += new eventhandler(settextboxonleavestyle);
}
if (control.controls.count > 0)
{
iteratecontrolssettextbox(control.controls);
}
}
}
希望本文所述对大家的c#程序设计有所帮助。