GotFocus event fires twice.

D

DocBrown

Can someone explain to me the following:

I have a Listbox on a form. I set the GotFocus property to [Event Procedure]
and then define a function. Do the same for LostFocus:

Private Sub cboSchoolNameSelect_LostFocus()
Listbox_Hght (False)
End Sub

Private Sub cboSchoolNameSelect_GotFocus()
Listbox_Hght (True)
End Sub

Public Sub Listbox_Hght(big As Boolean)
Dim myHeight As Long, myCount As Long
Dim myCurrCtrl As ListBox

Set myCurrCtrl = Me.ActiveControl

myHeight = myCurrCtrl.Height
myCount = myCurrCtrl.ListCount

If myCount > 5 Then
myCount = 5
End If
If myCount <= 0 Then
myCount = 1
End If
If big Then
myCurrCtrl.Height = myHeight * myCount
Else
myCurrCtrl.Height = myHeight / myCount
End If

'Debug.Print "Listbox_Hght: " & Str(myCount) & Str(myHeight)

End Sub

This function changes the height of the list box when it gets focus and
returns it to it's original height when focus is lost. I'm doing this because
I want the listbox height to be one line until the user clicks on it.

This works as expected. But then I change the GotFocus and LostFocus
properties to the following

GotFocus: =Listbox_Hght(True)
LostFocus: =Listbox_Hght(False)

This doesn't work because the function is called twice when I click in the
box and twice again with I click out of the box.

What's going on?? (Yes, I disabled the call in the *_GotFocus and
*_LostFocus calls as I was testing this.)

Thanks,
John S.
 
S

Stuart McCall

DocBrown said:
Can someone explain to me the following:

I have a Listbox on a form. I set the GotFocus property to [Event
Procedure]
and then define a function. Do the same for LostFocus:

Private Sub cboSchoolNameSelect_LostFocus()
Listbox_Hght (False)
End Sub

Private Sub cboSchoolNameSelect_GotFocus()
Listbox_Hght (True)
End Sub

Public Sub Listbox_Hght(big As Boolean)
Dim myHeight As Long, myCount As Long
Dim myCurrCtrl As ListBox

Set myCurrCtrl = Me.ActiveControl

myHeight = myCurrCtrl.Height
myCount = myCurrCtrl.ListCount

If myCount > 5 Then
myCount = 5
End If
If myCount <= 0 Then
myCount = 1
End If
If big Then
myCurrCtrl.Height = myHeight * myCount
Else
myCurrCtrl.Height = myHeight / myCount
End If

'Debug.Print "Listbox_Hght: " & Str(myCount) & Str(myHeight)

End Sub

This function changes the height of the list box when it gets focus and
returns it to it's original height when focus is lost. I'm doing this
because
I want the listbox height to be one line until the user clicks on it.

This works as expected. But then I change the GotFocus and LostFocus
properties to the following

GotFocus: =Listbox_Hght(True)
LostFocus: =Listbox_Hght(False)

This doesn't work because the function is called twice when I click in the
box and twice again with I click out of the box.

What's going on?? (Yes, I disabled the call in the *_GotFocus and
*_LostFocus calls as I was testing this.)

Thanks,
John S.

I don't have an explanation, sorry. I just wanted to ask why are you
emulating a combo box instead of using one? Is it because you don't want the
user to type, only select? If that's the case you can arrange for a combo to
do this by setting KeyAscii to zero in the combo's Keypress event. Is it
because the user has to click the down pointer to open the list? If so,
putting:

Me.ComboName.DropDown

in it's GotFocus event will drop the list automatically.

But if multiple selection is what you're after, you'll have to get your
technique to work...
 
D

DocBrown

I'm using this as a multi-select listbox and combo boxes don't allow
multi-select.

As you see, I do have it working as long as I call the function via the
'official' [Event Procedure]. I have several ListBoxes I want to do this for
and I'm hoping to elliminate all the almost redundent Event Procedures in the
VBA code.

John

Stuart McCall said:
DocBrown said:
Can someone explain to me the following:

