C#递归遍历窗体所有textbox控件并设置textbox事件的方法
本文实例讲述了C#递归遍历窗体所有textbox控件并设置textbox事件的方法。分享给大家供大家参考。具体实现方法如下:
///<summary>
///
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
publicvirtualvoidSetTextBoxOnEnterStyle(objectsender,EventArgse)
{
if(senderisTextBox)
{
TextBoxtbox=senderasTextBox;
if(!tbox.ReadOnly)
{
tbox.BackColor=Color.Yellow;
}
}
}
///<summary> /// ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> publicvirtualvoidSetTextBoxOnLeaveStyle(objectsender,EventArgse) { if(senderisTextBox) { TextBoxtbox=senderasTextBox; if(!tbox.ReadOnly) { tbox.BackColor=Color.White; } } } ///<summary> /// ///</summary> ///<paramname="frm"></param> publicvirtualvoidSetFormTextBoxControlStyle(Formfrm) { IterateControlsSetTextBox(frm.Controls); }
///<summary> /// ///</summary> ///<paramname="ctls"></param> publicvirtualvoidIterateControlsSetTextBox(Control.ControlCollectionctls) { foreach(Controlcontrolinctls) { if(controlisTextBox) { (controlasTextBox).Enter+=newEventHandler(SetTextBoxOnEnterStyle); (controlasTextBox).Leave+=newEventHandler(SetTextBoxOnLeaveStyle); }
if(control.Controls.Count>0) { IterateControlsSetTextBox(control.Controls); } } }