zoukankan
html css js c++ java
动态创建菜单项
如何从简单到复杂一步步创建menustrip,
一步步提升程序的抽象程序,努力做到相同的代码从来不写两次
相同功能的代码不写两次
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Windows.Forms;
using
System.Reflection;
namespace
CSLearn
{
public
class
CreateMenu
{
public
MenuStrip GetMenu()
{
MenuStrip ms
=
new
MenuStrip();
ToolStripMenuItem tsmi
=
new
ToolStripMenuItem(
"
file
"
);
//
------------------------------------------
//
ver1
//
tsmi.Click += new EventHandler(tsmi_Click);
//
------------------------------------------
//
ver2
//
MyCommandDeal test = new MyCommandDeal();
//
tsmi.Click += test.DealFileCommadn;
//
------------------------------------------
//
ver3
string
dealclassname
=
"
CSLearn.MyCommandDeal2
"
;
object
obj
=
Assembly.GetExecutingAssembly().CreateInstance(dealclassname);
ICommandDeal test
=
obj
as
ICommandDeal;
if
(test
==
null
)
{
MessageBox.Show(
"
动态创建类型失败
"
);
throw
new
Exception(
"
传入的类型参数不对,创建类型失败
"
);
}
tsmi.Click
+=
test.DealFileCommand;
ToolStripMenuItem tsmiedit
=
new
ToolStripMenuItem(
"
edit
"
);
tsmiedit.Click
+=
new
EventHandler(tsmiedit_Click);
ms.Items.AddRange(
new
ToolStripItem[]
{ tsmi,tsmiedit}
);
return
ms;
}
//
------------------------------------------
//
ver1
void
tsmiedit_Click(
object
sender, EventArgs e)
{
MessageBox.Show(
"
use this command to edit a file
"
);
}
void
tsmi_Click(
object
sender, EventArgs e)
{
MessageBox.Show(
"
Use this command to crate a menu
"
);
}
}
//
------------------------------------------
//
ver2
public
class
MyCommandDeal
{
public
void
DealFileCommadn(
object
sender, EventArgs e)
{
MessageBox.Show(
"
mycommand deal deal with the file command
"
);
}
}
//
------------------------------------------
//
ver3
public
interface
ICommandDeal
{
void
DealFileCommand(
object
sender, EventArgs e);
}
public
class
MyCommandDeal2:ICommandDeal
{
ICommandDeal 成员
#region
ICommandDeal 成员
public
void
DealFileCommand(
object
sender, EventArgs e)
{
MessageBox.Show(
"
this is the second version of cxy command deal ,use this command to crate a file
"
);
}
#endregion
}
}
查看全文
相关阅读:
.Net框架集WebClient类向WinCE平台上传文件(FTP方式)延迟15秒释疑
WinCE系统下BootLoader的开发
cf1154G 埃氏筛应用
关于调用C kernel functions
Download internal table data into Excel(比使用OLE自己填写速度要快)
Internet+大会和Google请来的大师
回到Mountain View
关于F4 Help帮助窗口的参数F4METHOD的设置
计划策略 MTS部分
人在Google
原文地址:https://www.cnblogs.com/sunbingzibo/p/961935.html
最新文章
error C2143: 语法错误 : 缺少“{”(在“:”的前面)编译错误的解决
谈谈国内开发的Linux手机软件平台Broncho,兼谈Android、Openmoko等[转]
Linux手机运行级别
R语言中缺失值的处理
R语言中diff函数
R语言中subset函数同时依据行列进行数据筛选
R语言中日期值
R语言中数据类型转换
linux shell while循环
R语言中排序函数order、rank、sort
热门文章
linux系统中awk命令 正则匹配
R语言中根据日期筛选数据
R语言中求值域
wince编程心得(一)wince字符转换函数及应用
WINCE快捷方式详解
嵌入式系统之Linux vs WinCE zz
wince Socket编程之一
Windows CE下的串口通讯实例
WinCE开发的一般问题
wince网络免费视频入门教程
Copyright © 2011-2022 走看看