Results 1 to 11 of 11

Thread: [RESOLVED] why doesnt form_losefocus work for me?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Resolved [RESOLVED] why doesnt form_losefocus work for me?

    I have this form, that responds to the up and down arrows..... i want it not to be able to be used when it is out of focus. i put this code...

    Code:
    Private Sub Form_GotFocus()
        keysallowed = True
        frmMainForm.Tag = 1
    End Sub
    Private Sub Form_LostFocus()
        keysallowed = False
        frmMainForm.Tag = 0
    End Sub
    but for some reason, when i click on another window, it still reponds! I even put it to where if the keysallwoed = true AND frmmainform.tag = 1 then respond to the keys.... why is it doing this?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: why doesnt form_losefocus work for me?

    Did you put a marker/break point on a line in LostFocus event? Are you sure the event is firing at all?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: why doesnt form_losefocus work for me?

    Okay.... Turns out, it isn't fireing at all..... =( I click on a different window, and it becomes gray......
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: why doesnt form_losefocus work for me?

    I think I needed something like you do earlier. I'm not sure but it was either using sublcassing or an API (maybe GetActiveWindow)... Try searching the forum for "form lost focus" or "window lost focus", I know I found a thread with a sample project attached, had a form that changed color and/or caption when it got or lost focus. Worked great.

    EDIT: I think the LostFocus event fires only when the form loses focus to another form withing your project/application. The code I mentioned above works globally. I'll try to dig it up.

    EDIT2: Found this, looks suspiciously like the code I mentioned: http://www.vbforums.com/showpost.php...3&postcount=10
    Last edited by baja_yu; Jun 8th, 2010 at 07:33 PM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: why doesnt form_losefocus work for me?

    But, do u know why it would be that it doesnt fire the LostFocus when i move to another window?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: why doesnt form_losefocus work for me?

    Quote Originally Posted by Gamemaster1494 View Post
    But, do u know why it would be that it doesnt fire the LostFocus when i move to another window?
    Yep. Read my Edit for the reason and Edit2 for the solution.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: why doesnt form_losefocus work for me?

    I think i found a post as well. http://www.vbforums.com/showthread.p...WM_ACTIVATEAPP =) thanks for ur help.
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [RESOLVED] why doesnt form_losefocus work for me?

    I prefer using the WM_NCACTIVATE message since its sent when the program starts up and will call my sub and set variables as needed. In the example baja_yu linked to it uses WM_ACTIVATEAPP, notice the caption doesn't change when the program is first started.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: [RESOLVED] why doesnt form_losefocus work for me?

    the one i found used the GotForegroundWindow thing.. so it has worked like i want it to. =)
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  10. #10
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] why doesnt form_losefocus work for me?

    The link you posted has the same code (by szlamany) that I posted, but you opted for the Timer, probably because it's less code. However the subclassing, even though it's more code, is a better and 'cleaner' solution than a timer.
    Last edited by baja_yu; Jun 8th, 2010 at 09:32 PM. Reason: typo

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Re: [RESOLVED] why doesnt form_losefocus work for me?

    Hmm....... Okay. i see what u mean. =)
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

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