Memadukan komponen MenuStrip dan ExplorerBar Control for .NET
2 komponen ini merupakan komponen favorit saya untuk kelengkapan setiap aplikasi yang saya buat, dimana komponen MenuStrip digunakan untuk menampilkan menu bergaya klasik.
dan komponen ExplorerBar Control for .NET menampilkan menu dengan ciri khas menu yg ada di Windows Explorernya XP
jadi dengan asumsi bahwa Anda sudah pernah membuat menu dengan MenuStrip dan ExplorerBar Control for .NET, maka fokus kita kali ini adalah pada poin-poin berikut :
1. Membuat menu ExplorerBar Control for .NET berdasarkan menu MenuStrip
Tentunya agar poin 1 ini berjalan dengan baik, maka kita harus mempersiapkan terlebih dulu item menu stripnya sekalian menambahkan komponen ExplorerBar Control for .NET ke form.
berhubung persiapan sudah siap, kita masuk kebagian kode
Private Function SetBarMenu(ByVal explorerBar As acclExplorerBar, ByVal menuName As String, ByVal menuCaption As String, ByVal iconIndex As Integer) As explorerBar
Dim barMenu As explorerBar
barMenu = New explorerBar
With barMenu
.IsSpecial = True
.iconIndex = iconIndex
.Text = menuCaption
.Tag = menuName
End With
Return barMenu
End Function
Private Function SetItemMenu(ByVal explorerBar As acclExplorerBar, ByVal menuName As String, ByVal menuCaption As String, ByVal iconIndex As Integer) As ExplorerBarLinkItem
Dim barItem As ExplorerBarLinkItem
barItem = New ExplorerBarLinkItem
With barItem
.iconIndex = iconIndex
.Text = menuCaption
.Tag = menuName ' ini berguna untuk menghandle menu yang diklik
End With
Return barItem
End Function
Private Sub AddMenu()
Dim barMenu As explorerBar
Dim barItems As ExplorerBarItemCollection
Dim barItem As ExplorerBarLinkItem
Try
With AcclExplorerBar1
.DrawingStyle = ExplorerBarDrawingStyle.Custom
.BackColorStart = Color.FromArgb(100, 149, 237)
.BackColorEnd = Color.FromArgb(100, 149, 237)
' ulang sebanyak menu induk (Master, Transaksi, Biaya dan Laporan)
For i As Integer = 0 To MenuStrip.Items.Count - 1
' menambahkan menu induk ke barItems
barMenu = Me.SetBarMenu(AcclExplorerBar1, MenuStrip.Items(i).Name, MenuStrip.Items(i).Text, 0)
barItems = barMenu.Items
' menampilkan menu anak berdasarkan menu induk
For Each itm As Object In CType(MenuStrip.Items(i), ToolStripMenuItem).DropDownItems
' perlu divalidasi khusus menu Separator, klo enggak akan menyebabkan error
' pada saat konversi dari tipe ToolStripItemCollection ke ToolStripMenuItem
If Not (Strings.InStr(1, itm.ToString(), "ToolStripSeparator") > 0) Then
Dim itmMenu As ToolStripMenuItem = CType(itm, ToolStripMenuItem)
' menambahkan menu anak ke barItems
barItem = Me.SetItemMenu(AcclExplorerBar1, itmMenu.Name, itmMenu.Text, 0)
barItems.Add (barItem)
End If
Next
.Bars.Add (barMenu)
Next
End With
' set warna menu bar (master, transaksi, biaya, laporan)
For Each bar As ExplorerBar In AcclExplorerBar1.Bars
bar.TitleForeColor = Color.FromArgb(0, 0, 128)
bar.TitleForeColorHot = Color.FromArgb(0, 0, 128)
bar.TitleBackColorStart = Color.FromArgb(234, 241, 253)
bar.TitleBackColorEnd = Color.FromArgb(100, 149, 237)
bar.BackColor = Color.FromArgb(176, 196, 222)
' set warna menu item (barang, customer, supplier, dll)
For Each itm As ExplorerBarItem In bar.Items
Dim textItem As ExplorerBarLinkItem = CType(itm, ExplorerBarLinkItem)
textItem.ForeColor = Color.FromArgb(0, 0, 128)
textItem.ForeColorHot = Color.White
Next
Next
Catch ex As Exception
Debug.Print (ex.Message)
End Try
End Sub
Selanjutnya tinggal panggil method AddMenu diatas di event Form_Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call AddMenu
Me.BackColor = Color.FromArgb(100, 149, 237)
End Sub
Ada yg kelupaan , tambahkan juga Namespace vbAccelerator (ingat posisinya paling atas)
Imports vbAccelerator.Components.Controls
contoh output yg dihasilkan dari kode diatas :
Coba saja Anda tambahkan menu baru di MenuStrip maka otomatis akan nambah juga dimenu ExplorerBar nya, gimana ? Gampangkan :)
2. Membatasi menu yang ditampilkan ExplorerBar Control for .NET
Misalkan ada beberapa menu yang tidak ingin kita tampilkan di menu ExplorerBar Control for .NET, kita ambil contoh menu Barang (mnuBarang), Retur Pembelian (mnuReturPembelian), Retur Penjualan (mnuReturPenjualan) dan Gaji Karyawan (mnuGajiKaryawan).
Maka yg perlu kita lakukan ada melakukan sedikit revisi di method AddMenu
Private Sub AddMenu()
Dim barMenu As explorerBar
Dim barItems As ExplorerBarItemCollection
Dim barItem As ExplorerBarLinkItem
' daftar menu yg tidak perlu ditampilkan di menu ExplorerBar Control for .NET
Dim abaikanMenuIni As String = "mnuBarang,mnuReturPembelian,mnuReturPenjualan,mnuGajiKaryawan"
Try
With AcclExplorerBar1
.DrawingStyle = ExplorerBarDrawingStyle.Custom
.BackColorStart = Color.FromArgb(100, 149, 237)
.BackColorEnd = Color.FromArgb(100, 149, 237)
' ulang sebanyak menu induk (Master, Transaksi, Biaya dan Laporan)
For i As Integer = 0 To MenuStrip.Items.Count - 1
' menambahkan menu induk ke barItems
barMenu = Me.SetBarMenu(AcclExplorerBar1, MenuStrip.Items(i).Name, MenuStrip.Items(i).Text, 0)
barItems = barMenu.Items
' menampilkan menu anak berdasarkan menu induk
For Each itm As Object In CType(MenuStrip.Items(i), ToolStripMenuItem).DropDownItems
' perlu divalidasi khusus menu Separator, klo enggak akan menyebabkan error
' pada saat konversi dari tipe ToolStripItemCollection ke ToolStripMenuItem
If Not (Strings.InStr(1, itm.ToString(), "ToolStripSeparator") > 0) Then
Dim itmMenu As ToolStripMenuItem = CType(itm, ToolStripMenuItem)
' validasi menu yang ditampilkan disini
If Not (Strings.InStr(1, abaikanMenuIni, itmMenu.Name) > 0) Then
' menambahkan menu anak ke barItems
barItem = Me.SetItemMenu(AcclExplorerBar1, itmMenu.Name, itmMenu.Text, 0)
barItems.Add (barItem)
End If
End If
Next
.Bars.Add (barMenu)
Next
End With
' set warna menu bar (master, transaksi, biaya, laporan)
For Each bar As ExplorerBar In AcclExplorerBar1.Bars
bar.TitleForeColor = Color.FromArgb(0, 0, 128)
bar.TitleForeColorHot = Color.FromArgb(0, 0, 128)
bar.TitleBackColorStart = Color.FromArgb(234, 241, 253)
bar.TitleBackColorEnd = Color.FromArgb(100, 149, 237)
bar.BackColor = Color.FromArgb(176, 196, 222)
' set warna menu item (barang, customer, supplier, dll)
For Each itm As ExplorerBarItem In bar.Items
Dim textItem As ExplorerBarLinkItem = CType(itm, ExplorerBarLinkItem)
textItem.ForeColor = Color.FromArgb(0, 0, 128)
textItem.ForeColorHot = Color.White
Next
Next
Catch ex As Exception
Debug.Print (ex.Message)
End Try
End Sub
revisinya ada di baris 7, 30-34
tuh kan beberapa menu kanannya udah lenyap.
3. Memanggil event handler MenuStrip dari ExplorerBar Control for .NET
Akhirnya nyampe juga dibagian yg saya tunggu2x, soalnya udah laper banget jadi klo postingan ini selesai bisa langsung sarapan pagi .
Klik ganda komponen ExplorerBarnya kemudian pindah ke event ItemClick
Private Sub AcclExplorerBar1_ItemClick(ByVal sender As Object, ByVal args As vbAccelerator.Components.Controls.ExplorerBarItemClickEventArgs) Handles AcclExplorerBar1.ItemClick
Dim itmMenu As ExplorerBarLinkItem = CType(args.Item, ExplorerBarLinkItem)
If itmMenu.Tag IsNot Nothing Then
For i As Integer = 0 To MenuStrip.Items.Count - 1
For Each itm As Object In CType(MenuStrip.Items(i), ToolStripMenuItem).DropDownItems
' jangan lupa divalidasi menu Separator pada saat pengecekan menu anak
If Not (Strings.InStr(1, itm.ToString(), "ToolStripSeparator") > 0) Then
If Strings.InStr(1, CType(itm, ToolStripMenuItem).Name, itmMenu.Tag.ToString()) > 0 Then
CType(MenuStrip.Items(i), ToolStripMenuItem).DropDownItems(itmMenu.Tag.ToString()).PerformClick()
Return
End If
End If
Next
Next
End If
End Sub
Selamat MENCOBA
Comments