Kamis, 16 Desember 2010

Create and modify menu items in VB.NET

Manipulating menu items in runtime

You can dynamically manipulate menu items in runtime. This may be necessary if you need to display a particular set of menus in response to a particular action performed by a user of the application. Additionally, it may be useful to disable menus in response to an error. You may also choose to add, hide, or disable menus in runtime, as well as display check marks next to them.

sample code A

��� Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
������� SetMenus()
��� End Sub
��� Private Sub SetMenus()
������� Dim mnuMenuItem1 As MenuItem, mnuMenuItem2 As MenuItem, mnuMenuItem3 As MenuItem


������� mnuMenuItem1 = New MenuItem("Item 1", New EventHandler(AddressOf mnuMenuItem1_Click))
������� MainMenu1.MenuItems.Add(mnuMenuItem1)


������� mnuMenuItem2 = New MenuItem("Item 2")
������� MainMenu1.MenuItems.Add(mnuMenuItem2)


������� mnuMenuItem3 = New MenuItem("Item 3", New EventHandler(AddressOf mnuMenuItem3_Click))
������� mnuMenuItem3.Checked = True
������� mnuMenuItem2.MenuItems.Add(mnuMenuItem3)


��� End Sub


��� Private Sub mnuMenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
������� MessageBox.Show("Menu Item 1 Clicked")
��� End Sub


��� Private Sub mnuMenuItem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
������� MessageBox.Show("Menu Item 3 Clicked")
��� End Sub


atau menggunakan kode
<code>
   Private Sub Form1_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load

      MainMenu1.MenuItems.Add("Item 1", _
         New EventHandler(AddressOf ClickHandler))
      MainMenu1.MenuItems.Add("Item 2", _
         New EventHandler(AddressOf ClickHandler))
      MainMenu1.MenuItems.Add("Item 3", _
         New EventHandler(AddressOf ClickHandler))

   End Sub

   Public Sub ClickHandler(ByVal sender As Object, _
                           ByVal e As System.EventArgs)

      MessageBox.Show("Menu '" & sender.Text.ToString() & "' clicked")

   End Sub
</code>

Tidak ada komentar:

Posting Komentar