Question : using button to navigate textboxes

vb.net 2008

What I have:
I have 72 textboxes on a form.
textbox0 to textbox71

9 across
8 down


For now I need a button to go to the "right"  --->


Problem:
This is working until I get to the "texbox2" then stops



In the Gotfocus event of each textbox I have:
Private Sub TextBox0_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox0.GotFocus
        MyName()
    End Sub
    Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
        MyName()
    End Sub

    Private Sub TextBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.GotFocus
        MyName()
    End Sub

    Private Sub TextBox3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.GotFocus
        MyName()
    End Sub


Public currentLocation As Integer

Public Function MyName()
        On Error Resume Next
        Dim myPointer As Integer
        Dim ctlCurrentControl As TextBox
        ctlCurrentControl = ActiveControl
        myPointer = Mid(ctlCurrentControl.Name, 8)

End Function

Any suggestions ?
Thanks
fordraiders

Answer : using button to navigate textboxes

Here's an example of how to "wire up" ALL 72 TextBoxes to the same event handler at run-time so you don't have to manually create 72 different handlers in the IDE.

Also included are examples of how to implement Left, Right, Up and Down buttons:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
Public Class Form1

    Private LastTextBox As TextBox = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tb As TextBox
        Dim ctls() As Control
        For i As Integer = 0 To 71
            ctls = Me.Controls.Find("TextBox" & i, True)
            If ctls.Length > 0 AndAlso TypeOf ctls(0) Is TextBox Then
                tb = CType(ctls(0), TextBox)
                AddHandler tb.GotFocus, AddressOf tb_GotFocus
            End If
        Next
    End Sub

    Private Sub tb_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tb As TextBox = CType(sender, TextBox)
        LastTextBox = tb
    End Sub

    Private Sub btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRight.Click
        If Not IsNothing(LastTextBox) Then
            Dim i As Integer
            Dim strInt As String = LastTextBox.Name.Remove(0, "TextBox".Length)
            If Integer.TryParse(strInt, i) Then
                If ((i + 1) Mod 9) <> 0 Then
                    i = i + 1
                    Dim ctls() As Control = Me.Controls.Find("TextBox" & i, True)
                    If ctls.Length > 0 AndAlso TypeOf ctls(0) Is TextBox Then
                        Dim tb As TextBox = CType(ctls(0), TextBox)
                        tb.Focus()
                    End If
                Else
                    LastTextBox.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub btnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click
        If Not IsNothing(LastTextBox) Then
            Dim i As Integer
            Dim strInt As String = LastTextBox.Name.Remove(0, "TextBox".Length)
            If Integer.TryParse(strInt, i) Then
                If (i Mod 9) <> 0 Then
                    i = i - 1
                    Dim ctls() As Control = Me.Controls.Find("TextBox" & i, True)
                    If ctls.Length > 0 AndAlso TypeOf ctls(0) Is TextBox Then
                        Dim tb As TextBox = CType(ctls(0), TextBox)
                        tb.Focus()
                    End If
                Else
                    LastTextBox.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        If Not IsNothing(LastTextBox) Then
            Dim i As Integer
            Dim strInt As String = LastTextBox.Name.Remove(0, "TextBox".Length)
            If Integer.TryParse(strInt, i) Then
                If i > 8 Then
                    i = i - 9
                    Dim ctls() As Control = Me.Controls.Find("TextBox" & i, True)
                    If ctls.Length > 0 AndAlso TypeOf ctls(0) Is TextBox Then
                        Dim tb As TextBox = CType(ctls(0), TextBox)
                        tb.Focus()
                    End If
                Else
                    LastTextBox.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
        If Not IsNothing(LastTextBox) Then
            Dim i As Integer
            Dim strInt As String = LastTextBox.Name.Remove(0, "TextBox".Length)
            If Integer.TryParse(strInt, i) Then
                If i < 63 Then
                    i = i + 9
                    Dim ctls() As Control = Me.Controls.Find("TextBox" & i, True)
                    If ctls.Length > 0 AndAlso TypeOf ctls(0) Is TextBox Then
                        Dim tb As TextBox = CType(ctls(0), TextBox)
                        tb.Focus()
                    End If
                Else
                    LastTextBox.Focus()
                End If
            End If
        End If
    End Sub

End Class
Random Solutions  
 
programming4us programming4us