问题描述

是将MenuStrip中的所有DropDownItems列出来,供用户选择,被选中的项将在ToolStrip被显示出来,并且可以使用与其对应的DropDownItem的事件.

其工作流程可以分解如下图所示:

图1.1流程图

实际上这个图所描述的正是我们上面说的一样,只是换作图形表示而已,但这样一来程序的结构就变得清晰起来了。

实际上这个图所描述的正是我们上面说的一样,只是换作图形表示而已,但这样一来程序的结构就变得清晰起来了。可以看出,整个问题实际上被分解成了三个部分,即遍历菜单栏所有可用的DropDownItems,提供用户的选择界面,和为工具栏动态添Items及其事件。提供用户的选择界面这部分内容没什么技术含量,可以用任何一种你喜欢的方式实现,没什么好说的。下面我将重点说一下遍历菜单栏所有可用的DropDownItems,和为工具栏动态添Items及其事件这两部分。

一、 遍历菜单栏所有可用的DropDownItems

菜单栏的结构实际上是典型的“森林”,而每一个下拉菜单(DropDownItem)都是一棵树。

面对这种问题最好的解决方式就是递归。很多人提到递归就头大,其实对于了解递归的人来说递归很简单。尤其是树的遍历就更加的简单了,它有固有的模式,一般先对结束条件进行判断,然后递归遍历各个子树,就这么简单。

对于本题DropDownItem的函数可以这样来写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private ToolStripMenuItem Items(ToolStripMenuItem tsItem)

{ //结束条件判定

if (tsItem.DropDownItems.Count <= 0) return tsItem;

else

{ //遍历各子树

foreach (ToolStripMenuItem mItem in tsItem.DropDownItems)

{ //递归遍历mItem子树

Items(mItem);

}

}

return null;

}

** 代码说明:**

  • 函数首先判断节点(就是一个DropDownItem)是否含有子节点,如果它不含有子节点就return它(这就是结束条件判断)。

我们这样做完全是因为我们菜单使用习惯(所有的菜单功能都是在叶子节点实现功能的),当然我们也可以将非叶子节点也return回去。但是最好不要这么干,因为它会令我们产生很多困扰(不信邪的朋友可以试试)。下一步再分别遍历该节点的每一个子树

  • 上面是遍历某个DropDownItem和它的子节点,下面遍历整个菜单栏:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
…… 

//这是遍历菜单栏,可以放在任何你需要的地方,

//注: menuStrip1是我菜单栏的名字

foreach (ToolStripMenuItem mItem in this.menuStrip1.Items)

{ //递归遍历各个DropDownItem

Items(mItem);

}

//

……

上面就是遍历菜单的全部代码。这当然还没完,我们需要将遍历菜单得到的各个菜单项存入容器,以备下面的程序使用。

什么容器都可以,而我选择了List<>。要实现这个功能:

  • 1.首先,我们需要增加一个容器 lMenuItems
1
2
3
4
5
6
7
……

//增加一个全局变量,它是一个容器

List<ToolStripMenuItem> lMenuItems;

……
  • 2.然后,在遍历菜单栏程序段中添加代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//这是遍历菜单栏,可以放在任何你需要的地方,

//menuStrip1是我菜单栏的名字

lMenuItems = new List<ToolStripMenuItem>();

foreach (ToolStripMenuItem mItem in this.menuStrip1.Items)

{ //递归遍历各个DropDownItem

Items(mItem);

}

//

……
  • 3.将DropDownItem的递归遍历函数修改成下面这个样子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private ToolStripMenuItem Items(ToolStripMenuItem tsItem)

{ //结束条件判定

if (tsItem.DropDownItems.Count <= 0) return tsItem;

else

{ //遍历各子树

ToolStripMenuItem value = null;//临时变量

foreach (ToolStripMenuItem mItem in tsItem.DropDownItems)

{ //递归遍历mItem子树

value = Items(mItem);

if (value != null && lMenuItems != null)

lMenuItems.Add(value);

}

}

return null;

}

好,初战告捷。跳过如何提供用户的选择界面部分不提,直接进入重点。

为工具栏动态添Items及其事件

给工具栏动态添加Items其实是一件非常简单的事,就像有些人说的new一个,再Add()进去就搞定了。

关键的问题不在这里,如何使各个工具栏的Item的事件可以与DropDownItem的事件一一对应才是问题的关键。

各个DropDownItem都可以拥有自己的事件,各个事件的名称不同,而且事件是不能直接拿事件赋值的,如果无法实现事件的动态赋值,菜单栏动态添加也就没戏了。如果没有好多解决方法,程序开发到这里只能是僵局。

关于事件的调整

  • 1.首先事件是可以调用事件的,并且几个控件可以共用一个事件。这就为问题的解决提供了可能性。
    让所有的可用菜单项都使用一个事件,或者说让这个事件统领其他的菜单事件。
    我们新建一个事件,并将这个事件命名为Menu_Click,它来统领整个Menu的所有Click事件:
