|
-
Nov 9th, 2006, 04:07 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] 2 Controls and Tabbing
Assume that there are 3 controls on a form, a ComboBox, a TextBox and a Button. The TextBox is disabled when the form is loaded. When the user selects a value in the ComboBox and then tabs out of the control, if the ComboBox passes validation, the TextBox is then enabled.
The problem that I am having is that by the time validating and validated have been processed for the ComboBox and TextBox is enabled, the form has already determined that it is going to move to the Button. I need for it to move to the TextBox.
I have played around with using .Focus(), but this causes problems when the user tries to Shift-Tab.
This seems so simple, yet it has been giving me troubles trying to get around it.
My.Settings.Signature = String.Empty
-
Nov 9th, 2006, 04:14 PM
#2
Re: 2 Controls and Tabbing
Instead of enabling/disabling the control, alter its readonly property. This will prevent the user from entering data when you don't want them to, as well as keep the tab order intact.
-
Nov 9th, 2006, 04:18 PM
#3
Thread Starter
Fanatic Member
Re: 2 Controls and Tabbing
If I were to go this route and just use TextBox as ReadOnly, I think it would cause some confusion to people as to why they were able to tab through it, even if I didn't enable it.
My.Settings.Signature = String.Empty
-
Nov 9th, 2006, 04:23 PM
#4
Re: 2 Controls and Tabbing
You could but a couple lines in the textbox's GotFocus event, to check the readonly value and set focus to the next control if need be.
-
Nov 9th, 2006, 04:31 PM
#5
Re: 2 Controls and Tabbing
I just tested this code and it works fine. the code in the "Validating" event is just for test.
VB Code:
Private Sub ComboBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Validated
Me.TextBox1.Focus()
End Sub
Private Sub ComboBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
If ComboBox1.SelectedIndex = -1 Then
Me.TextBox1.Enabled = True
End If
End Sub
-
Nov 9th, 2006, 04:40 PM
#6
Thread Starter
Fanatic Member
Re: 2 Controls and Tabbing
That is the exact code I have in the live application. However, there is a control just before the ComboBox that the office manager was trying to Shift-Tab back to. You can guess what she ended up with.
My.Settings.Signature = String.Empty
-
Nov 9th, 2006, 04:47 PM
#7
Re: 2 Controls and Tabbing
-
Nov 9th, 2006, 04:51 PM
#8
Re: 2 Controls and Tabbing
If you use VS. 2005 then you can add a condition in the Validated event to check if the shift key is down and then set the focuse acordingly.
-
Dec 27th, 2006, 11:54 AM
#9
Thread Starter
Fanatic Member
Re: 2 Controls and Tabbing
I realize that this thread is rather old, but I am just now trying to get back to this. How would I go about checking to see if the Shift key is down in the Validated event?
My.Settings.Signature = String.Empty
-
Dec 27th, 2006, 11:58 AM
#10
Re: 2 Controls and Tabbing
I have never used the validating event, so somebody could probably tell me that in certain cases it is necessary, but you might consider handling all validation in the click event for the combobox. Depending on exactly what you are doing during validation, this may not be what you want, since a mistaken choice could cause undesired behavior, but in most cases, you don't really need to worry whether the user knows what they are doing or not: They don't, so just proceed based on what they choose.
My usual boring signature: Nothing
 
-
Dec 27th, 2006, 12:24 PM
#11
Thread Starter
Fanatic Member
Re: 2 Controls and Tabbing
I have been playing around with trying to determine if the Shift key is pressed during the Validating event, but cannot seem to get that information.
There really has to be a MUCH simpler way to do this.
My.Settings.Signature = String.Empty
-
Dec 27th, 2006, 02:32 PM
#12
Re: 2 Controls and Tabbing
You did not mention what version of VS you are using. I know three ways to get it to know if the Shift key is pressed.
VB Code:
'This works for VS. 2005
If My.Computer.Keyboard.ShiftKeyDown Then
MessageBox.Show("Shift Key was pressed.")
End If
VB Code:
'This works for VS. 2005 but i don't know if it will work for previous versions
Dim d As New Devices.Keyboard
If d.ShiftKeyDown Then
MessageBox.Show("Shift Key was pressed.")
End If
And the last one would be using “GetAsyncKeyState” api call to see if the key is pressed. Check the api section of this forum for “GetAsyncKeyState” function.
There might be other ways to do it.
-
Dec 27th, 2006, 03:04 PM
#13
Thread Starter
Fanatic Member
Re: 2 Controls and Tabbing
Perfect. That does what I needed.
PS - I am using 2005.
Thanks!!!
My.Settings.Signature = String.Empty
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
|