Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

TreeView Node + brackets that update a number when an item is added to a Listbox VB.NET

i Experts,

Is there a way I could create brackets () which appear at the end of  a node on a TreeView in VB.net 2003?

example... the (2) appears next to the Inbox in Outlook with a number of how many emails are in the mailbox.

So ultermately, if an item appears in the Listbox... the listview (0) would change to (1)... for example.

Cheers,
Roberto
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

When you set the Text for the TreeNode, add the parentheses with the numbers.

Bob
If you know how many items you are adding the solution provided by the TheLearnedOne is the one

Here is an example

        Dim root As TreeNode = Me.TreeView1.Nodes.Add("Root")

        Dim parent As TreeNode = root.Nodes.Add("myNode1 (5)")  '(5) are the items added next
        parent.Nodes.Add("Child 5")
        parent.Nodes.Add("Child 4")
        parent.Nodes.Add("Child 3")
        parent.Nodes.Add("Child 2")
        parent.Nodes.Add("Child 1")

        Dim parent2 As TreeNode = root.Nodes.Add("myNode2 (3)")  '(3) are the items added next
        parent2.Nodes.Add("Child2 3")
        parent2.Nodes.Add("Child2 2")
        parent2.Nodes.Add("Child2 1")
Avatar of RobertoFreemano

ASKER

Hi,

That does add a node with node(5) and five nodes; however, it wasn't quite what I want to do... I will try explain more clearly.

1. I have a TreeView with a node
2. when node is clicked it loads .txt names into a Listbox from a folder.
3. I would like the node to detect how many .txt files are in that folder and report it to the root node(?).
4. so If I add a new txt file to that folder, the node(?) will add a number.

----code------
Select Case node.Text

Case "Login"
                    ListBox1.Items.Clear()
                    Me.TabControl1.SelectedTab = Me.TabPage1
                    Dim sourceFolder As String = "C:\data"
                    ListBox1.BeginUpdate()
                    ListBox1.Items.Clear()
                    ListBox1.Items.AddRange((New System.IO.DirectoryInfo(sourceFolder)).GetFiles("*.txt"))
                    ListBox1.EndUpdate()
----------------

Cheers,
Roberto
If you have a reference to a node, you can get a reference to parent, and ask how many children it has, and display that for the parent.  I am not sure where you are trying to do this.

Bob
       Dim di As New IO.DirectoryInfo(sourceFolder)
        Dim dirCountedFiles As Long = di.GetFiles("*.txt").Length
        MsgBox(dirCountedFiles)
parent2.Text = "myNode2 (" & dirCountedFiles & ")"
-or-
        Dim i as integer=Listbox1.Items.Count
parent2.Text = "myNode2 (" & i & ")"
-Also- you can detect the node clieecked by using some of these subs & functions
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim xParent As String = ""
        Dim mySelectedItem As String = e.Node.Text
        If e.Node.Parent Is Nothing Then
            xParent = ""
        Else
            Dim d As TreeNode = e.Node.Parent
            xParent = d.Text
        End If
        cnter = 0
        Dim IndexOfItem As Integer = 0
        If xParent.Length = 0 Then
            IndexOfItem = 0
        Else
            IndexOfItem = SearchParnetNode(Me.TreeView1.Nodes(0), mySelectedItem, xParent)
        End If
        MsgBox(IndexOfItem)
    End Sub

    Public Function SearchParnetNode(ByVal nod As TreeNode, ByVal mySelectedItem As String, ByVal xParentNode As String) As Integer
        Dim idx As Integer = 0
        Do While Not (nod Is Nothing)
            If nod.Text = xParentNode Then
                cnter += 1
                idx = GetIndexOfNode(nod.Nodes(0), mySelectedItem)
                Return idx
            Else
                cnter += 1
                If nod.Nodes.Count > 0 Then
                    idx = SearchParnetNode(nod.Nodes(0), mySelectedItem, xParentNode)
                    If idx > 0 Then
                        Return idx
                    End If
                End If
            End If
            Try
                nod = nod.NextNode
            Catch ex As Exception
                nod = Nothing
            End Try
        Loop
        Return idx
    End Function

    Public Function GetIndexOfNode(ByVal nod As TreeNode, ByVal sSearchFor As String) As Integer
        Dim idx As Integer = 0
        Do While Not (nod Is Nothing)
            If nod.Text = sSearchFor Then
                idx = cnter
                Return idx
            Else
                cnter += 1
                If nod.Nodes.Count > 0 Then
                    GetIndexOfNode(nod.Nodes(0), sSearchFor)
                End If
            End If
            Try
                nod = nod.NextNode
            Catch ex As Exception
                nod = Nothing
            End Try
        Loop
        Return idx
    End Function

    Private Function getNodeIndex(ByRef tv As TreeNode, ByVal txtToFind As String) As Integer
        Dim tmp As String = ""
        Dim xItem As Integer = -1
        Dim myNodeCollection As TreeNodeCollection = tv.Nodes
        Dim myCount As Integer = myNodeCollection.Count
        Try
            For i As Integer = 0 To myCount
                tmp = tv.Nodes(i).Text
                If InStr(tmp, " ") > 0 Then tmp = Mid(tmp, InStr(tmp, " ")).Trim
                'If InStr(tmp, txtToFind) > 0 Then
                If tmp = txtToFind Then
                    xItem = i
                    Exit For
                End If
            Next
        Catch ex As Exception
            MsgBox("Sub-->getNodeIndex " & vbCrLf & vbCrLf & ex.ToString)
        End Try
        Return xItem
    End Function
