一.简单添加
(1)this.contextMenustrip1.Item.Add(“toolstripMenuitem1”)------------------添加单个菜单项
(2)this.contextMenustrip1.Item.AddRange(new System.windows.forms.ToolstripItem[]{this.toolstripMenuItem1,this.toolstripMenuItem2.....})----------添加多个菜单项
二.动态添加,同时添加对应点击事件
(1)首先实例化几个ToolStripItem(要为父级添加几个就实例化几个)
//添加子菜单 toolStripItem ts_1=new toolStripItem("里面的text文本"); //实例化一个
ToolStripItem[] Tsi = new ToolStripItem[count]; //数组
(2)将其添加到ContextMenuStrip
((ToolStripDropItem)(contextMenustrip1.item[1])).DropdownIem.Add(ts_1);
(3)在实例化ToolStripItem要为实例化的ToolStripItem对象添加新的事件,然后再添加到ContextMenuStrip控件
ts_1.click+=new EventHandler(ts_1_click);
(4)添加ts_1_click方法
private void ts_1_click(object sender,EventArgs e)
{}
eg:动态添加
private void btPrint_Click(object sender, EventArgs e)
{
this.contextMenuStrip2.Items.Clear();
DataTable dt = DAL.TorchLabQuery.Query_ReportFormat(tbscpmc.Text);//通过报告导出格式表中的产品名称得到报告格式
int count = dt.Rows.Count;
ToolStripItem[] Tsi = new ToolStripItem[count];
for(int i=0;i<count;i++)
{
this.contextMenuStrip2.Items.Add(dt.Rows[i]["ReportFormat"].ToString());
this.contextMenuStrip2.Items[i].Click += new EventHandler(ts_1_click); //对应的点击事件
}
contextMenuStrip2.Show(btPrint, 0, btPrint.Height);
}
private void ts_1_click(object sender,EventArgs e)
{
ToolStripMenuItem Tsm = (sender as ToolStripMenuItem);
string name = Tsm.Text;
。。。。。。
}