Results 1 to 10 of 10

Thread: [RESOLVED] [2005] Treeview Events

  1. #1

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Resolved [RESOLVED] [2005] Treeview Events

    Hi there,

    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?

    Thanks a lot!
    Attached Images Attached Images  

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Treeview Events

    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!

  3. #3

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: [2005] Treeview Events

    Hi there,

    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...


    How do I do the event handler?

    Thanks
    Rudi

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Treeview Events

    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:
    1. Dim WithEvents myTree As New TreeView
    and you can add events as you do normaly.

  5. #5

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: [2005] Treeview Events

    Hi there,

    Ok tried that... but I get the following error....what can be wrong?
    Attached Images Attached Images  

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Treeview Events

    sorry i didn't mentioned! you need to declare it in the class level.
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim WithEvents myTree As New TreeView
    4.  
    5.  
    6.     Private Sub myTree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles myTree.NodeMouseClick
    7.  
    8.     End Sub
    9.  
    10. End Class

  7. #7

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: [2005] Treeview Events

    Hmmm...ok that is part of my problem...

    How will I work around this...

    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
    Something like that...?

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Treeview Events

    Ok, than you need to use an array of TreeView controls. Here is an example:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim myTreeViews As New List(Of TreeView)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'Create new TreeView (TV1)
    7.         Dim newTreeView As New TreeView
    8.         'Set some properties
    9.         newTreeView.Name = "TV1"
    10.         'Add event handler
    11.         AddHandler newTreeView.NodeMouseClick, AddressOf Me.myTree_NodeMouseClick
    12.         'Add to the list
    13.         MyTreeViews.Add(newTreeView)
    14.  
    15.  
    16.         'Create new TreeView (TV2)
    17.         newTreeView = New TreeView
    18.         'Set some properties
    19.         newTreeView.Name = "TV2"
    20.         'Add event handler
    21.         AddHandler newTreeView.NodeMouseClick, AddressOf Me.myTree_NodeMouseClick
    22.         'Add to the list
    23.         MyTreeViews.Add(newTreeView)
    24.  
    25.         '... you can add as many as you want.
    26.  
    27.     End Sub
    28.  
    29.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    30.  
    31.         'Don't forget to remove the handles, because the .Net will not for you.
    32.         For Each tv As TreeView In myTreeViews
    33.             RemoveHandler tv.NodeMouseClick, AddressOf Me.myTree_NodeMouseClick
    34.             tv.Dispose()
    35.         Next
    36.         myTreeViews.Clear()
    37.     End Sub
    38.  
    39.     'This will handle all the TreeViews' NodeMouseClicks
    40.     Private Sub myTree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs)
    41.         'A TreeView node was clicked, do some thing
    42.     End Sub
    43. End Class

  9. #9

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: [2005] Treeview Events

    :drool: wow... thanks... that worked great... you are the man..



    Thanks alot!

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [RESOLVED] [2005] Treeview Events

    Good, I am glad to be help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width