CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    SetFocus problem

    There's something weird going on, again.
    I have some code in the validate event of a listbox. Depending on the choise several textboxes will change from disabled to enabled.
    The textbox is the next in the tabindex, but the focus goes to the first enabled element in the tabindex (that is, before the textbox was enabled).
    When I then use the SetFocus to force the focus to the enabled box, the focus still ends up at the wrong control:

    this examples uses 3 textboxes, text2 is disabled, text3 is enabled
    the tabindex for text1 = 1, text2 = 2, text3 = 3.
    it should go from 1 to 2 to 3, but it doesn't

    private Sub Text1_Validate(Cancel as Boolean)
    If text1.text <> "" then
    text2.enabled = true
    else
    text2.enabled = false
    end if
    End Sub



    When using this code, it goes from text1 to text3, even when there's text in text1.

    private Sub Text1_Validate(Cancel as Boolean)
    If text1.text <> "" then
    text2.enabled = true
    text2.setfocus
    else
    text2.enabled = false
    end if
    End Sub



    When using this code, it still goes from text1 to text3, although I specifically told him to go to text2

    Any comments/suggestions?

    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: SetFocus problem

    The problem is that the Validate() event occurs after the textbox control has notified the parent form that it has lost the focus.

    At this point the control then decides where to put the focus and then calls the Validate event().

    The cludge fix for this is to have a picturebox control with tabindex one more than the listbox and in its gotfocus event:

    public Sub picDummy_GotFocus()

    If text1.text <> "" then
    text2.enabled = true
    text2.setfocus
    else
    text2.enabled = false
    If text3.Enabled And text3.Visible then
    text3.SetFocus
    End If
    end if

    End Sub




    HTH,
    Duncan


    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: SetFocus problem

    I think using the LostFocus() event is better than using the Validate() event in this case.

    Validate() is sent when right after the LostFocus() of Text1 but right before GetFocus of Text3. Therefore when you change the focus to Text2, it actually goes there for few nanoseconds, but because of the GetFocus event is queued for Text3, then the focus goes back to Text3 instead of staying on Text2 (this is from what I understand anyway).

    Validate event is also meant when you want to validate the data of previous control (Text1) when it is about to enter the next control (Text2). If data of Text1 is invalid, then you'll set the Cancel = True which will bring back the focus to Text1 instead of going to Text3.

    -Cool Bizs

    Good Luck,
    -Cool Bizs

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

Featured