|
-
Jul 30th, 2009, 03:39 AM
#1
Thread Starter
Fanatic Member
Navigate Textboxes
How do I easily navigate textboxes contained within a form.
My form has a lot of textboxes so I don't particularly want to make a KeyDown event for each.
I think I need to group the controls and handle it that way somehow with their TabIndex.
I'm lost without the control array of VB6 and can't find anything to show me how to do it.
-
Jul 30th, 2009, 03:45 AM
#2
Re: Navigate Textboxes
What do you mean by navigate?
You are talking about the KeyDown event, maybe you want to handle the KeyDown event for every button? In that case, you can have one method that handles all the KeyDown events of all buttons. You simply select all buttons in the designer simultaneously, and select the required event in the property grid (when the events button is active).
You will get something like this (note the long string of Handles at the end):
Code:
Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, ... TextBox81.KeyDown
End Sub
To figure out which button raised the event, you have the sender argument. The sender is the control that raised the event, but it's not of the right type yet (it's a Control, not a TextBox). All you need to do is cast it to the right type:
Code:
Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, TextBox3.KeyDown, TextBox4.KeyDown, ... TextBox81.KeyDown
Dim tb As TextBox = DirectCast(sender, TextBox)
tb.BackColor = Color.Red
End Sub
So, while there are no control arrays during desing-time as in VB6, there are much better alternatives in .NET. Believe me, once you get the hang of it, you never want to go back.
-
Jul 30th, 2009, 03:45 AM
#3
Re: Navigate Textboxes
In the design view of the form if you go to View menu you have there the TabOrder, where you can set the order of the text box's...
To use the same sub for multiple textbox's just add several controls to the same sub.
VB.NET Code:
Private Sub text1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles text1.KeyDown, text2.KeyDown, text3.KeyDown 'Code End Sub
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Jul 30th, 2009, 04:15 AM
#4
Thread Starter
Fanatic Member
Re: Navigate Textboxes
Uh huh. Thankyou.
I've just worked out why I was a little confused. In the web developer, controls have a property named GroupName. So you can probably move through the taborder of the group items.
Say I have 4 textboxes arranged in a square..how would I navigate between them using the arrow keys?
Last edited by sgrya1; Jul 30th, 2009 at 04:20 AM.
-
Jul 30th, 2009, 02:31 PM
#5
Re: Navigate Textboxes
You can do something like this:
VB.NET Code:
Private Sub txt1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUpLeft.KeyDown, txtUpRight.KeyDown, txtDownLeft.KeyDown, txtDownRight.KeyDown
Dim strTxt As String = sender.name
Select Case strTxt
Case "txtUpLeft"
If e.KeyCode = Keys.Right Then
txtUpRight.Focus()
ElseIf e.KeyCode = Keys.Down Then
txtDownLeft.Focus()
End If
Case "txtUpRight"
If e.KeyCode = Keys.Left Then
txtUpLeft.Focus()
ElseIf e.KeyCode = Keys.Down Then
txtDownRight.Focus()
End If
Case "txtDownLeft"
If e.KeyCode = Keys.Up Then
txtUpLeft.Focus()
ElseIf e.KeyCode = Keys.Right Then
txtDownRight.Focus()
End If
Case "txtDownRight"
If e.KeyCode = Keys.Up Then
txtUpRight.Focus()
ElseIf e.KeyCode = Keys.Left Then
txtDownLeft.Focus()
End If
End Select
End Sub
Rate People That Helped You
Mark Thread Resolved When Resolved
-
Jul 30th, 2009, 04:23 PM
#6
Thread Starter
Fanatic Member
Re: Navigate Textboxes
Ohh....
I haven't checked it yet but that looks arr(adjectives) awesome!
I am trying to convert a VB6 project which had a control arrays to deal with the problem and it was hundreds of lines of if's and buts.
-
Jul 31st, 2009, 01:06 PM
#7
Thread Starter
Fanatic Member
Re: Navigate Textboxes
That explains a lot but I think I spoke too soon. First time I looked at it I thought it was a general event for all textboxes on a form. Now that I look at it I still need to create an event handler for each individual text box.
I think VB6 was a bit easier with control arrays where you could set the next or previous control index number.
I have a lot of textboxes so the hundreds of lines in VB6 will increase a lot.
-
Jul 31st, 2009, 01:10 PM
#8
Re: Navigate Textboxes
Control arrays were not magic. The only difference was that VB6 would let you create a control array in the designer and that was only needed because of the way events were handled. Events are handled differently in VB.NET so design-time support for control arrays is not required. If you want an array of controls then you are free to create one in code, just as you would do for an other type of array. You can then access controls by their index in that array, just as you can for elements of any other array.
-
Jul 31st, 2009, 01:40 PM
#9
Thread Starter
Fanatic Member
Re: Navigate Textboxes
Thanks jncilhinney,
My issue is that I would like to create these controls ine the IDE and not spend time creating and positioning them at design time.
There is some logic in the new system as VB6 you had to understand which controls had which index.
My form has about 50 text boxes which are all relatively positioned to each other. It's just a shame that I need to create an event for each text box.
Was hoping ther was some code that says that "if the next closest text box is below the active text box then go there" if right then go there etc.
Last edited by sgrya1; Jul 31st, 2009 at 01:43 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|