ContextMenuStrip 动态添加多级子菜单(二)

本文介绍如何在C#中实现动态添加子菜单并响应其点击事件。主要步骤包括实例化ToolStripItem对象、添加到ContextMenuStrip控件及绑定点击事件。示例展示了如何根据不同子菜单触发特定操作。

昨天写了动态添加子菜单,但是今天操作没有找到合适的响应子菜单执行的方法:所以今天再记录一个方法:

1.首先要实例化几个ToolStripItem(要为某一父菜单添加几个子菜单就实例化几个);方法如下:

/*添加子菜单*/
ToolStripItem ts_1
= new ToolStripMenuItem("正常");

 2.将其添加到ContextMenuStrip添加方法与(一)中写的类似

((ToolStripDropDownItem)(contextMenuStrip1.Items[1])).DropDownItems.Add(ts_1);

按上面这个模式可以添加多级的,参考(一)。写到这里,没有发现在与(一)有什么不同,还是不知道怎么响应子

菜单的执行。这个时候就要借住委托和事件了

3.其实,在实例化后ToolStripItem后要为实例化的ToolStripItem对象添加新的事件,然后再添加到ContextMenuStrip控件。

代码如下:

ts_1.Click+=new EventHandler(ts_1_Click);

 4.接下来就是完成ts_1_Click这个方法了。当子菜单被单击的时候将会响应现在这个新方法。代码如下:

/*添加的"正常"子菜单Click事件*/
private void ts_1_Click(object sender, EventArgs e)
{
/*当添加的这个子菜单被单击时响应的操作写在这里面*/
}

5.如果要添加多个子菜单就要为每一个子菜单写一个Click事件。

到此此文就结束了。

 

加一些自己的代码以便参考:

1 this.contextMenuStrip1.Items.Add("修改");
2 this.contextMenuStrip1.Items.Add("状态");
3 this.contextMenuStrip1.Items.Add("删除");
4
5 /*添加子菜单*/
6 ToolStripItem ts_1 = new ToolStripMenuItem("正常");
7 ts_1.Click+=new EventHandler(ts_1_Click);
8 /*只能通过ContextMenuStrip的Item的索引为其添加子菜单,通过Item的Text属性会报错*/
9 ((ToolStripDropDownItem)(contextMenuStrip1.Items[1])).DropDownItems.Add(ts_1);
10 ToolStripItem ts_2 = new ToolStripMenuItem("维修");
11 ts_2.Click+=new EventHandler(ts_2_Click);
12 ((ToolStripDropDownItem)(contextMenuStrip1.Items[1])).DropDownItems.Add(ts_2);
13 ToolStripItem ts_3 = new ToolStripMenuItem("建设中");
14 ts_3.Click+=new EventHandler(ts_3_Click);
15 ((ToolStripDropDownItem)(contextMenuStrip1.Items[1])).DropDownItems.Add(ts_3);
16
17 /*chz是一个实体类对象,RentalStatus为其属性*/
18 chz.RentalStatus = this.dgv_SearchRentalInfo.Rows[e.RowIndex].Cells["RentalStatus"].Value.ToString();
19
20 /*子菜单Click事件*/
21 private void ts_1_Click(object sender, EventArgs e)
22 {
23 if (chz.RentalStatus != "01")
24 {
25 chz.RentalStatus = "01"; /*正常*/
26 this.Loading_rental.Start();
27 this.Loading_rental.Visible = true;
28 this.backgroundWorker4.RunWorkerAsync();
29 }
30 else
31 {
32 this.label2.Text = "已是此状态";
33 this.label2.Visible = true;
34 }
35 }
36 /*子菜单Click事件*/
37 private void ts_2_Click(object sender, EventArgs e)
38 {
39 if (chz.RentalStatus != "02")
40 {
41 chz.RentalStatus = "02"; /*维修*/
42 this.Loading_rental.Start();
43 this.Loading_rental.Visible = true;
44 this.backgroundWorker4.RunWorkerAsync();
45 }
46 else
47 {
48 this.label2.Text = "已是此状态";
49 this.label2.Visible = true;
50 }
51 }
52 /*子菜单Click事件*/
53 private void ts_3_Click(object sender, EventArgs e)
54 {
55 if (chz.RentalStatus != "00")
56 {
57 chz.RentalStatus = "00"; /*建设中*/
58 this.Loading_rental.Start();
59 this.Loading_rental.Visible = true;
60 this.backgroundWorker4.RunWorkerAsync();
61 }
62 else
63 {
64 this.label2.Text = "已是此状态";
65 this.label2.Visible = true;
66 }
67 }

转载于:https://www.cnblogs.com/jRoger/articles/1943249.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值