dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Detecting Shift-Tab

  1. #1
    Brian Higgins Guest

    Detecting Shift-Tab


    In my previous posting, I probably was not clear regarding my issue. I will
    try again as this may help others suggest a solution.

    I have a form with about 10 text boxes that I wish to control how the user
    moves between the text boxes depending upon the values in those text boxes
    rather than simply allowing the user to tab between the text boxes using
    the tab values of the controls. The following code snippets are how I am
    attempting to accomplish this...

    In the GotFocus event for each text box I have the following code required
    to turn off the tab stops for all controls:

    On Error Resume Next
    For i = 0 to Controls.Count - 1
    Controls(i).TabStop = False
    Next I

    The above code allows me to detect when the Tab key is pressed in each text
    box (the only way you can detect when the Tab key is pressed in a text box
    is to first turn off the tab stops for all controls) -- then use the following
    code in the KeyPress event for the text box (the code below also detects
    if the Enter key pressed):

    If KeyAscii = 13 or KeyAscii = 9 then '13 = Enter & 9 = Tab
    KeyAscii = 0
    TextX.SetFocus 'Where TextX refers to the destination
    Exit Sub ' text box when the user presses either
    End If ' the Enter or Tab keys

    I then restore the tab stops by placing the following code in the LostFocus
    event for each text box:

    On Error Resume Next
    For i = 0 to Controls.Count - 1
    Controls(i).TabStop = True
    Next I

    My problem arises when I also wish to detect the Shift-Tab combinition.
    I use the following code in the KeyDown event for each text box:

    If Shift = 1 and KeyCode = vbKeyTab then
    TextY.SetFocus 'Where TextY refers to the destination
    Exit Sub ' text box when the user presses the
    End If ' Shift-Tab combination

    When I run the application, each text box correctly detects when the Enter
    or Tab key is pressed using the KeyPress event and moves the focus to the
    appropriate text box. However, when the Shift-Tab key combination is pressed,
    the focus moves to the text box as though only the Tab key was pressed --
    the KeyDown event does not seem to be detected -- only the KeyPress event.
    From what other have suggested, the KeyDown event is fired before the KeyPress
    event and should work, but is doesn't.

    What makes it even more mysterious is that the KeyDown event will work properly
    if I insert a message box in the KeyDown event such as...

    If Shift = 1 and KeyCode = vbKeyTab then
    MsgBox ""
    TextY.SetFocus 'Where TextY refers to the destination
    Exit Sub ' text box when the user presses the
    End If ' Shift-Tab combination

    However, this is totally impractical as the message box will be displayed
    every time the user presses the Shift-Tab combination. But the above code
    (including the message box) proves that the KeyDown event is detected and
    properly moves the focus. My question is why does it only work when the
    message box is inserted and how can the KeyDown event be made to work without
    the message box???

    I hope this clarifies the problem and if there is another way to detect the
    Shift-Tab combination I would really appreciate some advice.

    Thanks.







  2. #2
    Sue Harsevoort Guest

    Re: Detecting Shift-Tab

    I didn't have enough text boxes and code last night so that is why it work
    for me. But this morning when I was thinking more clearly I figured out
    what the problem was. The KeyDown event does fire first, but the KeyPress
    still fires so it is the last code to fire and so the focus ends up in the
    text box that is in the Key Press code. The message box seems to eat the
    KeyPress event and it never fires when the message box is shown. I would
    think the easiest fix would be to put all the code for moving between text
    boxes in the Key Down event. You will probably want to keep the code that
    set the KeyAscii = 0 when the tab or enter is pressed in the Key Press event
    so you don't get a beep everytime.

    Sue

    "Brian Higgins" <brian.higgins@burke.com> wrote in message
    news:398a9e29$1@news.devx.com...
    >
    > In my previous posting, I probably was not clear regarding my issue. I

    will
    > try again as this may help others suggest a solution.
    >
    > I have a form with about 10 text boxes that I wish to control how the user
    > moves between the text boxes depending upon the values in those text boxes
    > rather than simply allowing the user to tab between the text boxes using
    > the tab values of the controls. The following code snippets are how I am
    > attempting to accomplish this...
    >
    > In the GotFocus event for each text box I have the following code required
    > to turn off the tab stops for all controls:
    >
    > On Error Resume Next
    > For i = 0 to Controls.Count - 1
    > Controls(i).TabStop = False
    > Next I
    >
    > The above code allows me to detect when the Tab key is pressed in each

    text
    > box (the only way you can detect when the Tab key is pressed in a text box
    > is to first turn off the tab stops for all controls) -- then use the

    following
    > code in the KeyPress event for the text box (the code below also detects
    > if the Enter key pressed):
    >
    > If KeyAscii = 13 or KeyAscii = 9 then '13 = Enter & 9 = Tab
    > KeyAscii = 0
    > TextX.SetFocus 'Where TextX refers to the destination
    > Exit Sub ' text box when the user presses either
    > End If ' the Enter or Tab keys
    >
    > I then restore the tab stops by placing the following code in the

    LostFocus
    > event for each text box:
    >
    > On Error Resume Next
    > For i = 0 to Controls.Count - 1
    > Controls(i).TabStop = True
    > Next I
    >
    > My problem arises when I also wish to detect the Shift-Tab combinition.
    > I use the following code in the KeyDown event for each text box:
    >
    > If Shift = 1 and KeyCode = vbKeyTab then
    > TextY.SetFocus 'Where TextY refers to the destination
    > Exit Sub ' text box when the user presses the
    > End If ' Shift-Tab combination
    >
    > When I run the application, each text box correctly detects when the Enter
    > or Tab key is pressed using the KeyPress event and moves the focus to the
    > appropriate text box. However, when the Shift-Tab key combination is

    pressed,
    > the focus moves to the text box as though only the Tab key was pressed --
    > the KeyDown event does not seem to be detected -- only the KeyPress event.
    > From what other have suggested, the KeyDown event is fired before the

    KeyPress
    > event and should work, but is doesn't.
    >
    > What makes it even more mysterious is that the KeyDown event will work

    properly
    > if I insert a message box in the KeyDown event such as...
    >
    > If Shift = 1 and KeyCode = vbKeyTab then
    > MsgBox ""
    > TextY.SetFocus 'Where TextY refers to the destination
    > Exit Sub ' text box when the user presses the
    > End If ' Shift-Tab combination
    >
    > However, this is totally impractical as the message box will be displayed
    > every time the user presses the Shift-Tab combination. But the above code
    > (including the message box) proves that the KeyDown event is detected and
    > properly moves the focus. My question is why does it only work when the
    > message box is inserted and how can the KeyDown event be made to work

    without
    > the message box???
    >
    > I hope this clarifies the problem and if there is another way to detect

    the
    > Shift-Tab combination I would really appreciate some advice.
    >
    > Thanks.
    >
    >
    >
    >
    >
    >




  3. #3
    Brian Higgins Guest

    Re: Detecting Shift-Tab


    "Sue Harsevoort" <HarsevoortS@empireco.com> wrote:
    >I didn't have enough text boxes and code last night so that is why it work
    >for me. But this morning when I was thinking more clearly I figured out
    >what the problem was. The KeyDown event does fire first, but the KeyPress
    >still fires so it is the last code to fire and so the focus ends up in the
    >text box that is in the Key Press code. The message box seems to eat the
    >KeyPress event and it never fires when the message box is shown. I would
    >think the easiest fix would be to put all the code for moving between text
    >boxes in the Key Down event. You will probably want to keep the code that
    >set the KeyAscii = 0 when the tab or enter is pressed in the Key Press event
    >so you don't get a beep everytime.
    >
    >Sue
    >
    >"Brian Higgins" <brian.higgins@burke.com> wrote in message
    >news:398a9e29$1@news.devx.com...
    >>
    >> In my previous posting, I probably was not clear regarding my issue.

    I
    >will
    >> try again as this may help others suggest a solution.
    >>
    >> I have a form with about 10 text boxes that I wish to control how the

    user
    >> moves between the text boxes depending upon the values in those text boxes
    >> rather than simply allowing the user to tab between the text boxes using
    >> the tab values of the controls. The following code snippets are how I

    am
    >> attempting to accomplish this...
    >>
    >> In the GotFocus event for each text box I have the following code required
    >> to turn off the tab stops for all controls:
    >>
    >> On Error Resume Next
    >> For i = 0 to Controls.Count - 1
    >> Controls(i).TabStop = False
    >> Next I
    >>
    >> The above code allows me to detect when the Tab key is pressed in each

    >text
    >> box (the only way you can detect when the Tab key is pressed in a text

    box
    >> is to first turn off the tab stops for all controls) -- then use the

    >following
    >> code in the KeyPress event for the text box (the code below also detects
    >> if the Enter key pressed):
    >>
    >> If KeyAscii = 13 or KeyAscii = 9 then '13 = Enter & 9 = Tab
    >> KeyAscii = 0
    >> TextX.SetFocus 'Where TextX refers to the destination
    >> Exit Sub ' text box when the user presses either
    >> End If ' the Enter or Tab keys
    >>
    >> I then restore the tab stops by placing the following code in the

    >LostFocus
    >> event for each text box:
    >>
    >> On Error Resume Next
    >> For i = 0 to Controls.Count - 1
    >> Controls(i).TabStop = True
    >> Next I
    >>
    >> My problem arises when I also wish to detect the Shift-Tab combinition.
    >> I use the following code in the KeyDown event for each text box:
    >>
    >> If Shift = 1 and KeyCode = vbKeyTab then
    >> TextY.SetFocus 'Where TextY refers to the destination
    >> Exit Sub ' text box when the user presses the
    >> End If ' Shift-Tab combination
    >>
    >> When I run the application, each text box correctly detects when the Enter
    >> or Tab key is pressed using the KeyPress event and moves the focus to

    the
    >> appropriate text box. However, when the Shift-Tab key combination is

    >pressed,
    >> the focus moves to the text box as though only the Tab key was pressed

    --
    >> the KeyDown event does not seem to be detected -- only the KeyPress event.
    >> From what other have suggested, the KeyDown event is fired before the

    >KeyPress
    >> event and should work, but is doesn't.
    >>
    >> What makes it even more mysterious is that the KeyDown event will work

    >properly
    >> if I insert a message box in the KeyDown event such as...
    >>
    >> If Shift = 1 and KeyCode = vbKeyTab then
    >> MsgBox ""
    >> TextY.SetFocus 'Where TextY refers to the destination
    >> Exit Sub ' text box when the user presses the
    >> End If ' Shift-Tab combination
    >>
    >> However, this is totally impractical as the message box will be displayed
    >> every time the user presses the Shift-Tab combination. But the above

    code
    >> (including the message box) proves that the KeyDown event is detected

    and
    >> properly moves the focus. My question is why does it only work when the
    >> message box is inserted and how can the KeyDown event be made to work

    >without
    >> the message box???
    >>
    >> I hope this clarifies the problem and if there is another way to detect

    >the
    >> Shift-Tab combination I would really appreciate some advice.
    >>
    >> Thanks.
    >>
    >>
    >>
    >>
    >>
    >>

    >
    >


    Sue,

    Thanks very much -- it works like a charm!!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center