Treeview and listbox - I got it

daPoet

Member
Joined
Jul 22, 2008
Messages
23
Programming Experience
Beginner
Okay, so I'm what they call a newbie like others were when they just started coding.

I love working with listboxes and TreeView, so as I make a breakthrough with something I didnt understand, I'll try and share. Hopefully I'll put together an actual sample when i learn how to deploy.

the following code solves the following problems:
1. Adding the Tag of a tree node when its checkbox is selected to a listbox
2. Removing the Tag of a tree node when its checkbox is de-selected from a listbox
3. Preventing Duplicate values in a listbox.

VB.NET:
        Dim s As String = e.Node.Text() & " " & e.Node.Tag()
        Dim IsExists As Boolean
        Try

            If e.Node.Checked.Equals(True) Then
                For i As Integer = 0 To ListBox1.Items.Count - 1 ' THE FOR BLOCK IS A CHECK TO PREVENT DUPLICATES IN THE LISTBOX
                    If ListBox1.Items(i) = s Then 'sItem: is item you get from your text file
                        IsExists = True 'IsExists: is boolean
                        Exit For
                    Else
                        IsExists = False
                    End If

                Next

                If IsExists = False Then
                    ListBox1.Items.Add(s)
                End If

            Else : e.Node.Checked.Equals(False)
                For i As Integer = 0 To ListBox1.Items.Count - 1 ' THIS FOR BLOCK IS To REMOVE ITEMS PREVIOUSLY SELECTED FROM THE LISTBOX
                    If ListBox1.Items(i) = s Then 'sItem: is item you get from your text file
                        IsExists = True 'IsExists: is boolean
                        Exit For
                    Else
                        IsExists = False
                    End If

                Next

                If IsExists = True Then
                    ListBox1.Items.Remove(s)
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub


Hope this helps someone ;-) NB: I claim no ownership or brilliance for codes, I just read, copy, paste, edit. Thanks to all the Geniuses out there that help a normal guy like me to survive by sharing what you know.
 
Last edited by a moderator:
shortened to:
VB.NET:
Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
    Dim s As String = e.Node.Text & " " & CStr(e.Node.Tag)
    If e.Node.Checked Then
        If Not Me.ListBox1.Items.Contains(s) Then
            Me.ListBox1.Items.Add(s)
        End If
    Else
        Me.ListBox1.Items.Remove(s)
    End If
End Sub
Note that ListBox.ObjectCollection.Remove doesn't throw any exceptions, so you can call it without checking first if there actually is an item that it would remove.

There is one logic flaw in your code though; you prevent duplicates in listbox, but when removing you don't consider if other identical treenodes are still checked. It is easy to catch this case, but I'll give you some time to think about it. (Hint: TreeView.Nodes.Find can help to check if you can remove)

PS - I renamed your thread to not mislead people actually looking for a real FAQ about Treeviews and Listboxes ;)

PPS - use code tags and the various formatting tools available to make your post readable
 
Lol..

The shortened version is like u using some serious zipping tools to condense all that I had.

I'm going to try using the .contains method, I tried at first and was having some difficulties, which is why I used the code that I posted..

thanks alot for all the tips though, and for the renaming, the links etc. Hopefully I'll get that good as you are ;-)

When I try the code, I'll post back.

PS: Im not sure I understand the flaw. For my situation, the checkboxes that really I want to get the values for are those beside an employee's name. No two nodes represent the same employee.

I'm sure you know what you're talking about though, so could you give an hypothetical example to help me understand where the problem lies... then I can go tackle the code.

PPS: This coding thing is fun.
 
No two nodes represent the same employee.
Then there is no way you could get duplicate items in listbox, and no reason to check Contains before adding the item (and no flaw when removing). I thought you had a reason to expect duplicates as per your requirement #3.
This coding thing is fun.
Yes, it can get addictive as you figure out more thing to do.
 
Sweet!!!!

Hey, Thanks a million

1.
VB.NET:
 e.Node.Checked.Equals(True)
is the same as
VB.NET:
e.Node.Checked
2.
VB.NET:
Me.ListBox1.Items.Contains(s).Equals(False)
is the same as
VB.NET:
If Not Me.ListBox1.Items.Contains(s)
3. but the
VB.NET:
.contains(s)
is the real beauty

Thanks a million. I'm still mulling over the problem you mentioned. Still dont fully understand it, but I'm probably gona run into it.

Thanks again.

I commented out my entire block that I had posted earlier and replaced it with the new one you posted. Already it looks much more reader friendly.
 
Then there is no way you could get duplicate items in listbox, and no reason to check Contains before adding the item. I thought you had a reason to expect duplicates as per your requirement #3.

Well somehow when I just started, it was just adding selected nodes to the listbox, no matter if they were there already cause I was just using the
VB.NET:
ListBox1.Items.Add(s)

but I think, in relation to the flaw you're talking about, later on I'm gona want to remove all "child" nodes of a particular parent based on a search criteria, so maybe at that point I am gona need to solve that problem that you alluded to earlier. So i'll come back to this post and the hint you gave and see if i can tackle it then.

I try to only cross the bridges when I'm at them "shortsighted" I know. lol
Doesn't mean I dont appreciate your input though, and the directions indicating where I may find trouble.
 

Latest posts

Back
Top