Winform 树型菜单例子

核心代码:

 

Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private ds As New DataSet()

    ' AddTree递归函数每次都要用到数据集中的一个表,所以定义成private
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.ImageIndex = 0
        Me.TreeView1.ImageList = Me.ImageList1
        Me.TreeView1.Location = New System.Drawing.Point(0, 0)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = 0
        Me.TreeView1.Size = New System.Drawing.Size(272, 476)
        Me.TreeView1.TabIndex = 0
        '
        'ImageList1
        '
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList1.Images.SetKeyName(0, "")
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(288, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "打开窗体二"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(288, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(75, 23)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(376, 488)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TreeView1)
        Me.Name = "Form1"
        Me.Text = "用存储过程加载树"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' '定义数据库连接

        Dim CN As New SqlConnection()
        Try

            '初始化连接字符串
            CN.ConnectionString = "data source =255.255.255.255;initial catalog=baobiaopri;persist security info=False;user id=sa;Password=admin@sa;"
            CN.Open()
            '添加命令,从数据库中得到数据
            Dim sqlCmd As New SqlCommand()

            sqlCmd.Connection = CN
            Dim sql As String = ""
            'sql = "select '1' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '业务报表' as description ,  0 as parentid"
            'sql = sql + " union  all "
            'sql = sql + " select '2' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '处理过的表单' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '3' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '我提交的表单' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '4' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '其他可访问的表单' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '5' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '任务夹' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '6' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '子子菜单' as description ,  1002 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '7' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '子子菜单' as description ,  6 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '8' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '子子子菜单' as description ,  7 as parentid"
            'sql = sql + " union all"

            sql = sql + " select Privileges.PrivilegeID, Privileges.formname , Privileges.description, Privileges.ParentID  "
            sql = sql + " from Privileges inner join User_Privileges  on Privileges.Privilegeid = User_Privileges.Privilegeid"
            sql = sql + " where User_Privileges.userid = 'yinqingyue' order by Privileges.PrivilegeID"

            sqlCmd.CommandText = sql
            'sqlCmd.CommandText = "select * from tbtree"


            sqlCmd.CommandType = CommandType.Text

            Dim adp As SqlDataAdapter = New SqlDataAdapter(sqlCmd)

            adp.Fill(ds)

        Catch ex As Exception

            MsgBox(ex.Message)

        Finally
            CN.Close()
        End Try

        '调用递归函数,完成树形结构的生成
        AddTree(0, Nothing)
    End Sub

    '递归添加树的节点
    Private Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode)
        Dim Node As TreeNode
        Dim dvTree As New DataView()
        dvTree = New DataView(ds.Tables(0))

        '过滤ParentID,得到当前的所有子节点
        dvTree.RowFilter = "PARENTID = " + ParentID.ToString

        Dim Row As DataRowView
        For Each Row In dvTree
            If pNode Is Nothing Then  '判断是否根节点
                '添加根节点
                'Node = TreeView1.Nodes.Add(Row("context").ToString())
                Node = TreeView1.Nodes.Add(Row("Description").ToString())

                '再次递归
                AddTree(Int32.Parse(Row("PrivilegeID").ToString()), Node)
            Else
                '添加当前节点的子节点
                Node = pNode.Nodes.Add(Row("Description").ToString())
                '再次递归
                AddTree(Int32.Parse(Row("PrivilegeID").ToString()), Node)
            End If
            Node.EnsureVisible()
        Next
    End Sub
End Class

posted @ 2008-12-29 15:18  寒天飞雪  阅读(1340)  评论(0编辑  收藏  举报