1
2
3
private void Menu_Click(object sender, EventArgs e)

{ …… }
  • 2.第二步,将原来所有DropDownItems的事件(菜单项事件)都替换成Menu_Click。

小知识: 事件参数中的sender事实上就是发出事件的控件(或源),但因为它的类型是object,所有在使用前要对其进行强制类型转换。

比如:有一个sender的实际类型是ToolStripItem,我需要获得sender的Name属性值就需要:

1
string sender_Name = ((ToolStripItem)sender).Name;

应用上面特点,在事件处理函数Menu_Click中就不难使用switch或者if/else来区分事件的来源,并分别调用他们自己的事件处理函数了。

3.动态添加

解决了事件的问题,工具栏的动态添加实际上就是一句话的事

1
2
3
4
5
6
7
8
9
10
11
toolStrip1.Items.Add(new ToolStripButton(

<某DropDownItem>.Text, //DropDownItem显示的文字

<某DropDownItem>.Image, //DropDownItem图标

new System.EventHandler(Menu_Click), //DropDownItem事件

<某DropDownItem>.Name //DropDownItem名称

));

简单实例:

(请将代码粘贴到你新建项目中对应得位置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/*******************/

/*Form1.Designer.cs*/

/*******************/

namespace dynamicToolbar

{

partial class Form1

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;



/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}



#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.menuStrip1 = new System.Windows.Forms.MenuStrip();

this.first = new System.Windows.Forms.ToolStripMenuItem();

this.f1 = new System.Windows.Forms.ToolStripMenuItem();

this.f11 = new System.Windows.Forms.ToolStripMenuItem();

this.f12 = new System.Windows.Forms.ToolStripMenuItem();

this.f2 = new System.Windows.Forms.ToolStripMenuItem();

this.f3 = new System.Windows.Forms.ToolStripMenuItem();

this.f31 = new System.Windows.Forms.ToolStripMenuItem();

this.secondToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.s1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.s2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.s3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.s31ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.s311ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.s32ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.thirdToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.t1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.t2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

this.set = new System.Windows.Forms.Button();

this.toolStrip1 = new System.Windows.Forms.ToolStrip();

this.menuStrip1.SuspendLayout();

this.SuspendLayout();

//

// menuStrip1

//

this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.first,

this.secondToolStripMenuItem,

this.thirdToolStripMenuItem});

this.menuStrip1.Location = new System.Drawing.Point(0, 0);

this.menuStrip1.Name = "menuStrip1";

this.menuStrip1.Size = new System.Drawing.Size(292, 24);

this.menuStrip1.TabIndex = 0;

this.menuStrip1.Text = "menuStrip1";

//

// first

//

this.first.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.f1,

this.f2,

this.f3});

this.first.Name = "first";

this.first.Size = new System.Drawing.Size(47, 20);

this.first.Text = "first";

this.first.Click += new System.EventHandler(this.Menu_Click);

//

// f1

//

this.f1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.f11,

this.f12});

this.f1.Name = "f1";

this.f1.Size = new System.Drawing.Size(82, 22);

this.f1.Text = "f1";

//

// f11

//

this.f11.Name = "f11";

this.f11.Size = new System.Drawing.Size(88, 22);

this.f11.Text = "f11";

this.f11.Click += new System.EventHandler(this.Menu_Click);

//

// f12

//

this.f12.Name = "f12";

this.f12.Size = new System.Drawing.Size(88, 22);

this.f12.Text = "f12";

this.f12.Click += new System.EventHandler(this.Menu_Click);

//

// f2

//

this.f2.Name = "f2";

this.f2.Size = new System.Drawing.Size(82, 22);

this.f2.Text = "f2";

this.f2.Click += new System.EventHandler(this.Menu_Click);

//

// f3

//

this.f3.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.f31});

this.f3.Name = "f3";

this.f3.Size = new System.Drawing.Size(82, 22);

this.f3.Text = "f3";

//

// f31

//

this.f31.Name = "f31";

this.f31.Size = new System.Drawing.Size(88, 22);

this.f31.Text = "f31";

this.f31.Click += new System.EventHandler(this.Menu_Click);

//

// secondToolStripMenuItem

//

this.secondToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.s1ToolStripMenuItem,

this.s2ToolStripMenuItem,

this.s3ToolStripMenuItem});

this.secondToolStripMenuItem.Name = "secondToolStripMenuItem";

this.secondToolStripMenuItem.Size = new System.Drawing.Size(53, 20);

this.secondToolStripMenuItem.Text = "second";

//

// s1ToolStripMenuItem

//

this.s1ToolStripMenuItem.Name = "s1ToolStripMenuItem";

