none
how to copy data from ListBox to TreeView and make filter RRS feed

  • Question

  • hi guys.. i have this example:

    i have names in listbox i need display them in treeview by the first char

    i mean i need make groups by the first char and count the name look like the example

    thank you

    Sunday, September 10, 2017 6:57 AM

Answers

  •  Assuming that the Items in the ListBox are arranged in the way you have shown us,  in alphabetical order of the first letters,  then here is a pretty simple example to understand...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()

            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing

            For Each s As String In ListBox1.Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If

                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub
     

     

      

     EDIT:  

     If your ListBox items are not actually sorted alphabetically as you showed,  then you can create a List of the items and sort them,  then iterate through the sorted list and create the TreeNodes.  For example...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()
    
            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing
    
            Dim Items As List(Of String) = ListBox1.Items.Cast(Of String).ToList
            Items.Sort()
    
            For Each s As String In Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If
    
                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Proposed as answer by tommytwotrain Sunday, September 10, 2017 12:01 PM
    • Edited by IronRazerz Sunday, September 10, 2017 1:30 PM
    • Marked as answer by Salar Music Monday, September 11, 2017 8:47 AM
    Sunday, September 10, 2017 11:09 AM

All replies

  • Hi,

    Would you share your sample project via cloud storage such as OneDrive, Dropbox, etc?
    It would shorten our moodifying code.

    Regards,

    Ashidacchi

    Sunday, September 10, 2017 8:47 AM
  • Hi,

    Would you share your sample project via cloud storage such as OneDrive, Dropbox, etc?
    It would shorten our moodifying code.

    Regards,

    Ashidacchi

    this is just example empty form not have any codes just component
    Sunday, September 10, 2017 9:04 AM
  • If there is no code, only form and controls would help us.

    Ashidacchi

    Sunday, September 10, 2017 9:06 AM
  • If there is no code, only form and controls would help us.

    Ashidacchi

    i hope you help me
    Sunday, September 10, 2017 9:26 AM
  • I cannot help you.
    I hope you will think about people who want to help you for their time-saving. I don't work for Microsoft, but a self-employed software developer. And I want to help not only you but also others.
    Good bye, KORG Pa3x.

    Ashidacchi

    Sunday, September 10, 2017 9:36 AM
  •  Assuming that the Items in the ListBox are arranged in the way you have shown us,  in alphabetical order of the first letters,  then here is a pretty simple example to understand...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()

            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing

            For Each s As String In ListBox1.Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If

                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub
     

     

      

     EDIT:  

     If your ListBox items are not actually sorted alphabetically as you showed,  then you can create a List of the items and sort them,  then iterate through the sorted list and create the TreeNodes.  For example...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()
    
            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing
    
            Dim Items As List(Of String) = ListBox1.Items.Cast(Of String).ToList
            Items.Sort()
    
            For Each s As String In Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If
    
                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Proposed as answer by tommytwotrain Sunday, September 10, 2017 12:01 PM
    • Edited by IronRazerz Sunday, September 10, 2017 1:30 PM
    • Marked as answer by Salar Music Monday, September 11, 2017 8:47 AM
    Sunday, September 10, 2017 11:09 AM
  •  Assuming that the Items in the ListBox are arranged in the way you have shown us,  in alphabetical order of the first letters,  then here is a pretty simple example to understand...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()

            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing

            For Each s As String In ListBox1.Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If

                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub
     

     

     


    If you say it can`t be done then i`ll try it

    IR,

    You're depending on the ListBox items to be sorted. If not, then the TreeView's items won't be either, but a slight modification to your code will order them correctly:

        Private Sub CreateTreeViewItems()
    
            ListBox1.Items.AddRange(New String() {"Horse", "Dog", "Elephant"})
    
            TreeView1.Nodes.Clear()
    
            Dim collection As IOrderedEnumerable(Of String) = _
                From s As String In ListBox1.Items.Cast(Of String)() _
                    Order By s
    
            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing
    
            For Each s As String In collection
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If
    
                RootNode.Nodes.Add(s)
                RootNode.Text = String.Format("{0}  (Count: {1})", _
                                              FirstLetter, _
                                              RootNode.Nodes.Count)
            Next
    
        End Sub


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, September 10, 2017 12:14 PM
  • IR,

    You're depending on the ListBox items to be sorted. If not, then the TreeView's items won't be either, but a slight modification to your code will order them correctly:

        Private Sub CreateTreeViewItems()
    
            ListBox1.Items.AddRange(New String() {"Horse", "Dog", "Elephant"})
    
            TreeView1.Nodes.Clear()
    
            Dim collection As IOrderedEnumerable(Of String) = _
                From s As String In ListBox1.Items.Cast(Of String)() _
                    Order By s
    
            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing
    
            For Each s As String In collection
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If
    
                RootNode.Nodes.Add(s)
                RootNode.Text = String.Format("{0}  (Count: {1})", _
                                              FirstLetter, _
                                              RootNode.Nodes.Count)
            Next
    
        End Sub


    "A problem well stated is a problem half solved.” - Charles F. Kettering

     That is why i started by saying...

    "Assuming that the Items in the ListBox are arranged in the way you have shown us,  in alphabetical order of the first letters..."

      I did think about sorting the names/strings but,  i figured OP was already doing that somehow by looking at the picture they posted.  However,  they are only alphabetized according to the first letters.  Notice that according to the second letter of the names,  they are not sorted alphabetically.  For example,  "Brian" is before "Blary".  8)

    If you say it can`t be done then i`ll try it

    Sunday, September 10, 2017 1:17 PM
  •   I did think about sorting the names/strings but,  i figured OP was already doing that somehow by looking at the picture they posted.  However,  they are only alphabetized according to the first letters.  Notice that according to the second letter of the names,  they are not sorted alphabetically.  For example,  "Brian" is before "Blary".  8)

    If you say it can`t be done then i`ll try it

    I don't understand why he's got a ListBox involved at all, but I'm sure there's a reason. ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, September 10, 2017 1:21 PM
  •  Assuming that the Items in the ListBox are arranged in the way you have shown us,  in alphabetical order of the first letters,  then here is a pretty simple example to understand...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()

            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing

            For Each s As String In ListBox1.Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If

                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub
     

     

      

     EDIT:  

     If your ListBox items are not actually sorted alphabetically as you showed,  then you can create a List of the items and sort them,  then iterate through the sorted list and create the TreeNodes.  For example...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TreeView1.Nodes.Clear()
    
            Dim FirstLetter As String = ""
            Dim RootNode As TreeNode = Nothing
    
            Dim Items As List(Of String) = ListBox1.Items.Cast(Of String).ToList
            Items.Sort()
    
            For Each s As String In Items
                If s.Remove(1).ToUpper <> FirstLetter Then
                    FirstLetter = s.Remove(1).ToUpper
                    RootNode = TreeView1.Nodes.Add(FirstLetter)
                End If
    
                RootNode.Nodes.Add(s)
                RootNode.Text = FirstLetter & "  (Count: " & RootNode.Nodes.Count.ToString & ")"
            Next
        End Sub


    If you say it can`t be done then i`ll try it

    thank you very much work fine :*
    Monday, September 11, 2017 8:47 AM