I have a Listbox on a form. I set the GotFocus property to [Event
Procedure]
and then define a function. Do the same for LostFocus:

Private Sub cboSchoolNameSelect_LostFocus()
Listbox_Hght (False)
End Sub

Private Sub cboSchoolNameSelect_GotFocus()
Listbox_Hght (True)
End Sub

Public Sub Listbox_Hght(big As Boolean)
Dim myHeight As Long, myCount As Long
Dim myCurrCtrl As ListBox

Set myCurrCtrl = Me.ActiveControl

myHeight = myCurrCtrl.Height
myCount = myCurrCtrl.ListCount

If myCount > 5 Then
myCount = 5
End If
If myCount <= 0 Then
myCount = 1
End If
If big Then
myCurrCtrl.Height = myHeight * myCount
Else
myCurrCtrl.Height = myHeight / myCount
End If

'Debug.Print "Listbox_Hght: " & Str(myCount) & Str(myHeight)

End Sub

This function changes the height of the list box when it gets focus and
returns it to it's original height when focus is lost. I'm doing this
because
I want the listbox height to be one line until the user clicks on it.

This works as expected. But then I change the GotFocus and LostFocus
properties to the following

GotFocus: =Listbox_Hght(True)
LostFocus: =Listbox_Hght(False)

This doesn't work because the function is called twice when I click in the
box and twice again with I click out of the box.

What's going on?? (Yes, I disabled the call in the *_GotFocus and
*_LostFocus calls as I was testing this.)

Thanks,
John S.

I don't have an explanation, sorry. I just wanted to ask why are you
emulating a combo box instead of using one? Is it because you don't want the
user to type, only select? If that's the case you can arrange for a combo to
do this by setting KeyAscii to zero in the combo's Keypress event. Is it
because the user has to click the down pointer to open the list? If so,
putting:

Me.ComboName.DropDown

in it's GotFocus event will drop the list automatically.

But if multiple selection is what you're after, you'll have to get your
technique to work...
 
D

Dirk Goldgar

DocBrown said:
Can someone explain to me the following:

I have a Listbox on a form. I set the GotFocus property to [Event
Procedure]
and then define a function. Do the same for LostFocus:

Private Sub cboSchoolNameSelect_LostFocus()
Listbox_Hght (False)
End Sub

Private Sub cboSchoolNameSelect_GotFocus()
Listbox_Hght (True)
End Sub

Public Sub Listbox_Hght(big As Boolean)
Dim myHeight As Long, myCount As Long
Dim myCurrCtrl As ListBox

Set myCurrCtrl = Me.ActiveControl

myHeight = myCurrCtrl.Height
myCount = myCurrCtrl.ListCount

If myCount > 5 Then
myCount = 5
End If
If myCount <= 0 Then
myCount = 1
End If
If big Then
myCurrCtrl.Height = myHeight * myCount
Else
myCurrCtrl.Height = myHeight / myCount
End If

'Debug.Print "Listbox_Hght: " & Str(myCount) & Str(myHeight)

End Sub

This function changes the height of the list box when it gets focus and
returns it to it's original height when focus is lost. I'm doing this
because
I want the listbox height to be one line until the user clicks on it.

This works as expected. But then I change the GotFocus and LostFocus
properties to the following

GotFocus: =Listbox_Hght(True)
LostFocus: =Listbox_Hght(False)

This doesn't work because the function is called twice when I click in the
box and twice again with I click out of the box.

What's going on?? (Yes, I disabled the call in the *_GotFocus and
*_LostFocus calls as I was testing this.)

Thanks,
John S.


I'm a bit surprised that that works at all, since you're only *supposed* to
be able to use Function expressions in the event properties, not calls to
Subs. I tested your code and saw the double-call effect you report. Then I
changed
Public Sub Listbox_Hght(big As Boolean)

to

Public Function Listbox_Hght(big As Boolean)

and the problem disappeared.
 
D

DocBrown

Yup, that WORKS.

I must have missed the point about functions vs subs in event properties.

Thanks a bunch
John

Dirk Goldgar said:
DocBrown said:
Can someone explain to me the following:

