Results 1 to 3 of 3

Thread: Treeview control Microsoft example performing oddly upon node click...

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    2

    Treeview control Microsoft example performing oddly upon node click...

    I downloaded the Visual Studio Community application. (2019). I followed the steps in this example on the Microsoft site:
    https://docs.microsoft.com/en-us/dot...orkdesktop-4.8

    The example is C#. I managed to convert to Visual Basic. It works, sort of.

    The list view is populated from the TreeView1.MouseClick.
    The problem is, it takes two node clicks before it updates the listbox.
    I have a node "bin", a child node "Debugg", which has a child node "netcoreapp3.1"
    listbox is populated with folder "debug" upon load.
    Expand top node "bin". msgbox displays "bin".
    Select node "debug", listbox does not update, msgbox displays "bin".
    Select node "debug" again, listbox updates, msgbox displays "Debug".
    Nothing I do fixes this "two clicks required". Its very annoying.
    Please help? What am I doing wrong?

    Code:
    Code:
        Private Sub TreeView1_MouseClick(sender As Object, e As MouseEventArgs) Handles TreeView1.MouseClick
    
            Dim newSelected As TreeNode = TreeView1.SelectedNode
            MessageBox.Show(newSelected.Text)
    
            ListView1.Items.Clear()
            Dim nodeDirInfo As DirectoryInfo = CType(newSelected.Tag, DirectoryInfo)
            Dim subItems As ListViewItem.ListViewSubItem()
            Dim item As ListViewItem = Nothing
    
            For Each dir As DirectoryInfo In nodeDirInfo.GetDirectories()
                item = New ListViewItem(dir.Name, 0)
                subItems = New ListViewItem.ListViewSubItem() {New ListViewItem.ListViewSubItem(item, "folder"), New ListViewItem.ListViewSubItem(item, dir.LastAccessTime.ToShortDateString())}
                item.SubItems.AddRange(subItems)
                ListView1.Items.Add(item)
            Next
    
            For Each file As FileInfo In nodeDirInfo.GetFiles()
                item = New ListViewItem(file.Name, 1)
                subItems = New ListViewItem.ListViewSubItem() {New ListViewItem.ListViewSubItem(item, "File"), New ListViewItem.ListViewSubItem(item, file.LastAccessTime.ToShortDateString())}
                item.SubItems.AddRange(subItems)
                ListView1.Items.Add(item)
            Next
    
            ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
    
        End Sub
    Thank you for any help.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,404

    Re: Treeview control Microsoft example performing oddly upon node click...

    Donít use the TreeView1_MouseClick event. Use the TreeView1_AfterSelect event instead

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2021
    Posts
    2

    Re: Treeview control Microsoft example performing oddly upon node click...

    Quote Originally Posted by .paul. View Post
    Donít use the TreeView1_MouseClick event. Use the TreeView1_AfterSelect event instead
    ...and that did the trick!
    Thank you very much.
    I knew it was going to be something so simple that I would be embarrassed for having asked. lol.
    The help was much appreciated.

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