Hi,
can't you use the ListIndex property, set it like:
ListView2.ListIndex = ListView2.Items.Count - 1
?
greetings
Main Topics
Browse All TopicsI am trying to select a listview item in code doing this:
ListView2.Items(ListView2.
ListView2.Items(ListView2.
as you can see, this would be the last row. I have FullRowSelect = True, and multiselect turned off.
In any event, this works unless I have selected a row with the mouse, then the above code does not seem to have any effect. I would like to know if there is a better way to do this, or if there is some work-around.
Many thanks
andrew200
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sample code....
'FORM 1
Public Class frmClickSubItem
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListView1 As System.Windows.Forms.ListV
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ColumnHeader1 As System.Windows.Forms.Colum
Friend WithEvents ColumnHeader2 As System.Windows.Forms.Colum
Friend WithEvents ColumnHeader3 As System.Windows.Forms.Colum
Friend WithEvents ColumnHeader4 As System.Windows.Forms.Colum
Friend WithEvents ColumnHeader5 As System.Windows.Forms.Colum
Friend WithEvents TextBox1 As System.Windows.Forms.TextB
<System.Diagnostics.Debugg
Me.ListView1 = New System.Windows.Forms.ListV
Me.ColumnHeader1 = New System.Windows.Forms.Colum
Me.ColumnHeader2 = New System.Windows.Forms.Colum
Me.ColumnHeader3 = New System.Windows.Forms.Colum
Me.ColumnHeader4 = New System.Windows.Forms.Colum
Me.ColumnHeader5 = New System.Windows.Forms.Colum
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextB
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Anchor = CType((((System.Windows.Fo
Or System.Windows.Forms.Ancho
Or System.Windows.Forms.Ancho
Me.ListView1.Columns.AddRa
Me.ListView1.FullRowSelect
Me.ListView1.GridLines = True
Me.ListView1.Location = New System.Drawing.Point(8, 8)
Me.ListView1.MultiSelect = False
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(458, 206)
Me.ListView1.TabIndex = 0
Me.ListView1.View = System.Windows.Forms.View.
'
'Label1
'
Me.Label1.Anchor = CType(((System.Windows.For
Or System.Windows.Forms.Ancho
Me.Label1.Location = New System.Drawing.Point(8, 222)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(190, 16)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(212, 220)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = "TextBox1"
'
'frmClickSubItem
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(478, 244)
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ListVie
Me.Name = "frmClickSubItem"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal lMsg As Integer, ByVal wParam As Integer, ByRef lParam As RECT) As Integer
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim lvi As ListViewItem
Dim ch As ColumnHeader
For i = 0 To 10
lvi = New ListViewItem("Item" & i)
lvi.SubItems.Add("Subitems
lvi.SubItems.Add("Subitems
lvi.SubItems.Add("Subitems
lvi.SubItems.Add("Subitems
ListView1.Items.Add(lvi)
Next
For Each ch In ListView1.Columns
ch.Width = -2
Next
End Sub
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
Label1.Text = "Column " & (GetListViewSubItem(Me.Lis
End Sub
Public Function GetListViewSubItem(ByVal listView1 As ListView, ByVal pt As Point) As Integer
Const LVM_FIRST As Integer = &H1000
Const LVM_GETSUBITEMRECT As Integer = LVM_FIRST + 56
Const LVIR_BOUNDS As Integer = 0
Dim myrect As RECT
Dim lvitem As ListViewItem = listView1.GetItemAt(pt.X, pt.Y)
If lvitem Is Nothing AndAlso listView1.SelectedItems.Co
lvitem = listView1.SelectedItems(0)
End If
Dim intLVSubItemIndex As Integer = -1
Dim LVSubItem As ListViewItem.ListViewSubIt
If Not (lvitem Is Nothing) Then
Dim intSendMessage As Integer
Dim i As Integer
lvitem.UseItemStyleForSubI
For i = 1 To lvitem.SubItems.Count - 1
LVSubItem = lvitem.SubItems(i)
myrect = New RECT
myrect.Top = i
myrect.Left = LVIR_BOUNDS
intSendMessage = SendMessage(listView1.Hand
If pt.X < myrect.Left Then
LVSubItem = lvitem.SubItems(0)
LVSubItem.ForeColor = Color.Red
intLVSubItemIndex = 0
TextBox1.Text = LVSubItem.Text
Exit For
ElseIf pt.X >= myrect.Left And pt.X <= myrect.Right Then
intLVSubItemIndex = i
LVSubItem.ForeColor = Color.Orange
TextBox1.Text = LVSubItem.Text
Exit For
Else
LVSubItem = Nothing
End If
Next i
End If
If LVSubItem Is Nothing OrElse lvitem Is Nothing Then
intLVSubItemIndex = -1
End If
Return intLVSubItemIndex
End Function
End Class
Business Accounts
Answer for Membership
by: FernandoSotoPosted on 2005-08-17 at 18:05:07ID: 14697082
Does it work if you place the statements in this order.
Items.Coun t - 1).Focused = True Items.Coun t - 1).Selected = True
ListView2.Items(ListView2.
ListView2.Items(ListView2.