Link to home
Start Free TrialLog in
Avatar of a1785
a1785

asked on

VB.net 2003 I want to move up or down or side to side using the arrow keys to go to different textboxes

Any clues
Avatar of JohnK813
JohnK813
Flag of United States of America image

Well, there's a brute force method, but it requires a bit of code.

Basically, each text box has a KeyDown sub, and in each sub is a case statement to check if the key is up, down, left, or right.  This case statement then calls a .setfocus for whichever textbox the focus should move to.  This requires quite a bit of work on your part, but I can't think of an easier way offhand (of course, that doesn't mean that one doesn't exist).

Let me know if you need a better explanation, or an outline of the code.
Avatar of Mike Tomlinson
Try out  this project.  What I have done is named the textboxes in this format "TextBoxCxRy" where x is the column and y is the row of the textbox.  I have set the Form KeyPreview to true so that all keypresses are processed by the form before they reach the active control.  Whenever a key is pressed, we retrieve the name of the current control and see if it matches the above format.  If it does, we parse out the col and row values and then increment/decrement them appropriately based upon the arrow key pressed.  Then we walk the forms controls collection and see if there is a control with a name matching that of the new col/row combination to move to.  If their is, then we give it the focus.

Note that with this method, the number of columns per row and the number of rows per column does not have to the same for each column and row.

Regards,

Idle_Mind

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBoxC1R1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC2R1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC3R1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC3R2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC2R2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC1R2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC3R3 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC2R3 As System.Windows.Forms.TextBox
    Friend WithEvents TextBoxC1R3 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBoxC1R1 = New System.Windows.Forms.TextBox
        Me.TextBoxC2R1 = New System.Windows.Forms.TextBox
        Me.TextBoxC3R1 = New System.Windows.Forms.TextBox
        Me.TextBoxC3R2 = New System.Windows.Forms.TextBox
        Me.TextBoxC2R2 = New System.Windows.Forms.TextBox
        Me.TextBoxC1R2 = New System.Windows.Forms.TextBox
        Me.TextBoxC3R3 = New System.Windows.Forms.TextBox
        Me.TextBoxC2R3 = New System.Windows.Forms.TextBox
        Me.TextBoxC1R3 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'TextBoxC1R1
        '
        Me.TextBoxC1R1.Location = New System.Drawing.Point(8, 8)
        Me.TextBoxC1R1.Name = "TextBoxC1R1"
        Me.TextBoxC1R1.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC1R1.TabIndex = 0
        Me.TextBoxC1R1.Text = "TextBoxC1R1"
        '
        'TextBoxC2R1
        '
        Me.TextBoxC2R1.Location = New System.Drawing.Point(136, 8)
        Me.TextBoxC2R1.Name = "TextBoxC2R1"
        Me.TextBoxC2R1.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC2R1.TabIndex = 1
        Me.TextBoxC2R1.Text = "TextBoxC2R1"
        '
        'TextBoxC3R1
        '
        Me.TextBoxC3R1.Location = New System.Drawing.Point(264, 8)
        Me.TextBoxC3R1.Name = "TextBoxC3R1"
        Me.TextBoxC3R1.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC3R1.TabIndex = 2
        Me.TextBoxC3R1.Text = "TextBoxC3R1"
        '
        'TextBoxC3R2
        '
        Me.TextBoxC3R2.Location = New System.Drawing.Point(264, 32)
        Me.TextBoxC3R2.Name = "TextBoxC3R2"
        Me.TextBoxC3R2.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC3R2.TabIndex = 5
        Me.TextBoxC3R2.Text = "TextBoxC3R2"
        '
        'TextBoxC2R2
        '
        Me.TextBoxC2R2.Location = New System.Drawing.Point(136, 32)
        Me.TextBoxC2R2.Name = "TextBoxC2R2"
        Me.TextBoxC2R2.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC2R2.TabIndex = 4
        Me.TextBoxC2R2.Text = "TextBoxC2R2"
        '
        'TextBoxC1R2
        '
        Me.TextBoxC1R2.Location = New System.Drawing.Point(8, 32)
        Me.TextBoxC1R2.Name = "TextBoxC1R2"
        Me.TextBoxC1R2.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC1R2.TabIndex = 3
        Me.TextBoxC1R2.Text = "TextBoxC1R2"
        '
        'TextBoxC3R3
        '
        Me.TextBoxC3R3.Location = New System.Drawing.Point(264, 56)
        Me.TextBoxC3R3.Name = "TextBoxC3R3"
        Me.TextBoxC3R3.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC3R3.TabIndex = 8
        Me.TextBoxC3R3.Text = "TextBoxC3R3"
        '
        'TextBoxC2R3
        '
        Me.TextBoxC2R3.Location = New System.Drawing.Point(136, 56)
        Me.TextBoxC2R3.Name = "TextBoxC2R3"
        Me.TextBoxC2R3.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC2R3.TabIndex = 7
        Me.TextBoxC2R3.Text = "TextBoxC2R3"
        '
        'TextBoxC1R3
        '
        Me.TextBoxC1R3.Location = New System.Drawing.Point(8, 56)
        Me.TextBoxC1R3.Name = "TextBoxC1R3"
        Me.TextBoxC1R3.Size = New System.Drawing.Size(120, 20)
        Me.TextBoxC1R3.TabIndex = 6
        Me.TextBoxC1R3.Text = "TextBoxC1R3"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 302)
        Me.Controls.Add(Me.TextBoxC3R3)
        Me.Controls.Add(Me.TextBoxC2R3)
        Me.Controls.Add(Me.TextBoxC1R3)
        Me.Controls.Add(Me.TextBoxC3R2)
        Me.Controls.Add(Me.TextBoxC2R2)
        Me.Controls.Add(Me.TextBoxC1R2)
        Me.Controls.Add(Me.TextBoxC3R1)
        Me.Controls.Add(Me.TextBoxC2R1)
        Me.Controls.Add(Me.TextBoxC1R1)
        Me.KeyPreview = True
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim curControl As Control = Me.ActiveControl()
        Dim col As Integer
        Dim row As Integer
        Dim colPos As Integer
        Dim rowPos As Integer
        Dim arrowKey As Boolean = False
        Dim targetControlName As String

        If curControl.Name.Substring(0, 7).ToUpper.Equals("TEXTBOX") Then
            colPos = curControl.Name.IndexOf("C")
            rowPos = curControl.Name.IndexOf("R")
            If colPos > 0 And rowPos > 0 And rowPos > colPos Then
                Try
                    col = CInt(curControl.Name.Substring(colPos + 1, rowPos - colPos - 1))
                    row = CInt(curControl.Name.Substring(rowPos + 1))
                    Select Case e.KeyCode
                        Case Keys.Up
                            row = row - 1
                            arrowKey = True

                        Case Keys.Right
                            col = col + 1
                            arrowKey = True

                        Case Keys.Down
                            row = row + 1
                            arrowKey = True

                        Case Keys.Left
                            col = col - 1
                            arrowKey = True

                    End Select
                    If arrowKey Then
                        e.Handled = True
                        targetControlName = "TextBoxC" & col & "R" & row
                        For Each curControl In Me.Controls
                            If curControl.Name.Equals(targetControlName) Then
                                Me.ActiveControl = curControl
                                Exit Sub
                            End If
                        Next
                    End If
                Catch ex As Exception
                End Try
            End If
        End If
    End Sub
End Class
Avatar of RanjeetRain
RanjeetRain

Its not difficult. Handle it in your Form KeyDown/KeyUp.

1) If its a KeyDown/KeyUp save the Top, Left of the current text box.
2) Loop thru all TextBox instances on the form.
3) Compare their Top, Left with the Top, Left of the current item and decide which should be teh next text box.
4) Set focus to teh desired TextBox

Avatar of a1785

ASKER

RanjeetRain,

I kinda of understand can you give me a brief example.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry mate, I won't be able to write and test code. You may modify Idle_Mind's code. That's a lot of extra but that should do the job to yoru satisfaction. I will see if I can post the code but don't wait for it.