Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
361 Views
Last Modified: 2012-05-04
Any clues
0
Comment
Question by:a1785
6 Comments
 
LVL 14

Expert Comment

by:JohnK813
ID: 10872346
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 86

Expert Comment

by:Mike Tomlinson
ID: 10872496
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
ID: 10876166
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:a1785
ID: 10877629
RanjeetRain,

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

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 10882182
a1785,

Did you try the project I posted?

Idle_Mind
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 10885766
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

971 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