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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hiding Control on Lost Focus 3

Status
Not open for further replies.

Hmadyson

Programmer
Mar 14, 2001
202
US
I would like to hide a control when it loses focus. I programmed

Private Sub lstMenu1_LostFocus()
lstMenu1.Visible = False
End Sub

Unfortunately, on Lost Focus, that control still has the focus, and will not make it visible until it has lost focus.

Is there any way to find out which control the user was moving to, and then do <NewField>.SetFocus, before running the visible line? I don't want to give focus to the next field in tab order because they may not have gone there and there would be a disconnect.

I also have tons of controls, and I don't want to put code in every control's GetFocus to check if that control is visible and then hide it.

Any help? Thanks.
 
Im not so sure i understand what you are saying, can you try explaining yourself a little better. What i think you want to do is whenever a control loses the focus, you want it to become invisible, right? Then you want to set the focus onto another control... Am I following you so far?

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
Ok so on the lost focus event procedure of whatever control it is that is losing the focus, type something like this:


private sub controlname_lost_focus()
controlname.setfocus = true
end sub

Setting the focus of whatever the other control is that you want to have the focus.... that should do it right?

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
no
I am asking how to figure out what the next field that the person put the focus on is called. I don't think it is easy, you must not understand my question.
 
No, i dont believe i fully understand. But i might understand a little better now... What your asking is 'how do i figure out what control has the user selected' Right?

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
Yes, I either need to know how to figure out which control has the focus before the LostFocus event of the previous control is run, or I need to know how to run an event for a control after the control has lost the focus, because I need to hide the control and I cannot hide it if it has the focus.
 
Cant you just send them to a specific control? That way you know for sure which control they are going to and you wont have to have a ton of extra code in there.

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
that would be cludgey that they choose a field, then all of a sudden they are moved to another field just because I wanted to hide one of the fields. That is just not user friendly.
 
ok.... how bout if you tried this. I tried it in a form of mine, and it works, so try this. Whenever i say controlname, thats where your control name should be each controlname should be different, for each control you have. Put this in the 'lost focus' event procedure of the control that you want them to move from.

If controlname = gotfocus then
perform actions
Else if controlname = gotfocus then
perform actions
Else if controlname = gotfocus then
perform actions
and so forth......


do this for each control you have, that way it checks to see which control has the focus, and runs whatever you need done. let me know what happens...

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
in the Get Focus Event of your next control place

Private Sub LastName_GotFocus()
Screen.PreviousControl.Visible = False
End Sub

But.. this works in both directions... you could end up with only one control visible, and it may or may not be the one you want. Better to code it to check for direction, this code uses the TabIndex value to ensure that only one control is set to visible, the one with a index # 1 less than it has.


Private Sub LastName_GotFocus()
If Me.ActiveControl.TabIndex = Screen.PreviousControl.TabIndex + 1 Then
Screen.PreviousControl.Visible = False
End If
End Sub


But you need to add this to all controls that you want the previous control to be hidden.

PaulF
 
Maybe I read you wrong, but I did this

Private Sub lstMenu1_LostFocus()
Dim ctl As Control

For Each ctl In Me.Controls
If ctl.ControlType <> acLabel Then
If ctl.TabStop Then
If ctl.GotFocus Then
ctl.SetFocus
Exit For
End If
End If
End If
Next
lstMenu1.Visible = False

Set ctl = Nothing
End Sub
But I am getting an error that there is no GotFocus property. &quot;Object doesn't support this property or method.&quot;

Am I reading you wrong, I know that you wanted me to do a lot of if statements, but cycling through the controls seemed easier.

I am surprised that the GotFocus Property is working for you.
 
Paul, I bet that would work, but I have way too many controls to do it, plus there are only 8 controls that I want to hide when they lose focus.

I give up!!!!!!!!!!!!!!!!
 
lol. Don't give up. Never give up. We can get through this. Try 'if ctl = gotfocus then
ctl.setfocus
endif'
maybe that will work.

One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
okay, lets try it this way, it might be easier. Create a Form Level String Variable (strCtrl in this example), then add these two Functions to the Form's Code Window. Then for the 8 controls you want to set the Visible property to False, enter = fLostFocus(&quot;TheControlName&quot;) in the LostFocus Event.
For the rest of the Controls that can get the focus select them all by holding down the Shift key and clicking on the Controls. Once you've selected them enter =fSetVisibleToFalse() in the GotFocus Event.

Dim strCtl As String

Function fSetVisibleToFalse()
If strCtl <> &quot;&quot; Then
Me(strCtl).Visible = False
strCtl = &quot;&quot;
End If
End Function

Function fLostFocus(strIn As String)
strCtl = strIn
End Function
 
Paul Thanks so much, that did it. Still lots more to do, but you definitely helped me out immensely.

Rmck87 thanks for helping me out and giving me the encouragement to try more things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top