?
Solved

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

Posted on 2007-08-12
25
Medium Priority
?
257 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:RobertoFreemano
  • 9
  • 8
  • 4
  • +1
22 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19680773
When you set the Text for the TreeNode, add the parentheses with the numbers.

Bob
0
 
LVL 11

Expert Comment

by:AkisC
ID: 19681155
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
 

Author Comment

by:RobertoFreemano
ID: 19690405
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 19690586
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
 
LVL 11

Expert Comment

by:AkisC
ID: 19690646
       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
 
LVL 11

Expert Comment

by:AkisC
ID: 19690660
-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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19691217
It's like magic!!  You suggest something, and it magically appears :D  Wow!!

Bob
0
 
LVL 11

Expert Comment

by:AkisC
ID: 19691886
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19691921
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
 
LVL 11

Expert Comment

by:AkisC
ID: 19692017
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19692125
>>PLEASE RobertoFreemano No points for me!

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

Bob
0
 

Author Comment

by:RobertoFreemano
ID: 19697761
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
 
LVL 11

Expert Comment

by:AkisC
ID: 19697794
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
 
LVL 11

Expert Comment

by:AkisC
ID: 19697973
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19699105
It sounds like you need to use an If with StartsWith:

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

Bob
0
 

Author Comment

by:RobertoFreemano
ID: 19726374
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19726522
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
 

Author Comment

by:RobertoFreemano
ID: 19726610
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19727434
Show me what you tried, and what you are having a problem with.

Bob
0
 
LVL 11

Accepted Solution

by:
AkisC earned 1000 total points
ID: 19932616
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
 
LVL 11

Expert Comment

by:AkisC
ID: 20058250
As I said
No points for me!
All credit goes to TheLearnedOne
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20106089
Forced accept.

Computer101
EE Admin
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question