ListView Selected Item

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

I've been through just about every imaginable archive topic regarding
listviews, yet I can't get any code to work. I simply need to do two
things:

1) Set a particular row in the listview (there are three columns) to
the index I need based on a specific variable's value.

2) Obtain the value out of the first column that is currently
selected. I did not explicitly declare subitems, rather, I did it
like this when I populated my listview upon activation of the form:

lstServices.Items.Add(New ListViewItem(New String() {ticketNum,
ticketStop, ticketStreet}))

Can anyone help me with these two issues...my project seems to be at a
standstill while I wrestle with what seems to be a rather trivial bit
of coding.

Thanks much.
 
For #1. Are you looking to change the index of a specific row? If so, then
you might need to remove and then re-add the item in the proper location.
Dim indexToMove As Integer = 2
Dim indexToGoTo As Integer = 0
Dim item As ListViewItem = Me.ListView1.Items(indexToMove)
Me.ListView1.Items.RemoveAt(indexToMove)
Me.ListView1.Items.Insert(indexToGoTo, item)

For #2.
If (Me.ListView1.SelectedIndices.Count > 0) Then
Dim item As ListViewItem =
DirectCast(Me.ListView1.Items(Me.ListView1.SelectedIndices.Item(0)),
ListViewItem)
MessageBox.Show(item.Text)
End If
 
Actually, for number one, all I meant was set it to be highlighted or
selected based on a value I have in a global. So if column 1 value of
row 25 matched my global, I'd want that row selected.

Also, I need to be able to highlight the first row when showing the
form for the first time and setting the listview to have the focus is
not working.

Thanks for your help so far.
 
Actually, for number one, all I meant was set it to be highlighted or
selected based on a value I have in a global. So if column 1 value of
row 25 matched my global, I'd want that row selected
When you add a row that contains the global value, or edit a row to contain
the global value, you'll need to use the index and set the ListViewItem's
Selected property to True.
Me.ListView1.Focus()
Dim item As ListViewItem = Me.ListView1.Items(index)
item.Selected = True
Also, I need to be able to highlight the first row when showing the
form for the first time and setting the listview to have the focus is
not working.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ListView1.Focus()
If (Me.ListView1.Items.Count > 0) Then
Dim item As ListViewItem = Me.ListView1.Items(0)
item.Focused = True
item.Selected = True
End If
End Sub
 
Why is this:

Private Sub lstServices_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
lstServices.SelectedIndexChanged
If (Me.lstServices.SelectedIndices.Count > 0) Then
Dim item As ListViewItem =
DirectCast(Me.lstServices.Items(Me.lstServices.SelectedIndices.Item(0)),
ListViewItem)
G_TICKET = Convert.ToInt32(item.Text)
End If
End Sub

executing twice, no matter what I do? I'm populating my listview in
my activate event since it is always changing everytime the user needs
to view the form. Am I missing something obvious here?
 
The selectedindex will fire twice. Once with no selected item and once with
the newly selected item. Is that what you mean or is the code in the middle
of your method executing twice? If the latter, then show us the rest of your
form code (in a reproducible sample that I can run).

Cheers
Daniel
 
The selectedindex will fire twice. Once with no selected item and once with
the newly selected item. Is that what you mean or is the code in the middle
of your method executing twice? If the latter, then show us the rest of your
form code (in a reproducible sample that I can run).

It's in the middle that must be executing twice, actually sometimes
three or four times as well, all in a row. Each and every time I'm
getting the first column's text or the ID number in my case. It's not
reproducible, but here's my entire form:

Public Class frmSelectTicket
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
internalInstance = Nothing
End If
MyBase.Dispose(disposing)
End Sub

