You are on page 1of 4

[Visual Basic]

Public Sub CreateMyMainMenu()


' Create an empty MainMenu.
Dim mainMenu1 As New MainMenu()

Dim menuItem1 As New MenuItem()


Dim menuItem2 As New MenuItem()

menuItem1.Text = "File"
menuItem2.Text = "Edit"
' Add two MenuItem objects to the MainMenu.
mainMenu1.MenuItems.Add(menuItem1)
mainMenu1.MenuItems.Add(menuItem2)

' Bind the MainMenu to Form1.


Menu = mainMenu1
End Sub
--------------------
Private Sub InitializeMyMainMenu()
' Create the MainMenu and the MenuItem to add.
Dim mainMenu1 As New MainMenu()
Dim menuItem1 As New MenuItem("&File")

' Use the MenuItems property to call the Add method


' to add the MenuItem to the MainMenu menu item collection.
mainMenu1.MenuItems.Add(menuItem1)

' Assign mainMenu1 to the form.


Me.Menu = mainMenu1
End Sub
---------------------
Public Sub CreateMyMainMenu()
' Create two MenuItem objects and assign to array.
Dim menuItem1 As New MenuItem()
Dim menuItem2 As New MenuItem()

menuItem1.Text = "&File"
menuItem2.Text = "&Edit"

' Create a MainMenu and assign MenuItem objects.


Dim mainMenu1 As New MainMenu(New MenuItem() {menuItem1, menuItem2})

' Bind the MainMenu to Form1.


Menu = mainMenu1
End Sub
----------------------------
Public Sub AddMenu()
Dim mnuFileMenu as New MainMenu
Me.Menu = mnuFileMenu

End Sub
-
Public Sub AddMenuAndItems()
Dim mnuFileMenu as new MainMenu
Me.Menu = mnuFileMenu
Dim myMenuItemFile as New MenuItem("&File")
Dim myMenuItemNew as New MenuItem("&New")

mnuFileMenu.MenuItems.Add(myMenuItemFile)
myMenuItemFile.MenuItems.Add(myMenuItemNew)

myMenuItemNew.MenuItems.Add(myMenuItemFolder)

mnuFileMenu.MenuItems.Add("Save &As")
End Sub
----------------
MenuItem.Click Event
Public Delegate Sub EventHandler( _
ByVal sender As Object, _
ByVal e As EventArgs _
)

---------------------------
Public Sub CloneMyMenu()
' Determine if mainMenu1 is currently hosted on the form.
If Not (mainMenu1.GetForm() Is Nothing) Then
' Create a copy of the MainMenu that is hosted on the form.
Dim mainMenu2 As MainMenu = mainMenu1.CloneMenu()
' Set the RightToLeft property for mainMenu2.
mainMenu2.RightToLeft = RightToLeft.Yes
End If
End Sub

Liên kết menu ngữ cảnh vào điều khiển kiểm


Bạn cần liên kết một menu ngữ cảnh vào mỗi điều kiểm trên form (các menu này khác nhau). Tuy
nhiên, bạn không muốn viết nhiều phương thức thụ lý sự kiện riêng rẽ để hiển thị menu ngữ cảnh
cho mỗi điều kiểm.
Viết một phương thức thụ lý sự kiện chung để thu lấy đối tượng ContextMenu được kết hợp với
điều kiểm, và rồi hiển thị menu này trên điều kiểm.

Bạn có thể liên kết một điều kiểm với một menu ngữ cảnh bằng cách thiết lập thuộc tính
ContextMenu của điều kiểm. Tuy nhiên, đây chỉ là một thuận lợi—để hiển thị menu ngữ cảnh, bạn
phải thu lấy menu và gọi phương thức Show của nó. Thông thường, bạn hiện thực logic này trong
phương thức thụ lý sự kiện MouseDown.

Thực ra, logic dùng để hiển thị menu ngữ cảnh hoàn toàn giống nhau, không quan tâm đến điều
kiểm gì. Mọi điều kiểm đều hỗ trợ thuộc tính ContextMenu (được thừa kế từ lớp cơ sở Control),
nghĩa là bạn có thể dễ dàng viết được một phương thức thụ lý sự kiện chung để hiển thị các
menu ngữ cảnh cho tất cả các điều kiểm.

Ví dụ, xét một form gồm một Label, một PictureBox, và một TextBox. Bạn có thể viết một
phương thức thụ lý sự kiện MouseDown cho tất cả các đối tượng này. Đoạn mã dưới đây kết nối
tất cả các sự kiện này vào một phương thức thụ lý sự kiện tên là Control_MouseDown:

this.label1.MouseDown += new MouseEventHandler(this.Control_MouseDown);


this.pictureBox1.MouseDown += new
MouseEventHandler(this.Control_MouseDown);
this.textBox1.MouseDown += new MouseEventHandler(this.Control_MouseDown);
Phần mã thụ lý sự kiện hoàn toàn được dùng chung. Nó chỉ ép kiểu sender thành Control, kiểm
tra menu ngữ cảnh, và hiển thị nó.

private void Control_MouseDown(object sender,


System.Windows.Forms.MouseEventArgs e) {

if (e.Button == MouseButtons.Right) {

// Lấy điều kiểm nguồn.


Control ctrl = (Control)sender;

if (ctrl.ContextMenu != null) {

// Hiển thị menu ngữ cảnh.


ctrl.ContextMenu.Show(ctrl, new Point(e.X, e.Y));
}
}
}
Trích từ “Các giải pháp lập trình C#”

đệ quy duyệt toàn bộ các menu trong 1 MENUTRIP ( bất kể có bao nhiêu cấp ), bạn có thể sửa
đổi đoạn code này để tạo TREE trong tree view hay GRID , tham số DT là 1 datatable chứa các
menu mà người dùng được phép truy cập, nếu không tìm thấy thì menu đó sẽ bị disable

Private Sub ScanMenu(ByRef Menu As Object, ByRef DT As DataTable)


If Menu Is Nothing Then Return
Dim mns As MenuStrip = TryCast(Menu, MenuStrip)
'if first levels
If mns IsNot Nothing Then
For I As Integer = 0 To mns.Items.Count - 1
Dim Mi As ToolStripMenuItem = mns.Items(I)
If Mi.DropDownItems.Count > 0 Then
ScanMenu(Mi, DT)
End If
Next
Else
Dim Mi As ToolStripMenuItem = TryCast(Menu, ToolStripMenuItem)
If Mi Is Nothing Then Return
For I As Integer = 0 To Mi.DropDownItems.Count - 1
Dim subMi As ToolStripMenuItem
subMi = TryCast(Mi.DropDownItems(I), ToolStripMenuItem)
If subMi Is Nothing Then Continue For
If subMi.DropDownItems.Count > 0 Then
ScanMenu(subMi, DT)
Else

Dim X As DataRow() = DT.Select(String.Format("AccessMenu='{0}'", subMi.Text))


subMi.Visible = Not (X.Length = 0)
End If
Next
End If
End Sub

Đoạn mã của mình là dùng đệ quy để duyet toàn bô các item trong menu dùng để :
- Vẽ cây phân quyền ( tree view hoặc dùng c1 flexgrid )
- Duyet menu để phân quyen - thường là ẩn menu đó đi
sửa đổi 1 chút cho khớp với dữ liệu của bạn .
Chỉ áp dụng cho menutrip ( các menu của 3rd party khác thì cần sửa lại code 1 chút )

You might also like