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
RobertoFreemanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Bob
0
AkisCCommented:
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")
0
RobertoFreemanoAuthor Commented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bob LearnedCommented:
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
0
AkisCCommented:
       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 & ")"
0
AkisCCommented:
-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
0
Bob LearnedCommented:
It's like magic!!  You suggest something, and it magically appears :D  Wow!!

Bob
0
AkisCCommented:
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
0
Bob LearnedCommented:
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
0
AkisCCommented:
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!
0
Bob LearnedCommented:
>>PLEASE RobertoFreemano No points for me!

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

Bob
0
RobertoFreemanoAuthor Commented:
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
0
AkisCCommented:
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
0
AkisCCommented:
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...
0
Bob LearnedCommented:
It sounds like you need to use an If with StartsWith:

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

Bob
0
RobertoFreemanoAuthor Commented:
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
0
Bob LearnedCommented:
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
0
RobertoFreemanoAuthor Commented:
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
0
Bob LearnedCommented:
Show me what you tried, and what you are having a problem with.

Bob
0
AkisCCommented:
It's like magic!!  You suggest something, and it magically appears :D  Wow!!
The times I usually have a problem is when someone repeats what I say as their own.
I don't need no stinkin' points, and you provided some detail for my vague suggestion.

AkisC,
I tried your code =
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
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

Hi Guys,
I'm making a mess of this (apologoes...)
Could you provide code (from Scatch)

WE REALLY MADE A MESS HERE..........................................DUE TO EE Cleanup Volunteer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AkisCCommented:
As I said
No points for me!
All credit goes to TheLearnedOne
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.