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
}
}
查看全文
相关阅读:
怎么能让json_decode解析带斜杠的字符串
**PHP转义Json里的特殊字符的函数
sql中exists,not exists的用法
**mysql数据库中实现内连接、左连接、右连接
**PHP foreach 如何判断为数组最后一个最高效?
mysql sql语句中用括号处理or和and的运算顺序
iOS图片缓存
linux regulator之浅见【转】
Linux中THIS_MODULE宏定义详解
likely()与unlikely()
原文地址:https://www.cnblogs.com/sunbingzibo/p/961935.html
最新文章
PHP新的垃圾回收机制:Zend GC详解
linuxcon-europe 2015 linux大会
孙钟秀-《 操作系统教程 》(第4版)注释(稿)
厦门大学数据库实验室----- 数据库系统原理
gluPerspective和gluLookAt的关系
使用glPushMatrix和glPopMatrix的原因
error C2440: “static_cast”: 无法从“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)
Android 连接网络数据库的方式
有关于OpenGL、OpenGL ES、WebGL的小结
74.ejs模版页面<%=succss%>不显示值 显示succss is not defined
热门文章
73.node.js开发错误——TypeError: Cannot set property 'XXX' of undefined
72.调用req.flash('error', '用户已存在!'); 时候 报错 "req.flash is not a function"
71.用express框架,出现 express.Router is not a function
70.nodejs操作mongodb
69.nodejs对mongodb数据库的增删改查操作
68.connect-flash 用法详解 req,flash()
67.nodejs取参四种方法req.body,req.params,req.param,req.body
66.app.use(express.static)
65.Express---express-session
iOS中URL的解码和转义问题
Copyright © 2011-2022 走看看