I am using a navigation panel component from a company called Nevron. I am trying to populate the navigation pane through a database, and then each pane has got a treeview with options in it.
I have constructed all this like so (Works great, everything is generated.. see screenshot)
Code:
Public Function BuildNavigationFromDatabase(ByVal UserID As Integer) As Boolean
Try
Dim DBCON As New SqlConnection(My.Settings.JMSConnectionString)
Dim GetNav As New SqlCommand("SELECT * FROM tNavigation WHERE ParentMenuID=-1", DBCON)
'Build Main Stuffs
DBCON.Open()
Dim NavRead As SqlDataReader = GetNav.ExecuteReader
If NavRead.HasRows = True Then
While NavRead.Read
Dim Pane As New Nevron.UI.WinForm.Controls.NNavigationPaneBand
With Pane
.Text = NavRead("Title")
.Image = Me.ImageList1.Images.Item(NavRead("ImageIndexID"))
.SmallImage = Me.ImageList1.Images.Item(NavRead("ImageIndexID"))
.Name = NavRead("MenuID")
End With
Me.NNavigationPane1.Controls.Add(Pane)
BuildTree(Pane.Name)
End While
End If
DBCON.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
Public Function BuildTree(ByVal PaneID As Integer)
Try
Dim DBCON As New SqlConnection(My.Settings.JMSConnectionString)
Dim GetNav As New SqlCommand("SELECT * FROM tNavigation WHERE ParentMenuID=" & PaneID, DBCON)
For Each Searchpane As Nevron.UI.WinForm.Controls.NNavigationPaneBand In Me.NNavigationPane1.Controls.Find(PaneID, True)
Dim ChildTree As New TreeView
ChildTree.Dock = DockStyle.Fill
ChildTree.ImageList = Me.ImageList1
ChildTree.ShowRootLines = False
ChildTree.FullRowSelect = True
DBCON.Open()
Dim ChildRead As SqlDataReader = GetNav.ExecuteReader
If ChildRead.HasRows = True Then
While ChildRead.Read
Dim unitnode As TreeNode
unitnode = New TreeNode
unitnode.Name = ChildRead("MenuID").ToString
unitnode.Text = ChildRead("Title").ToString
unitnode.ImageIndex = ChildRead("ImageIndexID").ToString
unitnode.Tag = ChildRead("FunctionID") & "/" & ChildRead("FunctionArgument")
ChildTree.Nodes.Add(unitnode)
End While
End If
DBCON.Close()
Searchpane.Controls.Add(ChildTree)
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Function
Now, all I need to do now, is be able to specify the onclick events somehow… normally I would have dragged the treeview component onto the form, and all my events are shown on the build events pages in the property page, but now the whole treeview is code, and I’m a little stuck….
How would I specify the events etc for each treeview?
If you are creating the TreeView controls in the code than you need to have an array of the TreeView controls, add event handlers for each control. Don't forget to remove the event handlers when the app is closing!
Rating is a way of saying thank you. Don't forget to rate always!
Ok yes, that's what I want to do... but my question is... How do I create the even handlers?
I'm giving each treeview it's own unique name in my code... something like "TV3" or "TV6" based on the id in my database... but how will I create an event handler for like say, the onclick event, and then I just pass the name of the treeview into the function, then I will use a select case statement to handle which treeview was clicked and then do something...
Ok, if your TreeView controls are know from the biginging and they will not change during the app runs than you don't need an array, but you can declar the treeTiew controls like this:
vb Code:
Dim WithEvents myTree As New TreeView
and you can add events as you do normaly.
Rating is a way of saying thank you. Don't forget to rate always!
The treeviews are created dynamically... in that function... So what I do is... first create the pane... and then create a treeview and add it to the pane... so in design time I don't know how many treeviews will be there... it all depends on the amount of records defined in the database and the users permissions...
I guess I have to initialise the treeviews? so how would I do that inside the function and not in the form's designer code? or could I do that via the db?
Also... could I create an event handler to handle all the treeviews ? so I got one event handler,that handles all the treeviews, and then Inside the event, I pass in the treeview being used... for eg:
Code:
Private Sub TV_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Treeview.Click '???
Select e.Name
Case "TV1"
'Handle Treeview 1's event which is specific
Case else
'Handle all other treeviews events
End Select
End Sub