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
}
}
查看全文
相关阅读:
01
py5.30
py 5.28
py5.25
py 5.24
py 5.22
py5.21
py 5.18
py 5.17
py 5.16
原文地址:https://www.cnblogs.com/sunbingzibo/p/961935.html
最新文章
ubuntu 140.4 LTS 安装 apach4 2.4并以mod_wsgi的方式部署django站点
静态博客的本地调试问题
简历第二个项目
网段
广播域
二层交换机和三层交换机的区别
网络基础知识
区块链最基本的技术原理
关于“车联网”的最强科普!
5G及移动边缘计算(MEC)学习笔记(3)
热门文章
5G及移动边缘计算(MEC)学习笔记(2)
5G及移动边缘计算(MEC)学习笔记(1)
车联网期刊和会议
OMNeT++ Samples
At a Glance
Getting Started with the Simulation IDE
veins中车联网仿真简述
Django 发送邮件
05
02
Copyright © 2011-2022 走看看