+ Reply to Thread
Results 1 to 10 of 10

Keeping The Focus From Changing. How?

  1. #1
    Registered User
    Join Date
    01-09-2004
    Location
    Chicago
    Posts
    31

    Keeping The Focus From Changing. How?

    Hi,

    I'm designing a userform for data entry.

    I'd like to have the userform set up that I can put text in a textbox and press enter, then it does what it's supposed to do and clears the textbox so that I can type new text in. I've seen other vb programs that do this.

    With the way things are now, when I press enter it does everything it's supposed to do, but the focus always moves to the next control.

    Is there anyway that I can stop the focus from moving to the next control when I press enter?

    Thanks a lot for the help. I appreciate it.

  2. #2
    Tom Ogilvy
    Guest

    Re: Keeping The Focus From Changing. How?

    In the Exit event, set cancel = true

    In the same event,

    Textbox1.Text = ""

    --
    Regards,
    Tom Ogilvy

    "abxy" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Hi,
    >
    > I'm designing a userform for data entry.
    >
    > I'd like to have the userform set up that I can put text in a textbox
    > and press enter, then it does what it's supposed to do and clears the
    > textbox so that I can type new text in. I've seen other vb programs
    > that do this.
    >
    > With the way things are now, when I press enter it does everything it's
    > supposed to do, but the focus always moves to the next control.
    >
    > Is there anyway that I can stop the focus from moving to the next
    > control when I press enter?
    >
    > Thanks a lot for the help. I appreciate it.
    >
    >
    > --
    > abxy
    > ------------------------------------------------------------------------
    > abxy's Profile:

    http://www.excelforum.com/member.php...fo&userid=4730
    > View this thread: http://www.excelforum.com/showthread...hreadid=400574
    >




  3. #3
    Registered User
    Join Date
    01-09-2004
    Location
    Chicago
    Posts
    31
    Works perfect. Thanks a lot mate

  4. #4
    Registered User
    Join Date
    01-09-2004
    Location
    Chicago
    Posts
    31
    Actually, this isn't working as perfect as I thought.

    Now, I can't click out of the textbox control.

    I'm trying to think of a way in code to say: if I press enter, don't change the focus (cancel = true), but if I click out of the control then change the focus like normal (cancel = false).

  5. #5
    Doug Glancy
    Guest

    Re: Keeping The Focus From Changing. How?

    abxy

    You need a global variable to track whether enter was pressed. If it was,
    then do the actions as Tom described, else behave normally, i.e., you can
    click or tab out of the textbox:

    Dim enter_pressed As Boolean ' this is above all subroutines
    Option Explicit
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If enter_pressed Then 'if it was set in the keydown event below
    Cancel = True
    Me.TextBox1 = ""
    enter_pressed = False 'clear it, so back to normal behavior
    End If
    End Sub

    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
    Shift As Integer)
    If KeyCode = 13 Then ' enter key
    enter_pressed = True
    End If
    End Sub

    hth,

    Doug

    "abxy" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Actually, this isn't working as perfect as I thought.
    >
    > Now, I can't click out of the textbox control.
    >
    > I'm trying to think of a way in code to say: if I press enter, don't
    > change the focus (cancel = true), but if I click out of the control
    > then change the focus like normal (cancel = false).
    >
    >
    > --
    > abxy
    > ------------------------------------------------------------------------
    > abxy's Profile:
    > http://www.excelforum.com/member.php...fo&userid=4730
    > View this thread: http://www.excelforum.com/showthread...hreadid=400574
    >




  6. #6
    Iain King
    Guest

    Re: Keeping The Focus From Changing. How?


    Doug Glancy wrote:
    > abxy
    >
    > You need a global variable to track whether enter was pressed. If it was,
    > then do the actions as Tom described, else behave normally, i.e., you can
    > click or tab out of the textbox:
    >
    > Dim enter_pressed As Boolean ' this is above all subroutines
    > Option Explicit
    > Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > If enter_pressed Then 'if it was set in the keydown event below
    > Cancel = True
    > Me.TextBox1 = ""
    > enter_pressed = False 'clear it, so back to normal behavior
    > End If
    > End Sub
    >
    > Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
    > Shift As Integer)
    > If KeyCode = 13 Then ' enter key
    > enter_pressed = True
    > End If
    > End Sub
    >
    > >


    or more simply, just put the code you want to run when enter is pushed
    in the KeyDown event.

    Iain


  7. #7
    Doug Glancy
    Guest

    Re: Keeping The Focus From Changing. How?

    Iain,

    I couldn't get what you suggest to work the way abxy wants. There is no
    cancel event in KeyDown event and SetFocus didn't bring it back to TextBox1.
    I'd be interested to see the code you'd use.

    Doug

    "Iain King" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Doug Glancy wrote:
    >> abxy
    >>
    >> You need a global variable to track whether enter was pressed. If it
    >> was,
    >> then do the actions as Tom described, else behave normally, i.e., you can
    >> click or tab out of the textbox:
    >>
    >> Dim enter_pressed As Boolean ' this is above all subroutines
    >> Option Explicit
    >> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    >> If enter_pressed Then 'if it was set in the keydown event below
    >> Cancel = True
    >> Me.TextBox1 = ""
    >> enter_pressed = False 'clear it, so back to normal behavior
    >> End If
    >> End Sub
    >>
    >> Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    >> ByVal
    >> Shift As Integer)
    >> If KeyCode = 13 Then ' enter key
    >> enter_pressed = True
    >> End If
    >> End Sub
    >>
    >> >

    >
    > or more simply, just put the code you want to run when enter is pushed
    > in the KeyDown event.
    >
    > Iain
    >




  8. #8
    Iain King
    Guest

    Re: Keeping The Focus From Changing. How?


    Doug Glancy wrote:
    > Iain,
    >
    > I couldn't get what you suggest to work the way abxy wants. There is no
    > cancel event in KeyDown event and SetFocus didn't bring it back to TextBox1.
    > I'd be interested to see the code you'd use.
    >


    To cancel keydown, set KeyCode to 0.

    I think his situation is something like having a textbox where you
    enter, say, a name, with an 'Add' button beside it, which adds that
    name to a list, then:

    Private Sub addName()
    list.Add(TextBox1.Value)
    TextBox1.Value = ""
    TextBox1.SetFocus()
    End Sub

    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    ByVal Shift As Integer)
    If KeyCode = 13 Then ' enter key
    KeyCode = 0
    addName()
    End If
    End Sub

    Private Sub Add_Click()
    addName()
    End Sub



    > Doug
    >
    > "Iain King" <[email protected]> wrote in message
    > news:[email protected]...
    > >
    > > Doug Glancy wrote:
    > >> abxy
    > >>
    > >> You need a global variable to track whether enter was pressed. If it
    > >> was,
    > >> then do the actions as Tom described, else behave normally, i.e., you can
    > >> click or tab out of the textbox:
    > >>
    > >> Dim enter_pressed As Boolean ' this is above all subroutines
    > >> Option Explicit
    > >> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    > >> If enter_pressed Then 'if it was set in the keydown event below
    > >> Cancel = True
    > >> Me.TextBox1 = ""
    > >> enter_pressed = False 'clear it, so back to normal behavior
    > >> End If
    > >> End Sub
    > >>
    > >> Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    > >> ByVal
    > >> Shift As Integer)
    > >> If KeyCode = 13 Then ' enter key
    > >> enter_pressed = True
    > >> End If
    > >> End Sub
    > >>
    > >> >

    > >
    > > or more simply, just put the code you want to run when enter is pushed
    > > in the KeyDown event.
    > >
    > > Iain
    > >



  9. #9
    Doug Glancy
    Guest

    Re: Keeping The Focus From Changing. How?

    Iain,

    Thanks. I didn't think of KeyCode = 0. That's a useful thing to know.

    Doug

    "Iain King" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Doug Glancy wrote:
    >> Iain,
    >>
    >> I couldn't get what you suggest to work the way abxy wants. There is no
    >> cancel event in KeyDown event and SetFocus didn't bring it back to
    >> TextBox1.
    >> I'd be interested to see the code you'd use.
    >>

    >
    > To cancel keydown, set KeyCode to 0.
    >
    > I think his situation is something like having a textbox where you
    > enter, say, a name, with an 'Add' button beside it, which adds that
    > name to a list, then:
    >
    > Private Sub addName()
    > list.Add(TextBox1.Value)
    > TextBox1.Value = ""
    > TextBox1.SetFocus()
    > End Sub
    >
    > Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    > ByVal Shift As Integer)
    > If KeyCode = 13 Then ' enter key
    > KeyCode = 0
    > addName()
    > End If
    > End Sub
    >
    > Private Sub Add_Click()
    > addName()
    > End Sub
    >
    >
    >
    >> Doug
    >>
    >> "Iain King" <[email protected]> wrote in message
    >> news:[email protected]...
    >> >
    >> > Doug Glancy wrote:
    >> >> abxy
    >> >>
    >> >> You need a global variable to track whether enter was pressed. If it
    >> >> was,
    >> >> then do the actions as Tom described, else behave normally, i.e., you
    >> >> can
    >> >> click or tab out of the textbox:
    >> >>
    >> >> Dim enter_pressed As Boolean ' this is above all subroutines
    >> >> Option Explicit
    >> >> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    >> >> If enter_pressed Then 'if it was set in the keydown event below
    >> >> Cancel = True
    >> >> Me.TextBox1 = ""
    >> >> enter_pressed = False 'clear it, so back to normal behavior
    >> >> End If
    >> >> End Sub
    >> >>
    >> >> Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    >> >> ByVal
    >> >> Shift As Integer)
    >> >> If KeyCode = 13 Then ' enter key
    >> >> enter_pressed = True
    >> >> End If
    >> >> End Sub
    >> >>
    >> >> >
    >> >
    >> > or more simply, just put the code you want to run when enter is pushed
    >> > in the KeyDown event.
    >> >
    >> > Iain
    >> >

    >




  10. #10
    Registered User
    Join Date
    01-09-2004
    Location
    Chicago
    Posts
    31
    Iain, this works very perfectly. My problem is now solved.

    keycode = 0 was the key to the solution all along. Thanks for showing me (us) that.

    And a big thankyou to everyone that contributed to the thread to help me get over this hump.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1