测试可用的代码:
#region 右键快捷菜单单击事件 private void contextMenuStrip1_ItemClick(object sender, EventArgs e) { ToolStripItem item = (ToolStripItem)sender; Clipboard.SetText(item.Text); //把要插入的文本放到剪切板 SendKeys.Send("^v"); // 模拟ctrl+v } #endregion //写右键快捷菜单控件 for (int i = 0; i < DataClass.ClassCustomParameter.intRowCountCustomParmeter; i++) { ToolStripMenuItem menuitem; menuitem = new ToolStripMenuItem(); menuitem.Text = DataClass.ClassCustomParameter.strCustomParameter[i, 0]; menuitem.Name = "tllStripMenu" + i; menuitem.Click += new EventHandler(contextMenuStrip1_ItemClick); contextMenuStrip1.Items.Add(menuitem); } //各控件绑定右键快捷菜单 textBoxCaseReason.ContextMenuStrip = contextMenuStrip1;
【转】ContextMenuStrip菜单应用
ContextMenuStrip 控件提供了与某个控件关联的快捷菜单。也就是说通过这个控件可以方便的实现在别的控件上弹出右键快捷菜单。
例子:
1、先添加一个ContextMenuStrip 控件到Form上
2、设置目标控件(如窗体、文本框等)的ContextMenuStrip属性,指向你刚才添加的ContextMenuStrip 控件。这样就实现了两者的关联。
3、动态将一些菜单项添加到 ContextMenuStrip 控件中,如:
//上下文菜单项
ContextMenuStrip1.Items.Add(msEditNew);
ContextMenuStrip1.Items.Add(msEditBar1);
ContextMenuStrip1.Items.Add(msEditCut);
ContextMenuStrip1.Items.Add(msEditPaste);
ContextMenuStrip1.Items.Add(msEditDelete);
ContextMenuStrip1.Items.Add(msEditBar2);
这样就OK了,你在目标控件上按右键时将弹出快捷菜单。
动态添加ContextMenuStrip项(ToolStripItem)
//绑定菜单 |
动态生成ContextMenuStrip
数据表:
代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication25 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 窗体加载事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { ToolStripMenuItem tmpItem = new ToolStripMenuItem("移动到"); this.contextMenuStrip1.Items.Add(tmpItem); CreateContextMenuStrip(tmpItem.DropDownItems, GetTable(), 1); } /// <summary> /// ContextMenuStrip中DropDownItem单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void MenuClicked(object sender, EventArgs e) { MessageBox.Show((sender as ToolStripMenuItem).Text); } /// <summary> /// 递归生成ContextMenuStrip /// </summary> /// <param name="items"></param> /// <param name="dt"></param> /// <param name="parentId"></param> public void CreateContextMenuStrip(ToolStripItemCollection items, DataTable dt, int parentId) { if (dt.Rows.Count == 0) return; string fliter = string.Format("ParentGroup={0}", parentId); DataRow[] dr = dt.Select(fliter); foreach (DataRow row in dr) { ToolStripMenuItem menuitem; menuitem = new ToolStripMenuItem(); menuitem.Text = row["GroupName"].ToString(); menuitem.Name = row["ID"].ToString(); menuitem.Click += new EventHandler(MenuClicked); items.Add(menuitem); CreateContextMenuStrip(menuitem.DropDownItems, dt, Convert.ToInt32(row["ID"])); dt.Rows.Remove(row); } } /// <summary> /// 获取数据源 /// </summary> /// <returns></returns> DataTable GetTable() { SqlDataAdapter da = new SqlDataAdapter("select * from ReportGroup", "server=(local);uid=sa;pwd=123;database=ExcelMaster;"); DataTable dt = new DataTable(); da.Fill(dt); return dt; } } }
结果: