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
}
}
查看全文
相关阅读:
wait waitpid
达梦备份还原
sigprocmask阻塞信号
sigaction信号处理
dd命令
linux系统启动过程
cpio建立、还原备份档
configure详解
Git入门基础教程
一篇文章了解Github和Git教程
原文地址:https://www.cnblogs.com/sunbingzibo/p/961935.html
最新文章
leetcode665
leetcode661
leetcode653
leetcode657
leetcode645
leetcode643
leetcode633
leetcode637
2019-7-22-Roslyn-获得-sln-文件所在的文件夹
2019-11-19-git-修改commit日期为之前的日期
热门文章
2019-10-22-win7-无法启动-WPF-程序-D3Dcompiler_47.dll-丢失
2019-1-9-WPF-最小的代码使用-DynamicRenderer-书写
2019-6-23-WPF-获得当前输入法语言区域
2018-9-30-VisualStudio-使用多个环境进行调试
2019-7-25-VisualStudio-2019-新创建项目添加-git-仓库
2018-8-10-win10-uwp-使用动画修改-Grid-column-的宽度
2019-10-7-WPF-如何跨线程重新抛出异常
2019-2-3-VisualStudio-扩展开发-添加输出窗口
达梦数据库安装使用
达梦常用语句
Copyright © 2011-2022 走看看