ListView - How to select first item on form load

ListView - How to select first item on form load

Anonymous
Not applicable
5,404 Views
8 Replies
Message 1 of 9

ListView - How to select first item on form load

Anonymous
Not applicable
Hi all,
vba user form with listView1
How to get listview to have focus when form appears,
Such that first item is selected/highlighted and a scroll arrow would
immediately scroll down
.SetFocus doesn't do it,
.ListItem(0).Selected = True raises out of bounds error(why?)
.ListItem(1).Selected = True creates first two entries with grey background
but not selected and scroll arrow is not active.
I have to use mouse to select an item, then scroll arrow will scroll from
there.

In bas module, to call form
Sub Main()
Dim f As frmPtsgLayerSelectvba
Set f = New frmPtsgLayerSelectvba
'set sFname to location of file
f.Init sFname
f.Show
End Sub

In the form module
Public Sub Init(sLayerDataFileName As String)
msLayerDataFileName = sLayerDataFileName

'this loads list view with items
Call LoadListViewWithLayerDataFromFile(Me.ListView1, msLayerDataFileName)


'this *should* put the focus inside listview
ListView1.SetFocus
'this *should* select first item...but instead raises out of bounds error
'ListView1.ListItems.Item(0).Selected = True

'this *half-way* selects first and second item(grey background, not
blue"selected" background)
ListView1.ListItems.Item(1).Selected = True

'but I still have to use mouse to get focus inside listview so scroll (with
keyboard) will work

End Sub

Is there a trick to this?
Thanks
Mark
0 Likes
5,405 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
".ListItem(0).Selected = True raises out of bounds error(why?)"
The syntax is wrong and would be trying to reference #0 dimension of an array, which since it is not an array, it is out of bounds.

Private Sub UserForm_Initialize()
ListBox1.AddItem "test0", 0
ListBox1.AddItem "test1", 1
ListBox1.AddItem "test2", 2
Me.ListBox1.SetFocus
Me.ListBox1.Selected(1) = True
End Sub
0 Likes
Message 3 of 9

Anonymous
Not applicable
Hi rstrandmark,
Thanks for the response.
However, per the subject line, I'm working with ListView not ListBox.
Sorry for not making that more clear.

ListView contains ListItems whose members are accessed via index similar to
array or collection.
in this case it appears index is 1 based like collection.
So I'm not understanding why .Item(1).Selected = True "highlights" (sort of)
the second item in addition to the first
and why it's only greyed, not selected...

any other ideas?
...anyone?....

Thanks
mark




wrote in message news:5766899@discussion.autodesk.com...
".ListItem(0).Selected = True raises out of bounds error(why?)"
The syntax is wrong and would be trying to reference #0 dimension of an
array, which since it is not an array, it is out of bounds.

Private Sub UserForm_Initialize()
ListBox1.AddItem "test0", 0
ListBox1.AddItem "test1", 1
ListBox1.AddItem "test2", 2
Me.ListBox1.SetFocus
Me.ListBox1.Selected(1) = True
End Sub
0 Likes
Message 4 of 9

Anonymous
Not applicable
Sorry I am not too familiar with listview control. Yes it does appear to be "1" based. If the listview is given focus, then the item selection is greyed out, if there is no focus given to the control then the item selection is highlighted correctly. I tested this on a simple userform that only has the listview control on it, so I am unsure if it behaves different with other controls on the form.

Private Sub UserForm_Initialize()
Me.ListView1.ListItems.Add 1, "keyA", "test0"
Me.ListView1.ListItems.Add 2, "keyB", "test1"
Me.ListView1.ListItems.Add 3, "keyC", "test2"
Me.ListView1.HideSelection = False
Me.ListView1.ListItems.Item("keyB").Selected = True
'or~ Me.ListView1.ListItems.Item(2).Selected = True
End Sub
0 Likes
Message 5 of 9

Anonymous
Not applicable
wrote in message news:5767008@discussion.autodesk.com...
Sorry I am not too familiar with listview control. Yes it does appear to be
"1" based. If the listview is given focus, then the item selection is greyed
out, if there is no focus given to the control then the item selection is
highlighted correctly.

Well thats *NON* intuitive !!!
if you want the control to have focus make sure NOT to set focus!!
:-)

what's weird is if I set .Item(1).Selected= True, Items(1 and 2) are
actually selected

the solution as weird as it seems to me is:

Public Sub Init(sLayerDataFileName As String)
msLayerDataFileName = sLayerDataFileName
Call LoadListViewWithLayerDataFromFile(Me.ListView1, msLayerDataFileName)
Me.ListView1.TabIndex = 0
Me.cmdCreate.TabIndex = 1
Me.cmdClose.TabIndex = 2

'can't set focus to listview even though we want the focus set to
listview!!!
'otherwise selected item below doesn't function correctly!!!
'ListView1.SetFocus
'Thanks rstrandmark for figuring that one out!!! 🙂

'this selects items 1 *AND* 2 !!!???!!!
ListView1.ListItems.Item(1).Selected = True

