Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jun 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't hide control in 'LostFocus' handler (2000/SR-1)

    When I click tbox1, I make cbox1 visible, give cbox1 the focus, and hide tbox1. Life is good. Works like a champ.

    Private Sub tbox1_Click ()
    cbox1.visible = True
    cbox1.setfocus
    tbox1.visible = False
    End Sub

    When cbox1 loses the focus, say from clicking any other control foo on the form, I want to hide cbox1 and make tbox1 visible, and still end up focused on foo.

    Private Sub cbox1_LostFocus ()
    cbox1.visible = False
    tbox1.visible = True
    End Sub

    Unfortunately, cbox1.visible = False fails with Run-time error '2165': You can't hide a control that has the focus."

    So I guess the Lost Focus event really means: "is about to lose the focus, but not until the Lost Focus event is finished", right?

    Is there a better solution than to put the "hide cbox1 and make tbox1 visible" code in each other controls foo_click routine?

    Thanks for your help.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can't hide control in 'LostFocus' handler (2000/SR-1)

    LostFocus indeed means that the control is about to lose the focus. You must set focus to something else before you make the control that has the focus invisible. Use code that exactly mirrors tbox1_Click:

    Private Sub cbox1_LostFocus()
    tbox1.Visible = True
    tbox1.SetFocus
    cbox1.Visible = False
    End Sub

    What would you expect of "a better solution"?

  3. #3
    New Lounger
    Join Date
    Jun 2002
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't hide control in 'LostFocus' handler (2000/SR-1)

    Better solution lets me click *any* other control to hide the cbox.

    Put tbox1 in front of cbox1, leave cbox1 visible all the time, and toggle tbox1.visible true and false to hide and show cbox1.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can't hide control in 'LostFocus' handler (2000/SR-1)

    I don't think that there is a generic solution, but you might take a look at Screen.PreviousControl. This holds the control that had the focus before the ActiveControl had focus. You would still have to put code in the OnFocus event of all other controls, but this could be a simple call to a common routine:

    Private Sub HideCombo()
    On Error Goto ExitHandler
    If Screen.PreviousControl.Name = "cbox1" Then
    cbox1.Visible = False
    End If
    ExitHandler:
    End Sub

    Private Sub tbox1_Focus()
    HideCombo
    End Sub

    Private Sub tbox2_Focus()
    HideCombo
    End Sub

    The On Error is meant to avoid problems when there is no PreviousControl.

Posting Permissions

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