I have a Listbox on a form. I set the GotFocus property to [Event
Procedure]
and then define a function. Do the same for LostFocus:

Private Sub cboSchoolNameSelect_LostFocus()
Listbox_Hght (False)
End Sub

Private Sub cboSchoolNameSelect_GotFocus()
Listbox_Hght (True)
End Sub

Public Sub Listbox_Hght(big As Boolean)
Dim myHeight As Long, myCount As Long
Dim myCurrCtrl As ListBox

Set myCurrCtrl = Me.ActiveControl

myHeight = myCurrCtrl.Height
myCount = myCurrCtrl.ListCount

If myCount > 5 Then
myCount = 5
End If
If myCount <= 0 Then
myCount = 1
End If
If big Then
myCurrCtrl.Height = myHeight * myCount
Else
myCurrCtrl.Height = myHeight / myCount
End If

'Debug.Print "Listbox_Hght: " & Str(myCount) & Str(myHeight)

End Sub

This function changes the height of the list box when it gets focus and
returns it to it's original height when focus is lost. I'm doing this
because
I want the listbox height to be one line until the user clicks on it.

This works as expected. But then I change the GotFocus and LostFocus
properties to the following

GotFocus: =Listbox_Hght(True)
LostFocus: =Listbox_Hght(False)

This doesn't work because the function is called twice when I click in the
box and twice again with I click out of the box.

What's going on?? (Yes, I disabled the call in the *_GotFocus and
*_LostFocus calls as I was testing this.)

Thanks,
John S.


I'm a bit surprised that that works at all, since you're only *supposed* to
be able to use Function expressions in the event properties, not calls to
Subs. I tested your code and saw the double-call effect you report. Then I
changed
Public Sub Listbox_Hght(big As Boolean)

to

Public Function Listbox_Hght(big As Boolean)

and the problem disappeared.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
S

Stuart McCall

Dirk Goldgar said:
DocBrown said:
Can someone explain to me the following:

I have a Listbox on a form. I set the GotFocus property to [Event
Procedure]
and then define a function. Do the same for LostFocus:

Private Sub cboSchoolNameSelect_LostFocus()
Listbox_Hght (False)
End Sub

Private Sub cboSchoolNameSelect_GotFocus()
Listbox_Hght (True)
End Sub

Public Sub Listbox_Hght(big As Boolean)
Dim myHeight As Long, myCount As Long
Dim myCurrCtrl As ListBox

Set myCurrCtrl = Me.ActiveControl

myHeight = myCurrCtrl.Height
myCount = myCurrCtrl.ListCount

If myCount > 5 Then
myCount = 5
End If
If myCount <= 0 Then
myCount = 1
End If
If big Then
myCurrCtrl.Height = myHeight * myCount
Else
myCurrCtrl.Height = myHeight / myCount
End If

'Debug.Print "Listbox_Hght: " & Str(myCount) & Str(myHeight)

End Sub

This function changes the height of the list box when it gets focus and
returns it to it's original height when focus is lost. I'm doing this
because
I want the listbox height to be one line until the user clicks on it.

This works as expected. But then I change the GotFocus and LostFocus
properties to the following

GotFocus: =Listbox_Hght(True)
LostFocus: =Listbox_Hght(False)

This doesn't work because the function is called twice when I click in
the
box and twice again with I click out of the box.

What's going on?? (Yes, I disabled the call in the *_GotFocus and
*_LostFocus calls as I was testing this.)

Thanks,
John S.


I'm a bit surprised that that works at all, since you're only *supposed*
to be able to use Function expressions in the event properties, not calls
to Subs. I tested your code and saw the double-call effect you report.
Then I changed
Public Sub Listbox_Hght(big As Boolean)

to

Public Function Listbox_Hght(big As Boolean)

and the problem disappeared.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Nice catch, Dirk. Because the OP mentioned that it was working (allbeit too
well!), I never even looked at the declaration line.

So if it's a public sub and you try to use it as an expression, it is called
twice. Peculiar. And not one I've run across before. Worth remembering..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top