Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree View Question

Status
Not open for further replies.

shannanl

IS-IT--Management
Apr 24, 2003
1,071
US
I want to create a routine that will add a main node to a tree view and then I can call a second routine to add subnodes to that main node. I keep getting an error. I am new to VB.NET, all my experience is in VB 6. What am I doing wrong here?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Call ADDNODE("Departments")
Call ADDSUBNODE("mainnode1", "Procedures")

End Sub

Private Sub ADDNODE(ByVal strNodename As String)

'-- ADD MAIN NODE
Dim mainnode1 As New Windows.Forms.TreeNode("" & strNodename & "", 0, 0)
TreeView1.Nodes.Add(mainnode1)

End Sub

Private Sub ADDSUBNODE(ByVal strNodename As String, ByVal nodename As String)

'-- ADD SUB NODE
Dim subnode1 As New Windows.Forms.TreeNode("" & nodename & "", 1, 1)
strNodename.Nodes.Add(subnode1)

End Sub

The error is in the "strNodename.Nodes.Add(subnode1)". It does not like me using strNodename. It saids "nodes is not a member of string".

Thanks in advance for the help.

Shannan
 
To be honest I don't think this would have worked in VB6 either.

do something like this

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Call ADDNODE("Departments")
        
    End Sub

    Private Sub ADDNODE(ByVal strNodename As String)

        '-- ADD MAIN NODE
        Dim mainnode1 As New Windows.Forms.TreeNode("" & strNodename & "", 0, 0)
        TreeView1.Nodes.Add(mainnode1)
Call ADDSUBNODE(mainnode1, "Procedures")

    End Sub

    Private Sub ADDSUBNODE(Byref MainNode As windows.forms.treenode, Byval nodename As string)

        '-- ADD SUB NODE
        Dim subnode1 As New Windows.Forms.TreeNode("" & nodename & "", 1, 1)
        MainNode.Nodes.Add(subnode1)

    End Sub

and yes I know this can be done a lot better

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Chrissie,

Here is what I use in VB-6. To add a node I pass a unique key and a text for the node. The subnode I pass the key from the node, a new unique key for the subnode and text for the subnode.

Private Sub ADDNODE(Mykey As String, mytext As String)
'== ADD A MAIN NODE TO THE ROOT NODE
TreeView1.Nodes.Add "root", tvwChild, "" & Mykey & "", "" & mytext & "", 1

End Sub

Private Sub ADDSUBNODE(Myroot As String, Mykey As String, mytext As String)
'== ADD A SUB NODE TO THE MAIN NODE
TreeView1.Nodes.Add "" & Myroot & "", tvwChild, "" & Mykey & "", "" & mytext & "", 2

End Sub

Its very simple to populate a treeview using these two routines. I was wanting to use something like this in VB.NET. Is this possible in VB.NET?

Thanks,

Shannan
 
It's at times like this that Pointers and references can make life so much easier.

Basicly, you need to give the second sub (AddSubNode) a way to know which Node to add the sub node too.

You could have your AddNode sub be a function and return the Node you just entered, and then use that returned value to send into the AddSubNode sub to add the sub node to the main node.

Or you could keep your same subs but in the AddNode sub iterate through all of the Nodes to find the one with the same text as what you passed in.

-Rick

----------------------
 
or try this:

Code:
public sub AddSubNode(ByVal strNodename As String, ByVal nodename As String)
 strNodename.Nodes.Add(strnodename, tvwChild, nodename, nodename)
end sub

that should use strNodeName as the relative, add this node as a Child, assign the Key and Text of this node to the value of NodeName. Make sure you assign the key of the parent node though.

-Rick

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top