Results 1 to 13 of 13

Thread: [RESOLVED] 2 Controls and Tabbing

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Resolved [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

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. Private Sub ComboBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Validated
    2.         Me.TextBox1.Focus()
    3.     End Sub
    4.  
    5.     Private Sub ComboBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ComboBox1.Validating
    6.         If ComboBox1.SelectedIndex = -1 Then
    7.             Me.TextBox1.Enabled = True
    8.         End If
    9.     End Sub

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: 2 Controls and Tabbing

    ye that wont help.

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. 'This works for VS. 2005
    2.         If My.Computer.Keyboard.ShiftKeyDown Then
    3.             MessageBox.Show("Shift Key was pressed.")
    4.         End If
    VB Code:
    1. 'This works for VS. 2005 but i don't know if it will work for previous versions
    2.         Dim d As New Devices.Keyboard
    3.         If d.ShiftKeyDown Then
    4.             MessageBox.Show("Shift Key was pressed.")
    5.         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.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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
  •  



Click Here to Expand Forum to Full Width