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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView Problems - 2 on same form

Status
Not open for further replies.

KeyserSoze1877

Programmer
Sep 6, 2001
95
US
I have two TreeView objects on my form.

On each form I have an AfterSelect event that does a function.
When I click on the two tree views it works fine if I select different items in the trees. But... the way I use the tree if one of the trees only has one node, it will only fire off the AfterSelect event once, even if I go click on the other tree or change focus, if I click on that one node it wont fire off that AfterSelect function again.

????



---------------
Keyser Soze
"Vote Pedro.
 
Strike this, screw it, went with List Boxes instead.

---------------
Keyser Soze
"Vote Pedro.
 
Keyser:

Don't give up so easily. Maybe my past few days with treeviews will enlighten you; though, I am a novice.

Try assigning a tag to each leaf of the tree. Then use the tag to identify which branch it belongs to using the tv1.SelectedNode.FullPath and/or the tv1.SelectedNode.Text.

This example uses SQL DMO to populate a treeview with the different nodes of the server, DB, TBL, VW, and Fields of all the objects within a specified SQL Server.

Code:
Sub ConnectToNodes()

        ''Variables
        Dim oDB As SQLDMO.Database
        Dim oTbl As SQLDMO.Table
        Dim oVW As SQLDMO.View = New SQLDMO.View
        Dim oCol As SQLDMO.Column
        Dim i As Int16
        Dim s As String
        Dim nodSrvr As System.Windows.Forms.TreeNode
        Dim nodDB As System.Windows.Forms.TreeNode
        Dim nodTbl As System.Windows.Forms.TreeNode
        Dim nodTbls As System.Windows.Forms.TreeNode
        Dim nodVW As System.Windows.Forms.TreeNode
        Dim nodVWs As System.Windows.Forms.TreeNode
        Dim nodFld As System.Windows.Forms.TreeNode

        Try
            Server2.Databases.Refresh()
        Catch ex As Exception
            Exit Sub
        End Try

        ''clear TV and CB
        TV1.Nodes.Clear()
        cbDB.Items.Clear()

        ''Nodes
        Server = GetDSource

        TV1.BeginUpdate()
        nodDB = New System.Windows.Forms.TreeNode
        nodDB = TV1.Nodes.Add((Server))
        nodDB.ImageIndex = 2
        nodDB.SelectedImageIndex = 2
        nodDB.Tag = "SERVER"

        'List available databases 
        For Each oDB In Server2.Databases
            If Not oDB.SystemObject Then
                Server2.Databases.Refresh()

                'Try
                ''Add DB
                nodDB = New System.Windows.Forms.TreeNode
                s = oDB.Name
                nodDB = TV1.Nodes(0).Nodes.Add((s))
                nodDB.Tag = "DB" '& nodDB.Index
                nodDB.ImageIndex = 3
                nodDB.SelectedImageIndex = 3
                'nodDB.Tag = "DB"

                ''Add Tables
                nodTbl = New System.Windows.Forms.TreeNode
                nodTbl = nodDB.Nodes.Add("Tables")
                nodTbl.Tag = "Tables"
                nodTbl.ImageIndex = 0
                nodTbl.SelectedImageIndex = 1
                'nodDB.Tag = "TBLS"

'''use the DMO collections
                For Each oTbl In oDB.Tables
                    If Not oTbl.SystemObject Then
                        nodTbls = New System.Windows.Forms.TreeNode
                        nodTbls = nodTbl.Nodes.Add(oTbl.Name)
                        'nodTbls.Tag = "Tables"
                        nodTbls.ImageIndex = 6
                        nodTbls.SelectedImageIndex = 6
                        nodTbls.Tag = "TBL"

                        ''Add Fields
                        For Each oCol In oTbl.Columns
                            nodFld = New System.Windows.Forms.TreeNode
                            nodFld = nodTbls.Nodes.Add(oCol.Name)
                            nodFld.ImageIndex = 9
                            nodFld.SelectedImageIndex = 9
                            nodFld.Tag = "FLD"
                        Next
                    End If
                Next oTbl

                ''Add Views
                nodVWs = New System.Windows.Forms.TreeNode
                nodVWs = nodDB.Nodes.Add("Views")
                nodVWs.Tag = "Views"
                nodVWs.ImageIndex = 0
                nodVWs.SelectedImageIndex = 1
                For Each oVW In oDB.Views
                    If Not oVW.SystemObject Then
                        nodVW = New System.Windows.Forms.TreeNode
                        nodVW = nodVWs.Nodes.Add(oVW.Name)
                        nodVW.Tag = "View"
                        nodVW.ImageIndex = 12
                        nodVW.SelectedImageIndex = 12
                        'MsgBox(oVW.Script())
                        ''Add Fields
                        For Each oCol In oVW.ListColumns
                            nodFld = New System.Windows.Forms.TreeNode
                            nodFld = nodVW.Nodes.Add(oCol.Name)
                            nodFld.Tag = "FLD"
                            nodFld.ImageIndex = 9
                            nodFld.SelectedImageIndex = 9
                        Next
                    End If
                Next oVW

                'Catch ex As Exception
                ''Write any errors here
                'txtErr.Text += oDB.Name & "-" & ex.Message & vbCrLf
                'End Try
            End If
        Next oDB
        Server2.Databases.Refresh()
        TV1.Nodes(0).Expand()
        TV1.EndUpdate()

    End Sub

I hope this helps.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top