I have a form with 30 textboxes which I want to function as if they are only 1 textbox.

Why Do this instead of substring the string from a single textbox?

I have them separated for user friendly appearance purposes. Each box is paired with a checkbox so that letters appear exactly above and center to the check box no matter what size/case the rest of the letters are.

I have the following things working

When a letter is entered in txtBox1, txtBox2 gets focus and if a character is deleted from txtBox2 focus goes to txtBox1.

    Private Sub inputTxtBox01_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles inputTxtBox01.TextChanged
        If inputTxtBox01.Text <> "" And inputTxtBox02.Enabled = True Then
            inputTxtBox02.Focus()
        ElseIf inputTxtBox01.Text = "" Then
            inputTxtBox01.Focus()
        End If
    End Sub

coded in each box to function the same way

My broken code attempt

I was trying to handle a box which is empty but the user presses backspace key to delete previous character. The code above wont work because there is no text in the current box yet so I need to detect the key and this is where I'm having trouble...

Forgive me if this is horrible because I really did not know where to begin with this. I have never tried handling keydown/keypress etc events before.

Also, I was tired of typing out all my code 30x so I was trying to make code that would apply to all 30 text boxes in one code block.

    Private Sub inputTxtBox01_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles inputTxtBox01.KeyDown, inputTxtBox02.KeyDown, inputTxtBox03.KeyDown, inputTxtBox04.KeyDown, inputTxtBox05.KeyDown, inputTxtBox06.KeyDown, _
inputTxtBox07.KeyDown, inputTxtBox08.KeyDown, inputTxtBox09.KeyDown, inputTxtBox10.KeyDown, inputTxtBox11.KeyDown, inputTxtBox12.KeyDown, _
inputTxtBox13.KeyDown, inputTxtBox14.KeyDown, inputTxtBox15.KeyDown, inputTxtBox16.KeyDown, inputTxtBox17.KeyDown, inputTxtBox18.KeyDown, _
inputTxtBox19.KeyDown, inputTxtBox20.KeyDown, inputTxtBox21.KeyDown, inputTxtBox22.KeyDown, inputTxtBox23.KeyDown, inputTxtBox24.KeyDown, _
inputTxtBox25.KeyDown, inputTxtBox26.KeyDown, inputTxtBox27.KeyDown, inputTxtBox28.KeyDown, inputTxtBox29.KeyDown, inputTxtBox30.KeyDown

        Dim o As Object

        If TypeOf sender Is Button Then
            Dim btn As Button = CType(sender, Button)
            If e.KeyCode = Keys.Back Then
                If btn.Name.Substring(11, 2) <> "01" Then
                    For Each o In Me.Controls
                        If TypeOf o Is Button And o.name.contains("inputTxtBox" + Val(btn.Name.Substring(11, 2) - 1).ToString) Then
                            o.focus()
                            o.clear()
                        End If
                    Next
                End If
            End If


        End If
    End Sub

Part of my problem is that this simply does nothing... no errors. It just does not work so, I think I may be going about this entirely wrong :(

never mind I'm an idiot trying to look for buttons when I should be looking for text boxes >.<

In case anyone was curious or wanted to know... The working code I was able to make once I got past my own stupidity :P

    Private Sub inputTxtBox01_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles inputTxtBox01.KeyDown, inputTxtBox02.KeyDown, inputTxtBox03.KeyDown, inputTxtBox04.KeyDown, inputTxtBox05.KeyDown, inputTxtBox06.KeyDown, _
inputTxtBox07.KeyDown, inputTxtBox08.KeyDown, inputTxtBox09.KeyDown, inputTxtBox10.KeyDown, inputTxtBox11.KeyDown, inputTxtBox12.KeyDown, _
inputTxtBox13.KeyDown, inputTxtBox14.KeyDown, inputTxtBox15.KeyDown, inputTxtBox16.KeyDown, inputTxtBox17.KeyDown, inputTxtBox18.KeyDown, _
inputTxtBox19.KeyDown, inputTxtBox20.KeyDown, inputTxtBox21.KeyDown, inputTxtBox22.KeyDown, inputTxtBox23.KeyDown, inputTxtBox24.KeyDown, _
inputTxtBox25.KeyDown, inputTxtBox26.KeyDown, inputTxtBox27.KeyDown, inputTxtBox28.KeyDown, inputTxtBox29.KeyDown, inputTxtBox30.KeyDown



        If TypeOf sender Is TextBox Then 'only look at textboxes
            Dim txtBox As TextBox = CType(sender, TextBox) 'determine which text box has sent a keycode




            '**************************************
            'Deleting with backspace from empty box
            '**************************************

            If e.KeyCode = Keys.Back Then 'ignore all keys except backspace

                'ignore inputTxtBox01 because that is as far left as we can go
                'ignore text boxes with content because they are already handled
                If Val(txtBox.Name.Substring(11, 2)) > 1 And txtBox.Text = "" Then

                    'we have a matching relavent txtBox with an appropriate keydown
                    For Each o As Object In Me.Controls 'determine which textbox is to the left
                        If TypeOf o Is TextBox Then
                            If Val(txtBox.Name.Substring(11, 2)) > 10 Then 'if it's bigger than 10 then -1 will be a 2 digit number (10+)
                                If o.name.contains("inputTxtBox" + Val(txtBox.Name.Substring(11, 2) - 1).ToString) Then 'handle my 2-digit naming convention
                                    o.focus() 'focus the txtBox to the left
                                    o.clear() 'clear it
                                End If

                            ElseIf Val(txtBox.Name.Substring(11, 2)) <= 10 Then 'if it is 10 or less we need to add the 0 to the string because -1 will be 9 or less
                                If o.name.contains("inputTxtBox0" + Val(txtBox.Name.Substring(11, 2) - 1).ToString) Then 'for the numbers lower than 10
                                    o.focus() 'focus the txtBox to the left
                                    o.clear() 'clear it
                                End If
                            End If
                        End If
                    Next
                End If
            End If





        End If 'end check for textbox object





    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.