Dim MenuView1 As New MenuView()
For i = 0 To dt.Rows.Count - 1
Dim MenuNode1 As New MenuNode()
MenuNode1.ID = dt.Rows(i)("ID")
MenuNode1.ParentID = dt.Rows(i)("ParentID")
MenuNode1.Text = Trim(dt.Rows(i)("text"))
If Not IsDBNull(dt.Rows(i)("AccessKey")) Then
MenuNode1.AccessKey = dt.Rows(i)("AccessKey")
End If
If Not IsDBNull(dt.Rows(i)("ToolTip")) Then
MenuNode1.ToolTip = dt.Rows(i)("ToolTip")
End If
If Not IsDBNull(dt.Rows(i)("ImageUrl")) Then
MenuNode1.ImageUrl = "tree\" + dt.Rows(i)("ImageUrl")
End If
MenuNode1.Enabled = dt.Rows(i)("Enabled")
MenuView1.AddNode(MenuNode1)Next
10.在菜单控件的OnChange事件中编写你所选择菜单要完成的功能。
Private Sub MenuView1_Change(ByVal sender As Object,
ByVal e As System.EventArgs)Handles MenuView1.Change
Dim ID As Int16
ID = Me.MenuView1.SelectIndex
Select Case ID
Case 1 ' 首页
Case 4 'asp.net
Case 5 'vb.net
Case 6 'c#
End Select
End Sub
'生成表格 (row*col)
Private Function CreateTable(ByVal row As Int16, ByVal col As Int16) As Table
Dim Table1 As New Table()
Table1.BackImageUrl = Me.BackGround
Dim i, j As Int16
For i = 0 To row - 1
Dim r As New TableRow()
r = Me.CreateTableRow(col)
Table1.Rows.Add(r)
Next
Table1.Attributes("onmouseover") = "this.style.cursor='hand'"
Return Table1
End Function
'建立菜单项
Private Function CreateMenuItem(ByVal item As MenuNode) As TableRow
Dim TableRow1 As New TableRow()
TableRow1 = Me.CreateTableRow(3)
Select Case Me._Type
Case 0 To 2 'image
TableRow1.Cells(0).BackColor = IconBackColor
' TableRow1.Cells(0).Text = "√"
' Case 1 'check
Case 3 '
End Select
'填充数据
'第一列
'是否有图片,有则生成图片对象,否则生成Label代替
If item.ImageUrl <> "" Then
TableRow1.Cells(0).Controls.Add(Me.CreateImage(item))
End If
TableRow1.Cells(0).Width = Unit.Pixel(22)
'第二列
Me.SetCellStyle(TableRow1.Cells(1), item)
'第三列
TableRow1.Cells(2).Width = Unit.Pixel(30)
If Me.HasSub(item) Then
TableRow1.Cells(2).HorizontalAlign = HorizontalAlign.Right
Me.SetCellStyle(TableRow1.Cells(2), item)
TableRow1.Cells(2).Text = "▶"
End If
Return TableRow1
End Function
'是否有热键,返回有热键的文本
Private Function GetHotKeyText(ByVal item As MenuNode) As String
Dim text As String
text = item.Text
If item.AccessKey <> Nothing Then
text = text + "(<U>" + item.AccessKey + "</U>)"
End If
Return text
End Function
'建立表行(1行,n列)
Private Function CreateTableRow(ByVal n As Int16) As TableRow
Dim TableRow1 As New TableRow()
Dim i As Int16
For i = 0 To n - 1
Dim c As New TableCell()
'Me.SetCellStyle(c)
TableRow1.Cells.Add(c)
Next
Return TableRow1
End Function
'当前索引放在最后一个位置
Private Function GetSubName(ByVal index As Int16, ByVal Count As Int16) As String
Dim i As Int16
Dim Names As String
For i = 0 To Count - 1
Names = Names + Me.ID + "_T" + i.ToString + "|"
Next
Names = Names + Me.ID + "_T" + index.ToString
Return Names
End Function
'由生成一个图标组件
Private Function CreateImage(ByVal item As MenuNode) As Image
Dim Image1 As New Image()
Image1.ImageUrl = item.ImageUrl
'Image1.Enabled = item.Enabled
'Image1.Visible = item.Visible
Return Image1
End Function
Private Sub TableRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim ID As String = Trim(sender.CommandArgument)
'MessageBox(ID)
_SelectIndex = CType(ID, Int16)
OnChange(e)
End Sub
Private Function HasSub(ByVal item As MenuNode) As Boolean
Dim i As Int16
For i = 1 To coll.Count
If coll.Item(i).ParentID = item.ID Then
Return True
End If
Next
Return False
End Function
'取得ID的所有子结点
Private Function GetSubNodes(ByVal ID As Int16) As MenuNode()
Dim i, j, SubCount As Int16
'计算子菜单数
For i = 1 To coll.Count
If coll.Item(i).ParentID = ID Then
SubCount = SubCount + 1
End If
Next
Dim SubMenuNodeS(SubCount) As MenuNode
'取出子菜子菜单
If SubCount > 0 Then
For i = 1 To coll.Count
If coll.Item(i).ParentID = ID Then
SubMenuNodeS(j) = coll.Item(i)
j = j + 1
End If
Next
End If
Return SubMenuNodeS
End Function
'计算子菜单数
Private Function GetSubCount() As Int16
Dim i, j, SubCount As Int16
'计算父菜单数(及面板数)
For i = 1 To coll.Count
If coll.Item(i).ParentID = 0 Or coll.Item(i).ParentID = Nothing Then
SubCount = SubCount + 1
End If
Next
Return SubCount
End Function
Private Function GetSpace(ByVal count As Short) As String
Dim TxtSb As New System.Text.StringBuilder()
Dim i As Short
For i = 0 To count - 1
TxtSb.Append(" ")
Next
Return TxtSb.ToString
End Function
Private Function GetColorValue(ByVal color1 As System.Drawing.Color) As String
Dim ColorValue As String
Dim r As String = Hex(color1.R)
Dim g As String = Hex(color1.G)
Dim b As String = Hex(color1.B)
If r.Length = 1 Then
r = "0" + r
End If
If g.Length = 1 Then
g = "0" + g
End If
If b.Length = 1 Then
b = "0" + b
End If
ColorValue = r + g + b
Return ColorValue
End Function
'把结点增加到集合中
Public Sub AddNode(ByVal item As MenuNode)
'Dim Form1 = Me.Parent.FindControl(Me.Parent.ID)
'' If Form1 Then
'If Not Form1.IsPostBack Then
coll.Add(item)
'End If
End Sub
Private Sub MessageBox(ByVal Text As String)
Dim Txt As String = "<script>alert('" + Text + "');</script>"
Me.Page.Response.Write(Txt)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
第四部分:
递归生成子菜单
Private Sub CreateSubMenu(ByVal ID As Int16)
Dim Nodes() As MenuNode
Nodes = Me.GetSubNodes(ID)
Dim Count As Int16 = Nodes.Length - 1
Dim TableRow(Count) As TableRow
'计算父菜单数(及面板数),子菜单数
Dim Table1 As New Table()
Me.SetTableStyle(Table1)
Dim i As Int16
For i = 0 To Count - 1
Dim Node As New MenuNode()
Node = Nodes(i)
TableRow(i) = New TableRow()
TableRow(i) = Me.CreateMenuItem(Node)
End If
' button1.Attributes("href") = "/"
End If
TableRow(i).Attributes("onmouseout") = "this.style.background='" + Me.GetColorValue(Me.BackColor) + "'"
Table1.Controls.Add(TableRow(i))
Next
Table1.Attributes("style") = "Z-INDEX:999;position:absolute;visibility:hidden ;BACKGROUND-COLOR:" + Me.GetColorValue(Me.BackColor)
Table1.ID = "T%" + ID.ToString 'Index.ToString
Index = Index + 1
Me.Controls.Add(Table1)
For i = 0 To Count - 1
ID = Nodes(i).ID
If Me.HasSub(Nodes(i)) Then
CreateSubMenu(ID)
End If
Next
End Sub
第三部分:
3、定义MenuView类的一些方法
'初使化菜单
Public Sub New()
Me.BackColor = System.Drawing.Color.DeepPink
Me.BorderStyle = BorderStyle.Outset
Me.Width = Unit.Pixel(250)
'Me.Width = Unit.Pixel(50)
Me.BackGround = ""
Me.BorderWidth = Unit.Pixel(0)
Me.Type = 1
End Sub
'自定义OnChange事件,该事件用来表示用户所调用菜单项所完成的功能
Public Event Change As EventHandler
'触发OnChange事件。
Protected Overridable Sub OnChange(ByVal e As EventArgs)
RaiseEvent Change(Me, e)
End Sub
' 当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
OnChange(New EventArgs())
End Sub
End Sub
'通知使用基于合成的实现的服务器控件创建它们包含的任何子控件,以便为回发或呈现做准备。
Protected Overrides Sub CreateChildControls()
'生成主菜单
Dim MainTable As New Table()
Dim MainNodes() As MenuNode
MainNodes = Me.GetSubNodes(0)
MainTable = CreateMainMenu(MainNodes)
Me.Controls.Add(MainTable)
'得到子菜单数及每个子菜单的行数
Dim subCount As Int16 = MainNodes.Length - 1
'递规生成子菜单
Dim i As Int16
For i = 0 To subCount - 1
CreateSubMenu(MainNodes(i).ID)
Next
' 透明处理
Me.Attributes("style") = Me.Attributes("style") + ";background-color: transparent "
Me.Attributes("onclick") = "OpenTable(this ,0,3 )"
Dim Form1 = cls.GetParent(Me, "HtmlForm")
Form1.Attributes("onclick") = "OpenTable('" + Me.ID + "',0,4 )"
Me.BorderWidth = Unit.Pixel(0)
'For i = 1 To Me.coll.Count
' Me.coll.Remove(1)
'Next
End Sub
'生成主菜单。由结点生成菜单体
Private Function CreateMainMenu(ByVal Nodes() As MenuNode) As Table
Dim i, j As Int16
'计算父菜单数(及面板数),子菜单数
Dim Count As Int16 = Nodes.Length - 1
Dim Table1 As New Table()
Table1 = Me.CreateTable(1, 2 * Count - 1)
Me.SetTableStyle(Table1)
'生成主表
For i = 0 To Count - 1
Dim Node As New MenuNode()
Node = Nodes(i)
Dim Cell1 As New TableCell()
j = 2 * i
Cell1 = Table1.Rows(0).Cells(j)
'是否有图片,有则生成图片对象,否则生成Label代替
If Node.ImageUrl <> "" Then
Cell1.Controls.Add(Me.CreateImage(Node))
End If
End If
Cell1.Attributes("onmouseout") = "this.style.background='" + Me.GetColorValue(Me.BackColor) + "'"
If i < Count - 1 Then
Table1.Rows(0).Cells(2 * i + 1).Width = Unit.Pixel(25)
End If
Next
If Table1.Rows(0).Cells.Count > 0 Then
Table1.Rows(0).Cells(0).ID = "Cell0"
End If
Table1.ID = "T%"
Return Table1
End Function
Public Class MenuView : Inherits WebControl
Implements INamingContainer
Implements IPostBackEventHandler
2、定义MenuView类的一些属性
'背景图片
Dim _BackGround As String
'风格类型
Dim _Type As Int16
'集合下标从1开始,用集合来存放菜单项
Public Shared coll As New Collection()
'定义菜单项的类型:图标型、检测型、检测背景型、普通型
Enum EType
IsImageBackColor = 0
IsCheckBackColor = 1
IsCheck = 2
None = 3
End Enum
'菜单表
Public Shared Index As Int16
'所选择的菜单项指针
Dim _SelectIndex As Int16
'所选择的菜单项背景色
Dim _SelectBackColor As System.Drawing.Color
'图标背景色
Dim _IconBackColor As System.Drawing.Color
Public Property SelectIndex() As Int16
Get
Return _SelectIndex
End Get
Set(ByVal Value As Int16)
End Set
End Property
Public Property SelectBorderStyle() As BorderStyle
Get
Return ViewState("_SelectBorderStyle")
End Get
Set(ByVal Value As BorderStyle)
ViewState("_SelectBorderStyle") = Value
End Set
End Property
Public Property SelectBackColor() As System.Drawing.Color
Get
Return ViewState("_SelectBackColor")
End Get
Set(ByVal Value As System.Drawing.Color)
ViewState("_SelectBackColor") = Value
End Set
End Property
Public Property IconBackColor() As System.Drawing.Color
Get
Return ViewState("_IconBackColor")
End Get
Set(ByVal Value As System.Drawing.Color)
ViewState("_IconBackColor") = Value
End Set
End Property
'
Public Property Type() As EType
Get
Return ViewState("_Type")
End Get
Set(ByVal Value As EType)
ViewState("_Type") = Value
End Set
End Property
Public Property BackGround() As String
Get
Return ViewState("_BackGround")
End Get
Set(ByVal Value As String)
ViewState("_BackGround") = Value
End Set
End Property
二、定义公共类MenuNode,该类为供应用软件调用。定义类MenuNode的私有变量_ImageUrl,该变量表示结点图片。定义类MenuNode的属性_ParentID,该属性父结点号,分别编写它们的功能。代码如下:
Public Class MenuNode : Inherits TableCell
Private _ImageUrl As String
Private _ParentID As Int16
'父结点号
Public Property ParentID() As Int16
Get
Return ViewState("_ParentID")
End Get
Set(ByVal Value As Int16)
ViewState("_ParentID") = Value
Dim a As TableCell
End Set
End Property
'结点图片
Public Property ImageUrl() As String
Get
Return ViewState("_ImageUrl")
End Get
Set(ByVal Value As String)
ViewState("_ImageUrl") = Value
End Set
End Property
End Class
public class DataSQL 'DataSQL的组件
Inherits System.ComponentModel.Component '继承Com组件
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
'We added our code for component constructor without passed parameters
privateModuleName = Me.GetType.ToString
End Sub
'Component overrides dispose to clean up the component list.
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
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
首先新生成一个类库classlibrary1,这个自动运行就可以生成一个.dll文件。然后添加一些你做的窗体,比如做一个登陆窗体,把与这个登陆窗体有关的内容都添加进来,生成解决方案没有问题,在添加一个项目调用这个dll测试是否通过就可以了。
public class form1 '项目2的一个新窗体,用于调用dll
Dim frm_new As New ClassLibrary1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm_new.Show()
End Sub
这样就可以了。