Results 1 to 7 of 7

Thread: How to keep focus on textbox when opening a browser or another app from an action

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    3

    How to keep focus on textbox when opening a browser or another app from an action

    Hi.

    We have an app (in VB .net) where a user uses Dragon to enter text (voice to text) in a specific textbox. The problem is that we have a button that has the action to open a browser window and the user loses focus from the box.

    Is there a way to keep the focus on said textbox when opening the other app? So that the user continue using dictation without having to go back with a mouse.

    Thanks en advance!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: How to keep focus on textbox when opening a browser or another app from an action

    This is likely two-fold:
    1. The Focus in your application switches to the button
    2. Your application is no longer the active window


    You will likely need to use the SetForegroundWindow API (documentation) to make your application active after the browser opens.

    You can switch focus from the button to your textbox by calling the Select method (documentation).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    3

    Re: How to keep focus on textbox when opening a browser or another app from an action

    Thank you very much for the infor dday9! I'll be checking the documentation tonight

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: How to keep focus on textbox when opening a browser or another app from an action

    I started thinking about it and it seems as if you want your form to be the topmost form anyways. If that's the case, I would do one of two things depending on what you're trying to achieve:
    1. Set the Form's TopMost property to True (documentation)
    2. Setup a checkbox that the user can check/uncheck to toggle the TopMost property and set the checked state to true by default
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    3

    Re: How to keep focus on textbox when opening a browser or another app from an action

    "Setup a checkbox that the user can check/uncheck to toggle the TopMost property and set the checked state to true by default"

    This is what i was trying to do with another method but it was not working. I'll try with setting the topmost property of the form to true first.

    Thanks again!

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: How to keep focus on textbox when opening a browser or another app from an action

    Actually, I just tested it on my end. Setting the TopMost property alone won't work because even though the application is at the top of the z-order, it still isn't the active window.

    You'll still need to use the SetForegroundWindow API.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to keep focus on textbox when opening a browser or another app from an action

    Quote Originally Posted by dday9 View Post
    This is likely two-fold:
    1. The Focus in your application switches to the button
    2. Your application is no longer the active window


    You will likely need to use the SetForegroundWindow API (documentation) to make your application active after the browser opens.

    You can switch focus from the button to your textbox by calling the Select method (documentation).
    It may or may not be what you want but it is possible to create a custom Button control that doesn't take focus when you click it. That would solve that part of the problem at least. If you're interested, follow the CodeBank link in my signature below and check out my On-Screen Keyboard thread.

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