Reputation: 13
I am using a loop to search indexed objects and create a treeview from the objects that meet the criteria.
I would like to "bind" data to each node in the tree view so when it is clicked I can pull up the indexed object's properties.
Is there a way to "Bind" data to a treeview node?
Code I wrote to loop through the indexed objects.
Private ClanIndex As Integer
Public Sub TreeviewPopulate()
Dim Clan_Level As TreeNode = Nothing
Dim Enclave_Title_Level As TreeNode = Nothing
Dim Enclave_Planet_Level As TreeNode = Nothing
Dim Enclave_Enclave_Level As TreeNode = Nothing
Dim _Planet As Integer
Dim _Enclave As Integer
Dim _Owner As String = Form1.GAME.Clan(ClanIndex).Clan
For _Planet = 2 To 41
For _Enclave = 1 To 4
If Form1.GAME.Planet(_Planet).Enclave(_Enclave).Owner = _Owner Then
Enclave_Planet_Level = Enclave_Title_Level.Nodes.Add(Form1.GAME.Planet(_Planet).PlanetName)
For i = 1 To 4
If Form1.GAME.Planet(_Planet).Enclave(i).Owner = _Owner Then
Enclave_Enclave_Level = Enclave_Planet_Level.Nodes.Add("Enclave " & Form1.GAME.Planet(_Planet).Enclave(i).EnclaveNumber)
End If
Next
GoTo 2
End If
Next
2:
Next
End Sub
Upvotes: 0
Views: 1121
Reputation: 6375
You can use the Tag property of the TreeNode objects that you create to store additional information. You can either store the information you want to display, or some kind of reference to the object that holds these information.
For example:
Enclave_Planet_Level = Enclave_Title_Level.Nodes.Add(Form1.GAME.Planet(_Planet).PlanetName)
Can be changed to
Dim newTN As New TreeNode(Form1.GAME.Planet(_Planet).PlanetName)
newTN.Tag = Form1.GAME.Planet(_Planet)
Enclave_Title_Level.Nodes.Add(newTN)
Then in the event handler where you handle the selection of a node you can reuse this reference
Private Sub Enclave_Title_Level_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles Enclave_Title_Level.AfterSelect
If e.Node.Tag IsNot Nothing Then
If TypeOf e.Node.Tag Is Planet Then
'Do something
End If
End If
End Sub
(I assumed the class is called Planet. The Ifs are in place to avoid exceptions).
Upvotes: 1