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