-
Dec 8th, 2011, 06:29 AM
#1
Thread Starter
Addicted Member
[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?
-
Dec 8th, 2011, 09:18 AM
#2
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.
-
Dec 8th, 2011, 10:33 AM
#3
Thread Starter
Addicted Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|