具体分5步
1:定义一个ContextMenuStrip对象。
2:用withevents关键字声明一个ContextMenu对象Private WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu。
3:对ContextMenu对象进行初始化。
4:将ContextMenu对象加入ContextMenu对象。
5:将ContextMenu对象与form对象绑定。
例子:‘添加三个右键菜单,并每个菜单都添加两个子菜单,把右键菜单绑定到当前窗体
Private cms As ContextMenuStrip = New ContextMenuStrip() Dim i As Integer For i = 1 To 3 Dim tsm As ToolStripMenuItem = New ToolStripMenuItem(i.ToString()) Dim j As Integer For j = 1 To 2 Dim tsmz As ToolStripMenuItem = New ToolStripMenuItem(j.ToString()) tsm.DropDown.Items.Add(tsmz) Next cms.Items.Add(tsm) Next Me.ContextMenuStrip = cms
例子:http://bbs.csdn.net/topics/350192775
Dim cm As New ContextMenu Dim m As New MenuItem("菜单项1") AddHandler m.Click,AddressOf MenuItemClicked cm.MenuItems.Add(m) Dim m2 As New MenuItem("菜单项2") AddHandler m2.Click,AddressOf MenuItemClicked cm.MenuItems.Add(m2) Me.ContextMenu = cm '设置窗体右键菜单绑定事件:
Public Sub MenuItemClicked(ByVal sender As Object,ByVal e As EventArgs) Dim m As MenuItem = DirectCast(sender,MenuItem) MessageBox.Show(m.Text) End Sub
读取菜单值:
建议把sender对象强制转换为菜单,你就可以任意访问该菜单对象的属性了。例如Text,例如Tag属性等。
DimmenuItemAsToolStripMenuItem=DirectCast(sender,ToolStripMenuItem)
DimtempAsString=menuItem.Text
Dim tag as object = menuItem.tag
'详细一点的:仅供参考!http://s.yanghao.org/program/viewdetail.php?i=51247
设置菜单的图标,是否合并,等。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ContextMenuStripDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void InitListBox() {//添加ListBox的项 for (int i = 0; i < 11; i++) { this.listBox1.Items.Add(i); this.listBox2.Items.Add(i); } } private void InitContextMenuStrip() { ContextMenuStrip contextMenuStrip = new ContextMenuStrip();//构造一个ContextMenuStrip contextMenuStrip.AllowMerge = false;//设置不能跟其他菜单合并 ImageList imagelist = new ImageList();//设置图像集合 imagelist.Images.Add(new Icon("delete.ico")); contextMenuStrip.ImageList = imagelist; contextMenuStrip.ShowCheckMargin = false; contextMenuStrip.ShowImageMargin = true;//显示图标在左边 contextMenuStrip.ShowItemToolTips = true;//显示工具提示 contextMenuStrip.TextDirection = ToolStripTextDirection.Horizontal;//水平显示文本 contextMenuStrip.opening += new CancelEventHandler(contextMenuStrip_opening); this.listBox1.ContextMenuStrip = contextMenuStrip;//绑定到ListBox1控件 this.listBox2.ContextMenuStrip = contextMenuStrip;//绑定到ListBox2控件 } void contextMenuStrip_opening(object sender,CancelEventArgs e) { ContextMenuStrip contextMenuStrip = sender as ContextMenuStrip; if (contextMenuStrip != null) {//第一次点击右键时,如果没有菜单项就添加 if (contextMenuStrip.Items.Count == 0) { ToolStripMenuItem itemDelete = new ToolStripMenuItem(); itemDelete.Text = "删除"; itemDelete.ImageIndex = 0; itemDelete.Click += new EventHandler(itemDelete_Click); contextMenuStrip.Items.Add(itemDelete);//增加菜单项 } } } void itemDelete_Click(object sender,EventArgs e) { ToolStripMenuItem Menuitem = sender as ToolStripMenuItem; ContextMenuStrip contextMenuStrip = Menuitem.Owner as ContextMenuStrip;//获取父菜单对象 if (contextMenuStrip != null) {//通过SourceControl获取目标控件 ListBox listBox = contextMenuStrip.SourceControl as ListBox; if (listBox != null && listBox.SelectedIndex>-1) { listBox.Items.RemoveAt(listBox.SelectedIndex); } } } private void Form1_Load(object sender,EventArgs e) { InitContextMenuStrip(); InitListBox(); } } }原文链接:https://www.f2er.com/vb/259047.html