|
-
Feb 1st, 2005, 06:29 AM
#1
Thread Starter
Lively Member
TreeView BUG! Why?!
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:
Code:
If ListView1.SelectedItems.Count = 0 Then Exit Sub
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:
MsgBox(ListView1.SelectedItems(0).Text)
If ListView1.SelectedItems(0).Index = 0 Then
ListView1.Items(2).Selected = True
End If
As you see, I have added a message box that shows the selected item text.
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
-
Feb 2nd, 2005, 09:22 AM
#2
Thread Starter
Lively Member
Re: TreeView BUG! Why?!
why ?????
-
Feb 2nd, 2005, 11:14 PM
#3
Hyperactive Member
Re: TreeView BUG! Why?!
the SelectedIndexChange always trigger everytime you select the items in the listview that's why i think it appear many times in your messagebox. Maybe you can do a different twist like this one:
VB Code:
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
Dim li As ListViewItem = ListView1.GetItemAt(e.X, e.Y)
If li.SubItems(0).Text = "Item 1" Then
li = ListView1.Items(li.Index + 2)
li.Selected = True
End If
MessageBox.Show(li.SubItems(0).Text)
End Sub
EDIT: BTW your topic is not a treeview its a listview
Last edited by fret; Feb 2nd, 2005 at 11:19 PM.
-
Feb 3rd, 2005, 12:55 AM
#4
Re: TreeView BUG! Why?!
you are creating a recursive loop because when you call the line
ListView1.Items(2).Selected = True
you then fire the selectedindex change event again... and it shows that message box before it comes back and finishes the first full run through the subroutine... set a break point on the line
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
and run the code and step through line by line and you will see what is happening
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|