'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 mnuMain As System.Windows.Forms.MainMenu
Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
Friend WithEvents mnuAbout As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents lstServices As System.Windows.Forms.ListView
Friend WithEvents InputPanel1 As
Microsoft.WindowsCE.Forms.InputPanel
Friend WithEvents label1 As System.Windows.Forms.Label
Friend WithEvents hdrTicket As System.Windows.Forms.ColumnHeader
Friend WithEvents hdrStop As System.Windows.Forms.ColumnHeader
Friend WithEvents hdrAddress As System.Windows.Forms.ColumnHeader
Friend WithEvents btnComplete As System.Windows.Forms.Button
Friend WithEvents btnSelect As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.mnuMain = New System.Windows.Forms.MainMenu
Me.mnuFile = New System.Windows.Forms.MenuItem
Me.mnuAbout = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
Me.mnuExit = New System.Windows.Forms.MenuItem
Me.VScrollBar1 = New System.Windows.Forms.VScrollBar
Me.Panel1 = New System.Windows.Forms.Panel
Me.btnSelect = New System.Windows.Forms.Button
Me.btnComplete = New System.Windows.Forms.Button
Me.label1 = New System.Windows.Forms.Label
Me.lstServices = New System.Windows.Forms.ListView
Me.hdrTicket = New System.Windows.Forms.ColumnHeader
Me.hdrStop = New System.Windows.Forms.ColumnHeader
Me.hdrAddress = New System.Windows.Forms.ColumnHeader
Me.InputPanel1 = New Microsoft.WindowsCE.Forms.InputPanel
'
'mnuMain
'
Me.mnuMain.MenuItems.Add(Me.mnuFile)
'
'mnuFile
'
Me.mnuFile.MenuItems.Add(Me.mnuAbout)
Me.mnuFile.MenuItems.Add(Me.MenuItem3)
Me.mnuFile.MenuItems.Add(Me.mnuExit)
Me.mnuFile.Text = "&File"
'
'mnuAbout
'
Me.mnuAbout.Text = "&About Route Tracker"
'
'MenuItem3
'
Me.MenuItem3.Text = "-"
'
'mnuExit
'
Me.mnuExit.Text = "&Exit"
'
'VScrollBar1
'
Me.VScrollBar1.Location = New System.Drawing.Point(226, -1)
Me.VScrollBar1.Maximum = 91
Me.VScrollBar1.Size = New System.Drawing.Size(15, 272)
Me.VScrollBar1.Visible = False
'
'Panel1
'
Me.Panel1.Controls.Add(Me.btnSelect)
Me.Panel1.Controls.Add(Me.btnComplete)
Me.Panel1.Controls.Add(Me.label1)
Me.Panel1.Controls.Add(Me.lstServices)
Me.Panel1.Size = New System.Drawing.Size(225, 271)
'
'btnSelect
'
Me.btnSelect.Font = New System.Drawing.Font("Tahoma", 8.0!,
System.Drawing.FontStyle.Regular)
Me.btnSelect.Location = New System.Drawing.Point(8, 239)
Me.btnSelect.Size = New System.Drawing.Size(84, 22)
Me.btnSelect.Text = "&Select Ticket"
'
'btnComplete
'
Me.btnComplete.Font = New System.Drawing.Font("Tahoma", 8.0!,
System.Drawing.FontStyle.Regular)
Me.btnComplete.Location = New System.Drawing.Point(99, 239)
Me.btnComplete.Size = New System.Drawing.Size(100, 22)
Me.btnComplete.Text = "&Route Completed"
'
'label1
'
Me.label1.Font = New System.Drawing.Font("Tahoma", 9.75!,
System.Drawing.FontStyle.Bold)
Me.label1.ForeColor = System.Drawing.Color.Firebrick
Me.label1.Location = New System.Drawing.Point(7, 8)
Me.label1.Size = New System.Drawing.Size(212, 16)
Me.label1.Text = "Select Ticket"
'
'lstServices
'
Me.lstServices.Columns.Add(Me.hdrTicket)
Me.lstServices.Columns.Add(Me.hdrStop)
Me.lstServices.Columns.Add(Me.hdrAddress)
Me.lstServices.FullRowSelect = True
Me.lstServices.HeaderStyle =
System.Windows.Forms.ColumnHeaderStyle.Nonclickable
Me.lstServices.Location = New System.Drawing.Point(8, 27)
Me.lstServices.Size = New System.Drawing.Size(212, 202)
Me.lstServices.View = System.Windows.Forms.View.Details
'
'hdrTicket
'
Me.hdrTicket.Text = "Ticket #"
Me.hdrTicket.Width = 55
'
'hdrStop
'
Me.hdrStop.Text = "Stop"
Me.hdrStop.Width = 55
'
'hdrAddress
'
Me.hdrAddress.Text = "Address"
Me.hdrAddress.Width = 200
'
'InputPanel1
'
'
'frmSelectTicket
'
Me.ControlBox = False
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.VScrollBar1)
Me.Font = New System.Drawing.Font("Tahoma", 8.0!,
System.Drawing.FontStyle.Regular)
Me.Menu = Me.mnuMain
Me.Text = "Route Tracker"

End Sub

#End Region

#Region "Singleton Support"

Private Shared internalInstance As frmSelectTicket

Public Shared Function Instance() As frmSelectTicket
If (internalInstance Is Nothing) Then
internalInstance = New frmSelectTicket
End If
Return internalInstance
End Function

#End Region

#Region "Scrolling Screen code"

Sub SetupScrollBar()
If (G_SIP = True) Then 'Define the size of the scrollbar
VScrollBar1.Visible = True
VScrollBar1.Height = MINSCROLLSIZE 'Set size of scrollbar
VScrollBar1.Maximum = Panel1.Height - MINSCROLLSIZE 'The
maximum scroll value places bottom of frame at bottom of screen
Else
VScrollBar1.Visible = False 'Hide the scrollbar, SIP isn't
showing
VScrollBar1.Value = 0
'Place the panel at the very top boundaries of the form
Panel1.Left = 0
Panel1.Top = 0
End If

VScrollBar1.Minimum = 0 'Set minimum to zero

