Solved

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

Posted on 2004-04-20
6
352 Views
Last Modified: 2012-05-04
Any clues
0
Comment
Question by:a1785
6 Comments
 
LVL 14

Expert Comment

by:JohnK813
Comment Utility
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
0
 
LVL 19

Expert Comment

by:RanjeetRain
Comment Utility
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:a1785
Comment Utility
RanjeetRain,

I kinda of understand can you give me a brief example.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
a1785,

Did you try the project I posted?

Idle_Mind
0
 
LVL 19

Expert Comment

by:RanjeetRain
Comment Utility
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now