Hello All
I have a ListView with MultiSelect=False.
I found that SelectedIndexChanged event for ListViews raised twice for each selection except firs selection.
Firs for unselecting the selected item and Second for selecting the new item. So I should put the folowing code in SelectedIndexChanged event to prevent some excpetions when there is no item selected:
Next , I have 3 items in my listview and i want to change the selection to "Item 3" when "Item 1" have selected.So I have putted the following codes in SelectedIndexChanged event:Code:If ListView1.SelectedItems.Count = 0 Then Exit Sub
As you see, I have added a message box that shows the selected item text.Code:MsgBox(ListView1.SelectedItems(0).Text) If ListView1.SelectedItems(0).Index = 0 Then ListView1.Items(2).Selected = True End If
Now, What is the BUG?
When I select "Item 1" (index=0) , it should show "Item 1" , then select "Item 3" (index=2) and show "Item 3", But this will occur twice ?!( 4 MessageBox ) ! Why?
This is the complete code :
Code:Public Class frmListViewTest 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.IContainer '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.ListView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 1") Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 2") Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 3") Me.ListView1 = New System.Windows.Forms.ListView Me.SuspendLayout() ' 'ListView1 ' Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3}) Me.ListView1.Location = New System.Drawing.Point(0, 0) Me.ListView1.MultiSelect = False Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(292, 266) Me.ListView1.TabIndex = 0 Me.ListView1.View = System.Windows.Forms.View.List ' 'frmListViewTest ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.ListView1) Me.Name = "frmListViewTest" Me.Text = "frmListViewTest" Me.ResumeLayout(False) End Sub #End Region Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedItems.Count = 0 Then Exit Sub MsgBox(ListView1.SelectedItems(0).Text) If ListView1.SelectedItems(0).Index = 0 Then ListView1.Items(2).Selected = True End If End Sub End Class




( 4 MessageBox ) ! Why?
Reply With Quote