this.s1ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);

this.s1ToolStripMenuItem.Text = "s1";

this.s1ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);

//

// s2ToolStripMenuItem

//

this.s2ToolStripMenuItem.Name = "s2ToolStripMenuItem";

this.s2ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);

this.s2ToolStripMenuItem.Text = "s2";

this.s2ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);

//

// s3ToolStripMenuItem

//

this.s3ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.s31ToolStripMenuItem,

this.s32ToolStripMenuItem});

this.s3ToolStripMenuItem.Name = "s3ToolStripMenuItem";

this.s3ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);

this.s3ToolStripMenuItem.Text = "s3";

//

// s31ToolStripMenuItem

//

this.s31ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.s311ToolStripMenuItem});

this.s31ToolStripMenuItem.Name = "s31ToolStripMenuItem";

this.s31ToolStripMenuItem.Size = new System.Drawing.Size(88, 22);

this.s31ToolStripMenuItem.Text = "s31";

//

// s311ToolStripMenuItem

//

this.s311ToolStripMenuItem.Name = "s311ToolStripMenuItem";

this.s311ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);

this.s311ToolStripMenuItem.Text = "s311";

this.s311ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);

//

// s32ToolStripMenuItem

//

this.s32ToolStripMenuItem.Name = "s32ToolStripMenuItem";

this.s32ToolStripMenuItem.Size = new System.Drawing.Size(88, 22);

this.s32ToolStripMenuItem.Text = "s32";

this.s32ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);

//

// thirdToolStripMenuItem

//

this.thirdToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {

this.t1ToolStripMenuItem,

this.t2ToolStripMenuItem});

this.thirdToolStripMenuItem.Name = "thirdToolStripMenuItem";

this.thirdToolStripMenuItem.Size = new System.Drawing.Size(47, 20);

this.thirdToolStripMenuItem.Text = "third";

//

// t1ToolStripMenuItem

//

this.t1ToolStripMenuItem.Name = "t1ToolStripMenuItem";

this.t1ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);

this.t1ToolStripMenuItem.Text = "t1";

this.t1ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);

//

// t2ToolStripMenuItem

//

this.t2ToolStripMenuItem.Name = "t2ToolStripMenuItem";

this.t2ToolStripMenuItem.Size = new System.Drawing.Size(82, 22);

this.t2ToolStripMenuItem.Text = "t2";

this.t2ToolStripMenuItem.Click += new System.EventHandler(this.Menu_Click);

//

// set

//

this.set.Location = new System.Drawing.Point(106, 185);

this.set.Name = "set";

this.set.Size = new System.Drawing.Size(75, 23);

this.set.TabIndex = 3;

this.set.Text = "set->Add";

this.set.UseVisualStyleBackColor = true;

this.set.Click += new System.EventHandler(this.btnSet_Click);

//

// toolStrip1

//

this.toolStrip1.Location = new System.Drawing.Point(0, 24);

this.toolStrip1.Name = "toolStrip2";

this.toolStrip1.Size = new System.Drawing.Size(292, 25);

this.toolStrip1.TabIndex = 4;

this.toolStrip1.Text = "toolStrip2";

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF( 6F , 12F );

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.toolStrip1);

this.Controls.Add(this.set);

this.Controls.Add(this.menuStrip1);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

this.menuStrip1.ResumeLayout(false);

this.menuStrip1.PerformLayout();

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion



private System.Windows.Forms.ToolStripMenuItem first;

private System.Windows.Forms.ToolStripMenuItem f1;

private System.Windows.Forms.ToolStripMenuItem f11;

private System.Windows.Forms.ToolStripMenuItem f12;

private System.Windows.Forms.ToolStripMenuItem f2;

private System.Windows.Forms.ToolStripMenuItem f3;

private System.Windows.Forms.ToolStripMenuItem f31;

private System.Windows.Forms.ToolStripMenuItem secondToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem s1ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem s2ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem s3ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem s31ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem s311ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem s32ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem thirdToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem t1ToolStripMenuItem;

private System.Windows.Forms.ToolStripMenuItem t2ToolStripMenuItem;

private System.Windows.Forms.MenuStrip menuStrip1;

private System.Windows.Forms.ToolStrip toolStrip1;

private System.Windows.Forms.Button set;

}

}

/*******************/

/* Form1.cs */

/*******************/

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



namespace dynamicToolbar

