Results 1 to 7 of 7

Thread: [RESOLVED] Setting Focus of a Textbox in an External application

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Resolved [RESOLVED] Setting Focus of a Textbox in an External application

    I have searched everywhere on multiple forums and can't find the answer to this tricky one.

    Currently I have used the SetForegroundWindow to bring the window to the front first and I have the hWnd of the textbox in the external application and I also have attached the thread input to the other application. It's also just the current "tab focus" that is all i needed to change, for example the window by default has a command button "tab focused" (highlighted) and I would like to change that to the textbox, either while the form is visible or not, but when the form gets focus I would like the focus to be in the textbox ready to type.

    So far i've tried: Using the SetFocus API, I've tried using SendMessage to send BM_CLICK (although of course that wouldn't work) and I'm stuck at this point, any ideas?

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Setting Focus of a Textbox in an External application

    Can you post the code you're using? Also, which OS are you testing your code with?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Setting Focus of a Textbox in an External application

    I'm using Windows 7 and here is my code below:

    hWndDirections is the textbox that I would like to set the focus to, and hWndParent is the Form that contains the textbox.

    Code:
    If IsWindowEnabled(hWndAltK) = 0 Then
            MsgBox "QuickS3 could not get the directions for this product." & vbCrLf & vbCrLf & "Please notify one of the dispensary staff that there is no sig for this product.", vbInformation, "QuickS3"
            SetForegroundWindow (hWndParent)
            PutFocus (hWndDirections) 'SetFocus API of directions textbox (which is a child of hWndParent)
            Exit Sub
        End If

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Setting Focus of a Textbox in an External application

    I thought you called AttachThreadInput?

    I tried a small test app (on XP) using only GetWindowThreadProcessId, AttachThreadInput and SetFocus. I was able to set focus to any window or control once I obtained their hWnd. Have you tried checking the return value of SetFocus and Err.LastDllError?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Setting Focus of a Textbox in an External application

    I got it working! Thanks Bonnie, I thought I did call AttachThreadInput and I did however I shared the two threads then as soon as I set the foreground window I set them to False so they no longer shared the same thread therefore when I ran the code above it didn't work. I wouldn't have looked back at my AttachThreadInput if it wasn't for you! Thanks so much for your quick replies!

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Setting Focus of a Textbox in an External application

    SetForegroundWindow doesn't seem to be needed for SetFocus to work...
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Setting Focus of a Textbox in an External application

    That's ok, that's actually a bonus! I still needed to set the foreground window anyway so that the user can start typing in the specified textbox after i alert them that my program couldn't find directions, but that's good to know! Thanks
    Last edited by koushi; Jan 16th, 2013 at 05:18 AM. Reason: Spelling

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