Windows Develop Bookmark and Share   
 index > Windows Forms General > TreeView clicked node?
 

TreeView clicked node?

ok, i have a treeview that i load all of the root nodes and thier children via code:

Code Snippet

Public Sub LoadTree()
'for each row in the dataset, create a button in the menubar
For Each dr As DataRow In _ds.Tables("tblTab_Topics").Rows
tn = New TreeNode(dr.Item("strDescrip").ToString)
tn.Tag = dr.Item("strDescrip").ToString
For Each drChild As DataRow In dr.GetChildRows("Topic_Group")
tn.Tag = drChild.Item("strDescrip").ToString
tn.Nodes.Add(drChild.Item("strDescrip").ToString)

Next

TreeMenu.Nodes.Add(tn)
Next


End Sub


before i tried the .tag option i tried the .name. it seems that later in the code when i need to pull that info back out so i can determine which child node was clicked, the .tag is nothing the .name was the same way.

i have the treeview on the left and a tab control on the right. i add a new tabpage and add a user control to that new tab when i click a child node. i do not want to add a tab if the root is clicked. also i need the text of the tab to be "root\child" because some of the roots have childs that have the same text as another child in a different root.
at this point the adding of the new tabpage works fine but i cant get the name or text or tag for some reason. so my new tab loads with a generic name. heres how i load the new tab:
Code Snippet

Private Sub TreeMenu_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeMenu.AfterSelect

Dim uc As UserControl

Select Case tn.Tag.ToString
Case "Patient Info"
uc = New ucDemoTab
Case Else
uc = New ucTabPage
End Select
ucTabs.AddNewTabPage(DirectCast(uc, Control), tn.Tag.ToString)

End Sub


If someone could straighten this one out that would be a major help.
thanks,
Jess


JeZteRicp  Tuesday, March 11, 2008 5:50 PM

Hi JeZteRicp,

Based on my understanding, you want to set each ParentNode and ChildNode's Tag property in order to use them later when creating new TreeNode object. If I have misunderstood you, please feel free to tell me, thanks !

I have just made one for you and it works. You need not declare TreeNode at the top of the method. You just need create a temporary variable and add it into the Nodes collection, meanwhile, set the Tag property of each node.

Code Snippet

Private Sub FillTreeView()

For Each drParent As DataRow In ds.Tables(0).Rows

Dim pnode As New TreeNode(drParent(0).ToString())

pnode.Tag = drParent(1).ToString()

'Set parent node's tag.

Me.treeView1.Nodes.Add(pnode)

For Each drChild As DataRow In drParent.GetChildRows("Cus_Ord")

Dim childNode As New TreeNode(drChild(0).ToString())

childNode.Tag = drChild(1).ToString()

' Set child node's tag.

pnode.Nodes.Add(childNode)

Next

Next

End Sub

Hope this helps,

Regards,

Roy Zhou

Roy Zhou - MSFT  Monday, March 17, 2008 4:07 AM
ok i've modified the code snippets to what i have now. i also moved dim tn as treenode to the declares at the top. so what i get now is a tab that loads with the name of whatever the last node was that was added in the loadtree method. so it seems as it iterates thru the for loops where i set "tn.tag = something", it apparently overwrites what the tn.tag was from the last iteration. so i get stuck with tn.tag being whatever the last node was. so how do i set the tag specifically for the node in the current iteration and set a different tag to the next node in the next iteration?
JeZteRicp  Tuesday, March 11, 2008 7:21 PM

Hi JeZteRicp,

Based on my understanding, you want to set each ParentNode and ChildNode's Tag property in order to use them later when creating new TreeNode object. If I have misunderstood you, please feel free to tell me, thanks !

I have just made one for you and it works. You need not declare TreeNode at the top of the method. You just need create a temporary variable and add it into the Nodes collection, meanwhile, set the Tag property of each node.

Code Snippet

Private Sub FillTreeView()

For Each drParent As DataRow In ds.Tables(0).Rows

Dim pnode As New TreeNode(drParent(0).ToString())

pnode.Tag = drParent(1).ToString()

'Set parent node's tag.

Me.treeView1.Nodes.Add(pnode)

For Each drChild As DataRow In drParent.GetChildRows("Cus_Ord")

Dim childNode As New TreeNode(drChild(0).ToString())

childNode.Tag = drChild(1).ToString()

' Set child node's tag.

pnode.Nodes.Add(childNode)

Next

Next

End Sub

Hope this helps,

Regards,

Roy Zhou

Roy Zhou - MSFT  Monday, March 17, 2008 4:07 AM

You can use google to search for other answers

Custom Search

More Threads

• Accessing mdi parent properties from mdichildren
• How to change the property of the custom control
• Issue with docking listboxes.. ugly gap at the bottom.
• Cut and paste text into textboxes doesn't work
• Custom ScrollBar control
• MDI parent background?
• Stumped... System.ComponentModel.Win32Exception: Error creating window handle.
• Text wrapping labels inside FlowLayoutPanel
• A .Net Plugin for a Browser
• GDI+ graphics and viewport