C # recursively traverse all textbox controls in the form, and then set the textbox event,
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} 63View Code