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
}
}
查看全文
相关阅读:
Eclipse集成Tomcat:6个常见的”how to”问题
linux环境变量配置
(原创)JS点击事件——Uncaught TypeError: Cannot set property 'onclick' of null
[ JS 进阶 ] 闭包,作用域链,垃圾回收,内存泄露
webstorm安装后的一些设置技巧:
前端工程师的知识体系
Git常用命令及软件推荐
Vue.js双向绑定的实现原理
GET和POST面试知识点
CSS 巧用 :before和:after
原文地址:https://www.cnblogs.com/sunbingzibo/p/961935.html
最新文章
iOS 9 配置HTTP
第三方框架
文本挖掘——jieba分词
关于“淘宝爆款”的数据抓取与数据分析
淘搜索之网页抓取系统分析与实现(4)- 实现&总结
淘搜索之网页抓取系统分析与实现(3)—scrapy+webkit & mysql+django
淘搜索之网页抓取系统分析与实现(2)—redis + scrapy
淘搜索之网页抓取系统分析与实现(1)—redis使用
Scrapy研究探索(七)——如何防止被ban之策略大集合
Scrapy研究探索(六)——自动爬取网页之II(CrawlSpider)
热门文章
Scrapy研究探索(五)——自动多网页爬取(抓取某人博客所有文章)
Scrapy研究探索(四)——中文输出与中文保存
java多线程批量执行的时限问题
为什么在java持久类中属性应该被定义为包装类型
自己实现spring IOC
JdbcTemplate查询数据中两种处理结果集方法的简单比较
HashMap的遍历
linux文件权限问题
文件重命名
关于字符编码
Copyright © 2011-2022 走看看