Message 1 of 9
ListView - How to select first item on form load

Not applicable
11-01-2007
07:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
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