VB
---
1. Public Sub New()
2. InitializeComponent()
3.
4. For Each control As Control In Me.Controls
5. Dim ctrlCurrent As TextBox = TryCast(control, TextBox)
6. AddHandler ctrlCurrent.GotFocus, AddressOf ctrlCurrent_GotFocus
7. AddHandler ctrlCurrent.LostFocus, AddressOf ctrlCurrent_LostFocus
8. Next
9. End Sub
10.
11. Private Sub ctrlCurrent_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
12. TryCast(sender, Control).BackColor = Color.White
13. End Sub
14.
15. Private Sub ctrlCurrent_GotFocus(ByVal sender As Object, ByVal e As EventArgs)
16.
17. TryCast(sender, Control).BackColor = Color.GreenYellow
18. End Sub
C#
---
public Form1()
{
InitializeComponent();
foreach (Control control in this.Controls)
{
TextBox ctrlCurrent = (control as TextBox);
ctrlCurrent.GotFocus += new EventHandler(ctrlCurrent_GotFocus);
ctrlCurrent.LostFocus += new EventHandler(ctrlCurrent_LostFocus);
}
}
void ctrlCurrent_LostFocus(object sender, EventArgs e)
{
(sender as Control).BackColor = Color.White;
}
private void ctrlCurrent_GotFocus(object sender, EventArgs e)
{
(sender as Control).BackColor = Color.GreenYellow;
}

Rispondi quotando