'Set our increments to be ratio of current maximums
VScrollBar1.SmallChange = VScrollBar1.Maximum / 100
VScrollBar1.LargeChange = VScrollBar1.Maximum / 10
End Sub

Private Sub VScrollBar1_ValueChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles VScrollBar1.ValueChanged
'On scroll bar change move the frame w/r to the form
Panel1.Left = 0
Panel1.Top = -VScrollBar1.Value
End Sub

Private Sub InputPanel1_EnabledChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles InputPanel1.EnabledChanged
If InputPanel1.Enabled = True Then
G_SIP = True 'SIP is showing
Else
G_SIP = False 'SIP isn't showing
End If
SetupScrollBar()
End Sub

#End Region

Private Sub frmServices_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load
Me.lstServices.Font = New Font(FontFamily.GenericSansSerif, 8,
FontStyle.Regular)
End Sub

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuExit.Click
Me.Dispose()
Application.Exit()
End Sub

Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles mnuAbout.Click
Dim about As New frmAbout

about.lblFormFrom.Text = "frmSelectTicket"
about.Text = "Route Tracker"
Me.Text = ""
about.Show()
G_SIP = False
End Sub

Private Sub lstServices_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles lstServices.Click
'Open ticket information screen
frmTicketInfo.Instance().txtStop.Focus()
frmTicketInfo.Instance().Text = "Route Tracker"
Me.Text = ""
frmTicketInfo.Instance().Show()
G_SIP = False
End Sub

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSelect.Click
'Open ticket information screen
frmTicketInfo.Instance().txtStop.Focus()
frmTicketInfo.Instance().Text = "Route Tracker"
Me.Text = ""
frmTicketInfo.Instance().Show()
G_SIP = False
End Sub

Private Sub frmSelectTicket_Activated(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.Activated
Dim ticketNum As Integer
Dim ticketStop As Integer
Dim ticketStreet As String

lstServices.Visible = False
lstServices.Items.Clear()

'Get the plant weigh-in status if any
rsWeighPlant.Open("SELECT tblServiceTickets.RouteID,
tblServices.WeighAt FROM tblServiceTickets INNER JOIN tblServices ON
tblServiceTickets.Number = tblServices.Number WHERE
tblServices.WeighAt = 'P' AND tblServiceTickets.RouteID = " &
G_ROUTE_ID, cn)
'rsWeighPlant.MoveFirst()
If Not rsWeighPlant.EOF And Not rsWeighPlant.BOF Then
G_PLANT = True
Else
G_PLANT = False
End If
rsWeighPlant.Close()

'Get the route information
rsTickets.Open("SELECT * FROM tblServiceTickets WHERE RouteID
= " & G_ROUTE_ID, cn)
If Not rsTickets.EOF And Not rsTickets.BOF Then
rsTickets.MoveFirst()
While Not rsTickets.EOF
ticketNum =
rsTickets.Fields("Number").Value().ToString()
ticketStop =
rsTickets.Fields("Stop").Value().ToString()
ticketStreet = rsTickets.Fields("Street").Value()

'Add ticket from current record
lstServices.Items.Add(New ListViewItem(New String()
{ticketNum, ticketStop, ticketStreet}))

rsTickets.MoveNext() 'get new values from next record
End While
End If
rsTickets.Close()

If G_PLANT = True Then
'Add listview item if there are items to be weighed-in at
the plant for this route
lstServices.Items.Add(New ListViewItem(New String()
{"Weigh-In at Plant", "", ""}))
End If
lstServices.Visible = True

'Set the listview to receive focus and highlight the first
item, otherwise to the currently working ticket
Me.lstServices.Focus()
If (Me.lstServices.Items.Count > 0) Then
Dim item As ListViewItem = Me.lstServices.Items(0)
item.Focused = True
item.Selected = True
End If
End Sub

Private Sub lstServices_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
lstServices.SelectedIndexChanged
If (Me.lstServices.SelectedIndices.Count > 0) Then
Dim item As ListViewItem =
DirectCast(Me.lstServices.Items(Me.lstServices.SelectedIndices.Item(0)),
ListViewItem)
G_TICKET = Convert.ToInt32(item.Text)
End If
End Sub
End Class
 
Another issue, even though it is setting focus to the item when the
form is shown, the listview does not have focus and therefore you
cannot use the navigation keys to move up and down in the listview.
Why is this and how can I keep the listview focused?
 
The code below places focus into the ListView and allows me to use the
navigational keys on the front of my iPAQ to move up and down, or side to
side, when the Form is first loaded.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ListView1.Focus()
If (Me.ListView1.Items.Count > 0) Then
Dim item As ListViewItem = Me.ListView1.Items(0)
item.Focused = True
item.Selected = True
End If
End Sub
 
Sorry Aaron did you want me to do anything with what you posted?

You state up front it is not reproducible (so what's the point?) but
actually the code is incomplete (G_, rsXX, frmXX missing)

Just in case you don't realise this but the event will also fire when you
clear the listview (which you seem to do in form activated)...

Cheers
Daniel
 
Back
Top