{

public partial class Form1 : Form

{ //容器

private List<ToolStripMenuItem> lMenuItems;



public Form1()

{

InitializeComponent();

}



private void Form1_Load(object sender, EventArgs e)

{ //加载菜单项到容器

lMenuItems = new List<ToolStripMenuItem>();

foreach (ToolStripMenuItem mItem in this.menuStrip1.Items)

{

Items(mItem);

}

}



#region 菜单事件

private void Menu_Click(object sender, EventArgs e)

{

string itemName = ((ToolStripItem)sender).Name;

switch (itemName[0])

{

case 'f': first_Click(sender, e); break;

case 's': second_Click(sender, e); break;

case 't': third_Click(sender, e); break;

}

}

private void third_Click(object sender, EventArgs e)

{

MessageBox.Show("这是菜单“Third”的事件处理函数");

}

private void second_Click(object sender, EventArgs e)

{

MessageBox.Show("这是菜单“Second”的事件处理函数");

}

private void first_Click(object sender, EventArgs e)

{

string itemName = ((ToolStripItem)sender).Name;

string showString = null;

switch (itemName)

{

case "f11": showString = "菜单项“" + this.f11.Name + "”被按下了"; break;

case "f12": showString = "菜单项“" + this.f12.Name + "”被按下了"; break;

case "f2": showString = "菜单项“" + this.f2.Name + "”被按下了"; break;

case "f31": showString = "菜单项“" + this.f31.Name + "”被按下了"; break;

default: break;

}

if (showString != null)

MessageBox.Show(showString);

}

#endregion



//递归遍历DropDownItem函数

private ToolStripMenuItem Items(ToolStripMenuItem tsItem)

{ //结束条件判定

if (tsItem.DropDownItems.Count <= 0) return tsItem;

else

{ //遍历各子树

ToolStripMenuItem value = null;//临时变量

foreach (ToolStripMenuItem mItem in tsItem.DropDownItems)

{

value = Items(mItem);//递归遍历mItem子树

if (value != null && lMenuItems != null)

lMenuItems.Add(value);

}

}

return null;

}//递归遍历DropDownItem函数<END>



//动态设置工具栏函数

private void btnSet_Click(object sender, EventArgs e)

{

seting fset = new seting();

foreach (ToolStripMenuItem mitem in lMenuItems)

{

if (mitem.Name != new ToolStripSeparator().Name)

fset.listBox1.Items.Add(mitem.Name);

}

if (fset.ShowDialog() == DialogResult.OK)

{

foreach (string mn in fset.listBox1.SelectedItems)

{ //如果toolStrip1还没有加载,加载之

if (toolStrip1 == null)

{

toolStrip1 = new ToolStrip();

toolStrip1.Top = menuStrip1.Bottom + 1;

toolStrip1.Left = menuStrip1.Left;

toolStrip1.Name = "toolStrip1";

toolStrip1.Size = new System.Drawing.Size(292, 25);

toolStrip1.TabIndex = 2;

toolStrip1.Text = "toolStrip1";



Controls.Add(this.toolStrip1);//将工具栏添加到窗口上

}

//找到与列表框中对应的DropDownItem,添加之

int i = 0;

while (i < lMenuItems.Count && mn != lMenuItems[i].Name)

{ i++; }

if (i < lMenuItems.Count)

{

//给ToolStrip动态添加

toolStrip1.Items.Add(new ToolStripButton(

lMenuItems[i].Text,

lMenuItems[i].Image,

new System.EventHandler(Menu_Click),

lMenuItems[i].Name

)

);

lMenuItems.Remove(lMenuItems[i]);//把已经添加过的从列表中去除

}

i = 0;

}

}

}//动态设置工具栏函数<END>



}

}

/********************/

/*seting.Designer.cs*/

/********************/

namespace dynamicToolbar

{

partial class seting

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;



/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}



#region Windows Form Designer generated code



/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.listBox1 = new System.Windows.Forms.ListBox();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(128, 238);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(75, 23);

this.button1.TabIndex = 1;

this.button1.Text = "add";

this.button1.UseVisualStyleBackColor = true;

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(209, 238);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(75, 23);

this.button2.TabIndex = 2;

this.button2.Text = "取消";

this.button2.UseVisualStyleBackColor = true;

//

// listBox1

//

this.listBox1.FormattingEnabled = true;

this.listBox1.ItemHeight = 12;

this.listBox1.Location = new System.Drawing.Point(12, 12);

this.listBox1.Name = "listBox1";

this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;

this.listBox1.Size = new System.Drawing.Size(268, 220);

this.listBox1.TabIndex = 3;

//

// seting

//

this.AutoScaleDimensions = new System.Drawing.SizeF( 6F , 12F );

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.listBox1);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Name = "seting";

this.Text = "seting";

this.ResumeLayout(false);



}



#endregion



private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

public System.Windows.Forms.ListBox listBox1;

}

}

/********************/

/*seting.Designer.cs*/

/********************/

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



namespace dynamicToolbar

{

public partial class seting : Form

{

public seting()

{

InitializeComponent();

}



private void button1_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.OK;

this.Close();

}

}

}

相关链接(侵删)

  1. C#趣味小程序(6)——动态工具栏

=================我是分割线=================

欢迎到公众号来唠嗑: