"Times like this I wish I could just
use multiple text boxes and assign
them each an index value. Oh VB6 how I
miss you."
Create a list of text-boxes and just add each TextBox control to the list. You can even store the index of the text-box into its Tag property.
Public Class Form1
Private _textBoxes As New List(Of TextBox)
Public Sub New()
InitializeComponent()
_textBoxes.Add(TextBox1)
_textBoxes.Add(TextBox2)
_textBoxes.Add(TextBox3)
For Each oTextBox As TextBox In _textBoxes
AddHandler oTextBox.Enter, AddressOf TextBox_Enter
AddHandler oTextBox.Leave, AddressOf TextBox_Leave
oTextBox.Tag = _textBoxes.IndexOf(oTextBox)
Next
For Each oTextBox As TextBox In _textBoxes
Debug.WriteLine(CInt(oTextBox.Tag), "Index of " + oTextBox.Name)
Next
End Sub
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs)
Dim iIndex As Integer = _textBoxes.IndexOf(CType(sender, TextBox))
Debug.WriteLine(iIndex, "IndexOf")
Debug.WriteLine(DirectCast(sender, TextBox).Name, "Enter")
End Sub
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim iIndex As Integer = _textBoxes.IndexOf(CType(sender, TextBox))
Debug.WriteLine(iIndex, "IndexOf")
Debug.WriteLine(DirectCast(sender, TextBox).Name, "Leave")
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_textBoxes(1).BackColor = Color.Tomato
End Sub
End Class
(I miss nothing of VB. The .NET Framework and VB.NET are awesome.)