Results 1 to 3 of 3

Thread: [RESOLVED] Picturebox Focus

  1. #1

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Resolved [RESOLVED] Picturebox Focus

    I've got question regarding total picturebox focus. What i would like to do is make it so that when a picturebox looses focus it's hidden. Now the first place i tried was in the "Lost Focus" event. There's one problem with that, there are five commandbuttons in the picturebox. That makes it so that when you click a button the picturebox looses focus and it's hidden. How can i find out when the picturebox and all of its controls don't have focus, easily?
    Hooked for good.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Picturebox Focus

    LostFocus for losing control occurs before GotFocus of gaining control. So, in this case, logically you'd have to test the GotFocus of all controls to see if the picturebox or its controls have focus.

    If you don't need this to be instantaneous, you can use a timer to help here.

    1) Add a timer
    2) When the picturebox or one of its buttons gets focus, start a timer
    3) When the timer activates, test the control with current focus

    :: From within your picturebox & its control's GotFocus events, call this:
    Code:
    Private Sub SetFocusTimer()
        If Timer1.Enabled = False Then 
              Timer1.Interval = 50 ' fast interval
              Timer1.Enabled = True
        End If
    End Sub
    :: Then your timer event looks like this
    Code:
    Private Sub Timer1_Timer()
       ' change Picture1 to your picbox name
       Timer1.Enabled = False
       If Me.ActiveControl Is Nothing Then Exit Sub
    
       If Me.ActiveControl Is Picture1 Then 
           Timer1.Enabled = True
       ElseIf Me.ActiveControl.Container Is Picture1 Then
           Timer1.Enabled = True
       Else
            Picture1.Visible = False
       End If
    End Sub
    If you have a lot of controls in your picturebox, you may want to simply allow the timer to run full time and modify the code appropriately. However, having a timer with a fast interval run full-time is not a good solution in my opinion.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Picturebox Focus

    Thanks lavolpe. I was wondering how to see which control has focus, didn't know about the "ActiveControl" property. This works perfectly
    Hooked for good.

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