list view question

D

Darin

See Code:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim oListView As New ListView
Dim oItem As ListViewItem
Dim cnt As Integer
Dim msg As String
Me.Controls.Add(oListView)
oListView.Dock = DockStyle.Fill
oListView.Columns.Add("Line #", 80, HorizontalAlignment.Right)
oListView.View = View.Details
For cnt = 1 To 100
msg = "LINE #: " & CStr(cnt)
oItem = oListView.Items.Add(msg, 0)
If cnt = 23 Then
oListView.Items(22).Selected = True
oListView.EnsureVisible(22)
End If
Next
End Sub

How come when I run the code, line 23 is selected (like I want), but if
I hit the down arrow, the selection moves to item 2 and NOT item 24. I
want it to move to item 24.

Can someone PLEASE help me.

Thanks.


Darin
 
C

Charles Law

Hi Darin

Include the following line as well:

oListView.Items(22).Focused = True

HTH

Charles
 
R

Richard Myers

If you checkout the selected indices property using your current code
you'll probably find that you have 2 items in the collection.
(Item 0 ) selected by default and item 22 selected by your code. When you
hit the down arrow the underlying UI nav for the list box
chooses the first item as the point of origin and then moves from it to the
next (item 2).

You'd be better off appending these items as an array and using BeginUpdte
& . EndUpdate (much faster and cleaner etc). But in just going with your
flow i'd rewrite like this an see what happens:

With oListView
For cnt = 1 To 100
msg = "LINE #: " & CStr(cnt)
oItem = .Items.Add(msg, 0)
Next

.SelectedItems.Clear
.Items(22).Selected = True
.EnsureVisible(22)
End With

Crap code but it should work.

Richard

I think you'd be better of using the selectedindex property.
 

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