Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus of controls in picturebox 1

Status
Not open for further replies.

Disferente

Programmer
Jun 23, 2008
112
US
I have a several controls with a picturebox as container.
All of the controls and the picturebox has tabstop set to false.

When one of the controls have focus and I switch to another window and then back it is always the picturebox that has focus, is there any good way I can prevent that and have the control keep focus?
 
In Form2

Dim AC As Control

Private Sub Form_Activate()
If Not (AC Is Nothing) Then
AC.SetFocus
End If
End Sub

Private Sub Form_Deactivate()
Set AC = Me.ActiveControl
End Sub

The first time its Activated the first Control able to recieve Focus and having TabIndex=>0 has the focus.
 
Even just;

Private Sub Form_Deactivate()
ActiveControl.TabIndex = 0
End Sub

in Form2 may do it for you...
 
Does not seem to work for me. Neither activate nor decativate are called when I switch between the program and other windows.
The only time activate is called is when the form is loaded.
 
Sorry I had assumed multiple windows in the same app and see your problem now with other windows.

Can you consider giving tabstop=true to all controls except the containing picture and give the latter the heightest tabindex.
 

The reason the picture box recieves the focus is because of the way tabstop and tabindex work. (your picture box has the lowest index of all of your controls.) If you make the other controls tabstops=true then you will not have your problem. However, if for some reason you have the tabstops set to false for some aesthetic reason or some other functionality then you could set the tabstop=true for that control in its gotfocus event and then set it to false in its lostfocus event.

Good Luck
 
Putting tabstop=true on getfocus and tabstop=false on lostfocus works great. At first when I read it I was a bit skeptical because I thought that it would trap the pressing of tab.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top