It's like magic!!  You suggest something, and it magically appears :D  Wow!!

Bob
Hi Bob
Did my code match your suggestion?
As I said in my first comment "If you know how many items you are adding the solution provided by the TheLearnedOne is the one" so I am not tring to get your glory. I just provided something in code.
Any way you can just continue helping our friend.
Take care
My friend, I usually try to have a sense of humor, and I have plenty of opportunity for my own glory.  The times I usually have a problem is when someone repeats what I say as their own.

Bob
Thank's Bob
I just provided some code in writting. I never try to repeat "as my own" anything. I just try to be of help.

As for the second comment I added, I did not see your suggestion.
I read the #19690405 question and while I was typing the answer you suggested the same thing. Indeed that is magic.

Bye
-and- PLEASE RobertoFreemano No points for me!
>>PLEASE RobertoFreemano No points for me!

I don't need no stinkin' points, and you provided some detail for my vague suggestion.

Bob
Hi Chaps,

AkisC,

I tried your code =
Dim di As New IO.DirectoryInfo(sourceFolder)
Dim dirCountedFiles As Long = di.GetFiles("*.txt").Length
MsgBox(dirCountedFiles)
parent2.Text = "myNode2 (" & dirCountedFiles & ")"

and it does the trick...( it creates a node with (?) and detects how many text files, etc....  but in my code I have a CASE SELECT code on the treeview and it doesn't seem to work on this new node 'myNode2'

----here is some of my code--------------------
Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
        Dim pt As Point = TreeView1.PointToClient(Windows.Forms.Cursor.Position)
        Dim node As TreeNode = TreeView1.GetNodeAt(pt)
        ' only do something with the child noodes
        If Not (node.Parent Is Nothing) Then
            Select Case node.Text
                ' LOGIN
            Case "Login"
                    ListBox1.Items.Clear()
                    Me.TabControl1.SelectedTab = Me.TabPage1
                    Dim sourceFolder As String = "C:\dat$\login"
                    ListBox1.BeginUpdate()
                    ListBox1.Items.Clear()
                    ListBox1.Items.AddRange((New System.IO.DirectoryInfo(sourceFolder)).GetFiles("*.txt"))
                    ListBox1.EndUpdate()
-------------------------------------------------------------

I've played around with the code but have hit all sorts of errors.

Any ideas
Hi RobertoFreemano
If TheLearnedOne can not come up with an idea, let me know and we can step to it...

I do not want to be an annoyance to the EE system, since I am quite new here and my will is to be helped and try to help. I am really sorry if I have created any kind of problem to the TheLearnedOne. That was not my intention.
AkisC
Well probablly TheLearnedOne is not online, and if I was you then I would want an answer quick, I beleive that the code bellow will do the job

before the
    ...here...
Select Case node.Text
-type-
dim tmp as string=node.Text
if instr(tmp,"(")>0 then
   tmp=mid(tmp,1,instr(tmp,"(")-1).trim
end if
-then-
replace the
Select Case node.Text
-with-
Select Case tmp
....
After the statment
End Select
-type-
node.Text = tmp & " (" & Listbox1.Items.Count & ")"
tmp=Nothing

Let me know if this did -or- did not work...
It sounds like you need to use an If with StartsWith:

If node.Text.StartsWith("Login") Then
End If

Bob
Hi Guys,

I'm making a mess of this (apologoes...)

Could you provide code (from Scatch)

1. A NODE that detects how many files in a folder and displays NODE (?)
2. when that node is clicked... it performs an action.

Cheers,
Roberto
Roberto,

I don't know what you are trying to do, and where you have gone with this.   I don't have code for you for this, but I am willing to help you get where you need to be.

Bob
Thanks Bob,

I'm trying to build an app wich will generate txt files and save then to a specific folder... my winform will have an Explorer like TREEVIEW wich I want to report the total number of text files on the note (?)... just like Outlook does.

The code above creates a node and reports how many txt files are in that folder... but I need to be able to click on the node to be able to bring the file names up on a listbox...

Hope that give you a better idea of what I want to do.

Cheers,
Roberto
Show me what you tried, and what you are having a problem with.

Bob
ASKER CERTIFIED SOLUTION
Avatar of AkisC
AkisC
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As I said
No points for me!
All credit goes to TheLearnedOne
Forced accept.

Computer101
EE Admin