Results 1 to 5 of 5

Thread: [RESOLVED] Odd behaviour when attempting to activate a form.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    4

    Resolved [RESOLVED] Odd behaviour when attempting to activate a form.

    Hello all,

    I have a program where the main form is called (shown/activated) via a keystroke combination. When the form pops-up, I've set the focus to a textbox to begin typing right away.

    When the keystroke event occurs, I have:

    Me.Activate()

    And in the Form_Activated event:

    Textbox1.Focus()


    Simple right? It works the way I want it to, even after I've compiled and run the EXE, but ONLY if Visual Studio 2008 is running. Once I close my project in VS, with the program still running outside VS, the form is shown, but input focus is not set to the textbox.

    Input focus is however still set to whatever it was previously, like a word document for example. My form will be topmost, but if I attempt to input text, it would still be entered into the word doc.

    Any ideas on why having my projects' source open in VS has anything to do with running the compiled EXE of said program?

    I've even tried API calls to SetForegroundWindow and ShowWindow with the same result.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Odd behaviour when attempting to activate a form.

    Try calling Select on the TextBox instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    4

    Re: Odd behaviour when attempting to activate a form.

    Tried that as well, same problem.

    It appears that if and when the textbox loses focus, its impossible to get it back by any of the means I've already tried, unless of course VS is running in the background for some odd reason.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Odd behaviour when attempting to activate a form.

    Works fine for me. Try this and tell me what happens:

    1. Create a new WinForms project.
    2. Add a TextBox to the form.
    3. Add a Button to the form.
    4. Add this code:
    Code:
    Private Sub Form1_Activated(ByVal sender As Object, _
                                ByVal e As System.EventArgs) Handles Me.Activated
        Me.TextBox1.Select()
    End Sub
    5. Change the build configuration on the tool bar from Debug to Release.
    6. Select Build -> Build Solution from the main menu.
    7. Click the Save All button on the tool bar and save the solution.
    8. Close VS.
    9. Navigate to your project folder and run the EXE from the bin\Release folder. NB: NOT the vshost.exe file.
    10. Click the Button.
    11. Activate some other application.
    12. Activate your form.

    Does the TextBox receive focus? It does for me.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    4

    Re: Odd behaviour when attempting to activate a form.

    jmcilhinney, you are a lifesaver.

    I tried your little app to test the focus issues, and got the same result you did (i.e. the expected result). From there I started re-creating my code from your test app, by implementing the (makeshift) keyboard hook I had thrown together for my app, and found the problem occurred when my key combination was pressed after the app had lost focus (so it appears the problem was somewhere in my keyboardhook).

    So I did a bit of searching and decided to make use of the API call for RegisterHotKey instead, and it seems infallible so far, and since I changed it to this, my code is cleaner, simpler, and the program itself uses a smaller memory footprint.

    Just wanted to thank jmcilhinney for reminding me to keep things simple, and that solving problems with a simple test app is by no means 'backtracking'.

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