MenBh MenText Bhparent
0001 文件
0002 编辑
0005 打开 0001
0006 新建 0001
0011 access数据库 0006
0012 VFP的Dbf 0006
0013 剪切 0002
0014 复制 0002
0015 完全复制 0014
Private m As New MainMenu()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection("server=localhost;uid=sa;pwd=;database=jiang")
Dim cmd As New SqlCommand("select * from a_menu ", conn)
Dim ds As New DataSet()
Dim sqldpr As New SqlDataAdapter(cmd)
sqldpr.Fill(ds, "menu")
Me.DataGrid1.DataSource = ds.Tables("menu")
'创建关系
Dim dr As New DataRelation("self_menu", ds.Tables("menu").Columns("MenBh"), ds.Tables("menu").Columns("Bhparent"))
ds.Relations.Add(dr)
Dim r1 As DataRow
'查找最高的菜单级别,也就是Bhparent列为空的行
For Each r
If r1.IsNull("Bhparent") Then
addmenuitem(r1, Nothing)
End If
Next
Me.Menu = m '指定主窗体菜单
End Sub
'递归函数
Private Sub addmenuitem(ByVal r As DataRow, ByVal item As MenuItem)
Dim mi As MenuItem
'如果是最外层菜单,要直接添加
If item Is Nothing Then
mi = m.MenuItems.Add(r.Item("MenText"))
Else '如果是下级菜单要在菜单项的上级添加
mi = item.MenuItems.Add(r.Item("MenText"))
End If
Dim r2 As DataRow
For Each r
addmenuitem(r2, mi)
Next
End Sub
'--------------------------------------------------------一种方法的改进
‘/////////////////////////////////////////////////////////////////////////////////////
‘继承自menuitem的类
‘/////////////////////////////////////////////////////////////////////////////////////
Public Class mymenuitem
Inherits System.Windows.Forms.MenuItem
Public Sub New(ByVal s As String, ByVal tag As String)
MyBase.New()
Me.Text = s
m_tag = tag
End Sub
Private m_tag As String
Public Property tag() As String
Get
Return m_tag
End Get
Set(ByVal Value As String)
m_tag = Value
End Set
End Property
End class
‘////////////////////////////////////////////////////////////////////////////////////
‘在代码中实现这种菜单项的添加和菜单的建立
‘//////////////////////////////////////////////////////////////////////////////////
Private m As New MainMenu()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection("server=localhost;uid=sa;pwd=;database=jiang")
Dim cmd As New SqlCommand("select * from a_menu ", conn)
Dim ds As New DataSet()
Dim sqldpr As New SqlDataAdapter(cmd)
sqldpr.Fill(ds, "menu")
'Me.DataGrid1.DataSource = ds.Tables("menu")
Me.DataGrid1.DataSource = ds
Me.DataGrid1.DataMember = "menu"
'创建关系
Dim dr As New DataRelation("self_menu", ds.Tables("menu").Columns("MenBh"), ds.Tables("menu").Columns("Bhparent"))
ds.Relations.Add(dr)
Dim r1 As DataRow
'查找最高的菜单级别,也就是Bhparent列为空的行
For Each r
If r1.IsNull("Bhparent") Then
addmenuitem(r1, Nothing)
End If
Next
Me.Menu = m '指定主窗体菜单
End Sub
'递归函数
Private Sub addmenuitem(ByVal r As DataRow, ByVal item As MenuItem)
Dim mi As MenuItem
'如果是最外层菜单,要直接添加
If item Is Nothing Then
Try
mi = New mymenuitem(r.Item("MenText"), "1")
m.MenuItems.Add(mi)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Else '如果是下级菜单要在菜单项的上级添加
mi = New mymenuitem(r.Item("MenText"), "2")
item.MenuItems.Add(mi)
End If
Dim r2 As DataRow
For Each r
addmenuitem(r2, mi)
Next
End Sub