'so now we have to uncheck item 2 (which we never checked...but oh well)
ListView1.ListItems.Item(2).Selected = False

End Sub
0 Likes
Message 6 of 9

Anonymous
Not applicable
ListView's ListItems is 0 base collection. You need to pay attention to two
things here:

1. When ListItem is added to ListView's ListItems collection, the first
Item's selected property is set to True, by default, and other Item's
Selected is set to False, unless you set it in your code.

2. ListView has a property called "HideSelection", which default to "True".
In this case, if ListView does not have focus, the selected item does not
get highlighted. and only show highlighted (in blue) when the listview has
the focus. When HideSelection is set to Flase, then selected item(s) will be
highlighted differently: if ListView has focus, highlighted regularly, if
listview does not have focuse, selected item(s) highlighted in grey.

See more comment inline below



"MP" wrote in message
news:5767396@discussion.autodesk.com...
wrote in message news:5767008@discussion.autodesk.com...
Sorry I am not too familiar with listview control. Yes it does appear to be
"1" based. If the listview is given focus, then the item selection is greyed
out, if there is no focus given to the control then the item selection is
highlighted correctly.

Well thats *NON* intuitive !!!
if you want the control to have focus make sure NOT to set focus!!
:-)

what's weird is if I set .Item(1).Selected= True, Items(1 and 2) are
actually selected

the solution as weird as it seems to me is:

Public Sub Init(sLayerDataFileName As String)
msLayerDataFileName = sLayerDataFileName
Call LoadListViewWithLayerDataFromFile(Me.ListView1, msLayerDataFileName)
Me.ListView1.TabIndex = 0
Me.cmdCreate.TabIndex = 1
Me.cmdClose.TabIndex = 2

'can't set focus to listview even though we want the focus set to
listview!!!
'otherwise selected item below doesn't function correctly!!!
'ListView1.SetFocus
'Thanks rstrandmark for figuring that one out!!! 🙂


Why you cannot set focus??


'this selects items 1 *AND* 2 !!!???!!!
ListView1.ListItems.Item(1).Selected = True


Yes, this set secont item to selected. Since the first item's Selected is
True by default, and the listView's MultiSelect is True by default, so the
first two items are selected.

Now, if the ListView has focus the first 2 items are hilighted, if not
having focus, the twoitems either not highlighted, or highlighted in gray,
depending on "HideSelection" property's value.



'so now we have to uncheck item 2 (which we never checked...but oh well)
ListView1.ListItems.Item(2).Selected = False

End Sub
0 Likes
Message 7 of 9

Anonymous
Not applicable
Hi Norman,
Thanks for the explanation! Good info to know...did you find this in help or
you just Know it?
I actually couldn't find any help in vba for listview topic..
:-)

Mark

"Norman Yuan" wrote in message
news:5768263@discussion.autodesk.com...
ListView's ListItems is 0 base collection. You need to pay attention to two
things here:

1. When ListItem is added to ListView's ListItems collection, the first
Item's selected property is set to True, by default, and other Item's
Selected is set to False, unless you set it in your code.

2. ListView has a property called "HideSelection", which default to "True".
In this case, if ListView does not have focus, the selected item does not
get highlighted. and only show highlighted (in blue) when the listview has
the focus. When HideSelection is set to Flase, then selected item(s) will be
highlighted differently: if ListView has focus, highlighted regularly, if
listview does not have focuse, selected item(s) highlighted in grey.

See more comment inline below



"MP" wrote in message
news:5767396@discussion.autodesk.com...
wrote in message news:5767008@discussion.autodesk.com...
Sorry I am not too familiar with listview control. Yes it does appear to be
"1" based. If the listview is given focus, then the item selection is greyed
out, if there is no focus given to the control then the item selection is
highlighted correctly.

Well thats *NON* intuitive !!!
if you want the control to have focus make sure NOT to set focus!!
:-)

what's weird is if I set .Item(1).Selected= True, Items(1 and 2) are
actually selected

the solution as weird as it seems to me is:

Public Sub Init(sLayerDataFileName As String)
msLayerDataFileName = sLayerDataFileName
Call LoadListViewWithLayerDataFromFile(Me.ListView1, msLayerDataFileName)
Me.ListView1.TabIndex = 0
Me.cmdCreate.TabIndex = 1
Me.cmdClose.TabIndex = 2

'can't set focus to listview even though we want the focus set to
listview!!!
'otherwise selected item below doesn't function correctly!!!
'ListView1.SetFocus
'Thanks rstrandmark for figuring that one out!!! 🙂


Why you cannot set focus??


'this selects items 1 *AND* 2 !!!???!!!
ListView1.ListItems.Item(1).Selected = True


Yes, this set secont item to selected. Since the first item's Selected is
True by default, and the listView's MultiSelect is True by default, so the
first two items are selected.

Now, if the ListView has focus the first 2 items are hilighted, if not
having focus, the twoitems either not highlighted, or highlighted in gray,
depending on "HideSelection" property's value.



