CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2001
    Posts
    7

    SetFocus not working

    I'm attempting to reset the focus back to a text box after I reset a form. I use the command:
    txtItem.SetFocus and it generates Runtime error 5, invalid procedure call or argument. Why?

    ~~~~~~~~~~~~
    James Fowler

  2. #2
    Join Date
    Aug 2000
    Location
    Ottawa, Canada
    Posts
    469

    Re: SetFocus not working

    You should do it in Form_Activate but not in Form_Load.


  3. #3
    Join Date
    Sep 2001
    Posts
    7

    Re: SetFocus not working

    Okay, but it will be called several times during a form's life. How can I get it to work every time? I'm including it in a subroutine that is called in many places to reset the form.

    ~~~~~~~~~~~~
    James Fowler

  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: SetFocus not working

    As long as the Form is loaded and the control you are trying to setfocus to is enabled it should work with no problems.
    If you Unload the Form or disable the control then you will get the error you mentioned.
    You mentioned it as being done from a subroutine. If the Subroutine is not part of the From then include the Form name in front of the setfocus like this

    Form1.Text1.SetFocus




    John G

  5. #5
    Join Date
    Sep 2001
    Posts
    7

    Re: SetFocus not working

    The subroutine is part of the same form that the text field is in. The text field is never disabled.

    ~~~~~~~~~~~~
    James Fowler

  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: SetFocus not working

    Is the form visible at the time of the .SetFocus? If not, that error will occur.

    John G

  7. #7
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: SetFocus not working

    send the relevent code

    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  8. #8
    Join Date
    Sep 2001
    Posts
    16

    Re: SetFocus not working

    Hi,
    I think u have written the code in the form load event. At the time of loading the form in to the memory it cant give the focus directly to a control. Bcos without the form gets loaded the control wont be loaded.
    It is better u give the code text1.setfocus in the form_activate event.
    have a nice time
    sunil.


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

    Re: SetFocus not working


    public Sub SafeSetFocus(byval ctlIn as Control)

    If ctlIn.Visible And ctlIn.Enabled then
    ctlIn.SetFocus
    End If

    End Sub




    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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