Results 1 to 4 of 4

Thread: TreeView BUG! Why?!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    76

    Unhappy 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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    76

    Re: TreeView BUG! Why?!

    why ?????

  3. #3
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    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:
    1. Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    2.         Dim li As ListViewItem = ListView1.GetItemAt(e.X, e.Y)
    3.         If li.SubItems(0).Text = "Item 1" Then
    4.             li = ListView1.Items(li.Index + 2)
    5.             li.Selected = True
    6.         End If
    7.         MessageBox.Show(li.SubItems(0).Text)
    8.     End Sub
    EDIT: BTW your topic is not a treeview its a listview
    Last edited by fret; Feb 2nd, 2005 at 11:19 PM.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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
  •  



Click Here to Expand Forum to Full Width