'so now we have to uncheck item 2 (which we never checked...but oh well)
ListView1.ListItems.Item(2).Selected = False

End Sub
0 Likes
Message 8 of 9

Anonymous
Not applicable
I have been used ListView in VB/VBA a lot.

"MP" wrote in message
news:5769431@discussion.autodesk.com...
Hi Norman,
Thanks for the explanation! Good info to know...did you find this in help or
you just Know it?
I actually couldn't find any help in vba for listview topic..
:-)

Mark

"Norman Yuan" wrote in message
news:5768263@discussion.autodesk.com...
ListView's ListItems is 0 base collection. You need to pay attention to two
things here:

1. When ListItem is added to ListView's ListItems collection, the first
Item's selected property is set to True, by default, and other Item's
Selected is set to False, unless you set it in your code.

2. ListView has a property called "HideSelection", which default to "True".
In this case, if ListView does not have focus, the selected item does not
get highlighted. and only show highlighted (in blue) when the listview has
the focus. When HideSelection is set to Flase, then selected item(s) will be
highlighted differently: if ListView has focus, highlighted regularly, if
listview does not have focuse, selected item(s) highlighted in grey.

See more comment inline below



"MP" wrote in message
news:5767396@discussion.autodesk.com...
wrote in message news:5767008@discussion.autodesk.com...
Sorry I am not too familiar with listview control. Yes it does appear to be
"1" based. If the listview is given focus, then the item selection is greyed
out, if there is no focus given to the control then the item selection is
highlighted correctly.

Well thats *NON* intuitive !!!
if you want the control to have focus make sure NOT to set focus!!
:-)

what's weird is if I set .Item(1).Selected= True, Items(1 and 2) are
actually selected

the solution as weird as it seems to me is:

Public Sub Init(sLayerDataFileName As String)
msLayerDataFileName = sLayerDataFileName
Call LoadListViewWithLayerDataFromFile(Me.ListView1, msLayerDataFileName)
Me.ListView1.TabIndex = 0
Me.cmdCreate.TabIndex = 1
Me.cmdClose.TabIndex = 2

'can't set focus to listview even though we want the focus set to
listview!!!
'otherwise selected item below doesn't function correctly!!!
'ListView1.SetFocus
'Thanks rstrandmark for figuring that one out!!! 🙂


Why you cannot set focus??


'this selects items 1 *AND* 2 !!!???!!!
ListView1.ListItems.Item(1).Selected = True


Yes, this set secont item to selected. Since the first item's Selected is
True by default, and the listView's MultiSelect is True by default, so the
first two items are selected.

Now, if the ListView has focus the first 2 items are hilighted, if not
having focus, the twoitems either not highlighted, or highlighted in gray,
depending on "HideSelection" property's value.



'so now we have to uncheck item 2 (which we never checked...but oh well)
ListView1.ListItems.Item(2).Selected = False

End Sub
0 Likes
Message 9 of 9

Anonymous
Not applicable
Thanks for the info Norm,
However, it still doesn't work as advertised.
:-)

I think it has to do with columnheaders maybe??? or lvwReport view????

Seems I heard somewhere about columnHeaders changing list items somehow...?
Have you seen anything like that affecting selection of listitems or
lbound/ubound of .ListItems

what I'm experiencing is the following:
With .HideSelection = False and .SetFocus and no explicit setting of
selection,
default selection is first item greyed, not (blue highlighted), Focus is
not really on list view because arrow keys don't move selection and letter
keys don't move to "aplhabetic matched line"
(ie hit a and the first item that starts with a should become
highlighted...but it doesn't)

'Using this temp sub to check various options
'in Form
Public Sub SetSelection(bHideSel As Boolean, bSetFocus As Boolean)
Me.ListView1.HideSelection = bHideSel
If bSetFocus Then Me.ListView1.SetFocus
End Sub
'in sub main
f.Init sFname
' f.SetSelection True, False 'SECOND ITEM IS HIGHLIGHTED
' f.SetSelection False, False 'SECOND ITEM IS HIGHLIGHTED
' f.SetSelection True, True 'NO ITEMS ARE HIGHLIGHTED
f.SetSelection False, True 'FIRST ITEM IS GREYED
f.Show


acad 2008
.ListItems seems to be 1 based
'this works
Private Sub UserForm_Activate()
Dim lChek As Long, sRpt As String
For lChek = 1 To ListView1.ListItems.Count
If ListView1.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & vbCrLf
End If
Next lChek
LogEntry "Selected items: " & vbCrLf & sRpt
End Sub

'this raises out of bound error on this line
'>>> If ListView1.ListItems.Item(lChek).Selected = True Then
''when lChek = 0

Private Sub UserForm_Activate()
Dim lChek As Long, sRpt As String
For lChek = 0 To ListView1.ListItems.Count -1
If ListView1.ListItems.Item(lChek).Selected = True Then
sRpt = sRpt & "Item " & CStr(lChek) & vbCrLf
End If
Next lChek
LogEntry "Selected items: " & vbCrLf & sRpt
End Sub
0 Likes