首页 > 代码库 > c#递归遍历窗体所有textbox控件,然后设置textbox事件

c#递归遍历窗体所有textbox控件,然后设置textbox事件

 1 /// <summary> 2         ///  3         /// </summary> 4         /// <param name="sender"></param> 5         /// <param name="e"></param> 6         public virtual void SetTextBoxOnEnterStyle(object sender, EventArgs e) 7         { 8             if (sender is TextBox) 9             {10                 TextBox tbox = sender as TextBox;11                 if (!tbox.ReadOnly)12                 {13                     tbox.BackColor = Color.Yellow;14                 }15             }16         }17 18         /// <summary>19         /// 20         /// </summary>21         /// <param name="sender"></param>22         /// <param name="e"></param>23         public virtual void SetTextBoxOnLeaveStyle(object sender, EventArgs e)24         {25             if (sender is TextBox)26             {27                 TextBox tbox = sender as TextBox;28                 if (!tbox.ReadOnly)29                 {30                     tbox.BackColor = Color.White;31                 }32             }33         }34 /// <summary>35         /// 36         /// </summary>37         /// <param name="frm"></param>38         public virtual void SetFormTextBoxControlStyle(Form frm)39         {40             IterateControlsSetTextBox(frm.Controls);41         }42 43         /// <summary>44         /// 45         /// </summary>46         /// <param name="ctls"></param>47         public virtual void IterateControlsSetTextBox(Control.ControlCollection ctls)48         {49             foreach (Control control in ctls)50             {51                 if (control is TextBox)52                 {53                     (control as TextBox).Enter += new EventHandler(SetTextBoxOnEnterStyle);54                     (control as TextBox).Leave += new EventHandler(SetTextBoxOnLeaveStyle);55                 }56 57                 if (control.Controls.Count > 0)58                 {59                     IterateControlsSetTextBox(control.Controls);60                 }61             }62         }63   
View Code

 

c#递归遍历窗体所有textbox控件,然后设置textbox事件