SetFocus OnOpen to two listboxes?

B

bhammer

My form has two listboxes. when the form opens, I need one listbox to scroll
to the bottom. I can do this with SetFocus and SendKey "{END}". But I also
need the other listbox to be able to accept the Focus and highlight the first
item (in order to load an image box).

*******I tried . . .

Form_Open()

Me.lstLeft.SetFocus
SendKeys "{END}"
Me.lstRight.SetFocus
SendKeys "{DOWN}"

End Sub

*******And I tried . . .

Form_Open()

Me.lstLeft.SetFocus
SendKeys "{END}"

End Sub

Form_Load()

Me.lstRight.SetFocus
SendKeys "{DOWN}"

End Sub

I can't get both to hightlight upon the form's opening. Ideas?

-Brad
 
D

Dirk Goldgar

bhammer said:
My form has two listboxes. when the form opens, I need one listbox to
scroll
to the bottom. I can do this with SetFocus and SendKey "{END}". But I also
need the other listbox to be able to accept the Focus and highlight the
first
item (in order to load an image box).

*******I tried . . .

Form_Open()

Me.lstLeft.SetFocus
SendKeys "{END}"
Me.lstRight.SetFocus
SendKeys "{DOWN}"

End Sub

*******And I tried . . .

Form_Open()

Me.lstLeft.SetFocus
SendKeys "{END}"

End Sub

Form_Load()

Me.lstRight.SetFocus
SendKeys "{DOWN}"

End Sub

I can't get both to hightlight upon the form's opening. Ideas?


SendKeys is to be avoided wherever possible. As an alternative, have you
tried this in the form's Load event:

With Me.lstLeft
.SetFocus
.ListIndex = .ListCount - 1
End With

With Me.lstRight
.Value = .ItemData(Abs(.ColumnHeads))
.SetFocus
End With

I wasn't completely sure whether you intended to actually select the last
item in lstLeft, rather than just scroll to the bottom. The code above will
select the last item in the list box if the control is not in MultiSelect
mode. The code for lstRight will actively set the value of the list box,
but it won't work if that list box is in MultiSelect mode (since MultiSelect
list boxes have no Value). In that case, you need to set the ListIndex
property like this:

With Me.lstRight
.ListIndex = Abs(.ColumnHeads)
.SetFocus
End With
 
B

bhammer

Dirk Goldgar said:
SendKeys is to be avoided wherever possible. As an alternative, have you
tried this in the form's Load event:

With Me.lstLeft
.SetFocus
.ListIndex = .ListCount - 1
End With

With Me.lstRight
.Value = .ItemData(Abs(.ColumnHeads))
.SetFocus
End With

I wasn't completely sure whether you intended to actually select the last
item in lstLeft, rather than just scroll to the bottom. The code above will
select the last item in the list box if the control is not in MultiSelect
mode. The code for lstRight will actively set the value of the list box,
but it won't work if that list box is in MultiSelect mode (since MultiSelect
list boxes have no Value). In that case, you need to set the ListIndex
property like this:

With Me.lstRight
.ListIndex = Abs(.ColumnHeads)
.SetFocus
End With


Dirk,

Still having trouble. What is the issue with SendKeys, anyway?

On my form, lstLeft is in MultiSelect and lstRight is not. I need lstLeft to
highlight the first row, and lstRight to hightlight the last row.

I tried this:
Private Sub Form_Load()

With Me.lstLeft
.ListIndex = (Abs(.ColumnHeads))
.SetFocus
End With

With Me.lstRight
.Value = .ItemData(Abs(.ColumnHeads))
.SetFocus
End With

End Sub

I get and error 7777 You've used the ListIndex property incorrectly.

-Brad
 
K

Krzysztof Pozorek [MVP]

(...)
I can't get both to hightlight upon the form's opening. Ideas?


Set the default value.
1. For field lstLeft :
=lstLeft.ItemData(-lstLeft.ColumnHeads)

2. For field lstRight
=lstRight.ItemData(lstRight.ListCount-1)

K.P.
 
D

Dirk Goldgar

bhammer said:
Still having trouble. What is the issue with SendKeys, anyway?

It's has been buggy in the past (has been known to set NumLock), but more
importantly, you never actually know where the keystroke is going to go --
some other window might become active while your code is running.
On my form, lstLeft is in MultiSelect and lstRight is not. I need lstLeft
to
highlight the first row, and lstRight to hightlight the last row.

I tried this:
Private Sub Form_Load()

With Me.lstLeft
.ListIndex = (Abs(.ColumnHeads))
.SetFocus
End With

With Me.lstRight
.Value = .ItemData(Abs(.ColumnHeads))
.SetFocus
End With

End Sub

I get and error 7777 You've used the ListIndex property incorrectly.

You *must* set focus to the list box before setting the .ListIndex property.
In the code I posted, I did that. Why did you reverse the order of the
statements?
 
D

Dirk Goldgar

bhammer said:
On my form, lstLeft is in MultiSelect and lstRight is not. I need lstLeft
to
highlight the first row, and lstRight to hightlight the last row.

I just noticed, this is the reverse of what your originally posted. Here's
revised code:

With Me.lstLeft
.SetFocus
.ListIndex = Abs(.ColumnHeads)
End With

With Me.lstRight
.Value = .ItemData(.ListCount - 1)
.SetFocus
End With
 

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

Similar Threads


Top