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
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.ICon tainer
'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.TextB ox
Friend WithEvents TextBoxC2R1 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC3R1 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC3R2 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC2R2 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC1R2 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC3R3 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC2R3 As System.Windows.Forms.TextB ox
Friend WithEvents TextBoxC1R3 As System.Windows.Forms.TextB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.TextBoxC1R1 = New System.Windows.Forms.TextB ox
Me.TextBoxC2R1 = New System.Windows.Forms.TextB ox
Me.TextBoxC3R1 = New System.Windows.Forms.TextB ox
Me.TextBoxC3R2 = New System.Windows.Forms.TextB ox
Me.TextBoxC2R2 = New System.Windows.Forms.TextB ox
Me.TextBoxC1R2 = New System.Windows.Forms.TextB ox
Me.TextBoxC3R3 = New System.Windows.Forms.TextB ox
Me.TextBoxC2R3 = New System.Windows.Forms.TextB ox
Me.TextBoxC1R3 = New System.Windows.Forms.TextB ox
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.TextBox C3R3)
Me.Controls.Add(Me.TextBox C2R3)
Me.Controls.Add(Me.TextBox C1R3)
Me.Controls.Add(Me.TextBox C3R2)
Me.Controls.Add(Me.TextBox C2R2)
Me.Controls.Add(Me.TextBox C1R2)
Me.Controls.Add(Me.TextBox C3R1)
Me.Controls.Add(Me.TextBox C2R1)
Me.Controls.Add(Me.TextBox C1R1)
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.KeyEv entArgs) 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.Subst ring(colPo s + 1, rowPos - colPos - 1))
row = CInt(curControl.Name.Subst ring(rowPo s + 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(tar getControl Name) 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
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.ICon
'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.TextB
Friend WithEvents TextBoxC2R1 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC3R1 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC3R2 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC2R2 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC1R2 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC3R3 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC2R3 As System.Windows.Forms.TextB
Friend WithEvents TextBoxC1R3 As System.Windows.Forms.TextB
<System.Diagnostics.Debugg
Me.TextBoxC1R1 = New System.Windows.Forms.TextB
Me.TextBoxC2R1 = New System.Windows.Forms.TextB
Me.TextBoxC3R1 = New System.Windows.Forms.TextB
Me.TextBoxC3R2 = New System.Windows.Forms.TextB
Me.TextBoxC2R2 = New System.Windows.Forms.TextB
Me.TextBoxC1R2 = New System.Windows.Forms.TextB
Me.TextBoxC3R3 = New System.Windows.Forms.TextB
Me.TextBoxC2R3 = New System.Windows.Forms.TextB
Me.TextBoxC1R3 = New System.Windows.Forms.TextB
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.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
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.KeyEv
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(
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.Subst
row = CInt(curControl.Name.Subst
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(tar
Me.ActiveControl = curControl
Exit Sub
End If
Next
End If
Catch ex As Exception
End Try
End If
End If
End Sub
End Class
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
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
ASKER
RanjeetRain,
I kinda of understand can you give me a brief example.
I kinda of understand can you give me a brief example.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.