求教:C#下动态加载菜单(200分)

  • 主题发起人 主题发起人 小菠萝
  • 开始时间 开始时间

小菠萝

Unregistered / Unconfirmed
GUEST, unregistred user!
设计思想:将菜单项的click事件(已设计好在源文件中或公用类中)的名称存放在文件或数据库,增加菜单项时,并将click事件也加上,并使生效.
就像开发环境中,编辑事件时,才出现"重构"
简化代码如下:(核心部分,请)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public string Even_name = "menunew_Click";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.ToolStripMenuItem mi = new ToolStripMenuItem();
mi.Text = "新增菜单项";
//新增菜单的click事件:
//mi.Click += new EventHandler(this.menunew_Click);
//此方法可以实现,当函数名存放在字符串Even_name中时,如何实现
//写成函数或其它直接加上都行(求源码或例子源码)
menuStrip1.Items.Insert(0,mi);
}
private void menunew_Click(object sender, EventArgs e)
{
MessageBox.Show("这是一个程序新增菜单!");
}
private void menu1_Click(object sender, EventArgs e)
{
MessageBox.Show("这是一个设计时的菜单!");
}
}
}
 
Mark,我也想知道。
 
你可在.
private void menunew_Click(object sender, EventArgs e)
{
MessageBox.Show("这是一个程序新增菜单!");
}
判断mi的名字加载不同函数
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Data;
using Client.Comm;
namespace Client.Base
{
/// <summary>
/// Summary description for FormBaseMain.
/// </summary>
public class FormBaseMain : FormBase
{
protected System.Windows.Forms.MainMenu mainMenu;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public FormBaseMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
///////////////////////////////////////////////
if (!DesignMode)
{
InitMenu();
}
///////////////////////////////////////////////

}
/// <summary>
/// Clean up any resources being used.
/// </summary>
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support -do
not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu = new System.Windows.Forms.MainMenu();
//
// FormBaseMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(688, 421);
this.IsMdiContainer = true;
this.KeyPreview = false;
this.Menu = this.mainMenu;
this.Name = "FormBaseMain";
this.Text = "FormBaseMain";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormBaseMain_Closing);
}
#endregion
protected Hashtable fHT = new Hashtable();
protected Hashtable fHTParameter1 = new Hashtable();
protected Hashtable fHTParameter2 = new Hashtable();
protected Hashtable MenuHashtable
{
get
{
return fHT;
}
}
private void NewForm(object sender, System.EventArgs e)
{
//string FormName = (string)fHT[((MenuItem)sender).Text];
string FormName = fHT[((MenuItem)sender).Text].ToString();
Assembly a = Assembly.LoadFrom(Application.ExecutablePath);
Type[] ts = a.GetTypes();
foreach(Type t in ts)
{
string s = fHTParameter1[((MenuItem)sender).Text].ToString();
string s2 = fHTParameter2[((MenuItem)sender).Text].ToString();
if (t.Name != FormName)
{
continue;
}
if (t.BaseType.Name != "FormBaseChild" && t.BaseType.Name != "FormBaseDialog")
{
continue;
}
if (OpenChildForm(FormName, s, s2))
{
return;
}
object f = System.Activator.CreateInstance(t);
((FormBase)f).Parameter1 = s;
((FormBase)f).Parameter2 = s2;
((FormBase)f).AfterNew();
((FormBase)f).BeforeLoad();
if (t.BaseType.Name == "FormBaseChild" && t.Name == FormName && ((FormBase)f).ShowVisible)
{
((FormBaseChild)f).MdiParent = this;
((FormBaseChild)f).Show();
}
else
if (t.BaseType.Name == "FormBaseDialog" && t.Name == FormName && ((FormBase)f).ShowVisible)
{
if (((FormBaseDialog)f).ShowModel == "ShowDialog")
{
((FormBaseDialog)f).ShowDialog();
}
else
if (((FormBaseDialog)f).ShowModel == "Show")
{
((FormBaseDialog)f).MdiParent = this;
((FormBaseDialog)f).Show();
}
}
((FormBase)f).AfterLoad();
if (((FormBase)f).ShowVisible)
{
((FormBase)f).BeforeActivate();
((FormBase)f).Activate();
((FormBase)f).AfterActivate();
}
else
{
((FormBase)f).Close();
if (this.ActiveMdiChild != null)
{
OpenChildForm(this.ActiveMdiChild.Name.ToString(), s, s2);
}
}
return;
}
}
private bool OpenChildForm(string ChildFromName, string sParameter1, string sParameter2)
{
foreach(Form cf in this.MdiChildren)
{
if(cf.Name == ChildFromName)
{
//cf.WindowState = FormWindowState.Maximized;
//cf.Show();
((FormBase)cf).Parameter1 = sParameter1;
((FormBase)cf).Parameter2 = sParameter2;
((FormBase)cf).BeforeActivate();
cf.Activate();
((FormBase)cf).AfterActivate();
return true;
}
}
return false;
}

private void InitMenu()
{
DataTable dt = COMM.GetInstance().GetAuth(0);
if (Func.CheckDataTableCount(dt) < 1)
{
return;
}
for (int i = 0;
i <= dt.Rows.Count - 1;
++i)
{
if (dt.Rows["Func_cat"].ToString().ToLower() != "CS".ToLower())
{
continue;
}
MenuItem mi = new MenuItem(dt.Rows["func_no"].ToString());
mi.Text = dt.Rows["func_name"].ToString();
mi.Click += new System.EventHandler(NewForm);
mainMenu.MenuItems.Add(mi);
fHT.Add(mi.Text, dt.Rows["func_no"].ToString());
fHTParameter1.Add(mi.Text, Func.StrToStr(dt.Rows["func_text_1"].ToString()));
fHTParameter2.Add(mi.Text, Func.StrToStr(dt.Rows["func_text_2"].ToString()));
FillMenu(mi, int.Parse(dt.Rows["func_id"].ToString()));
}
}
private void FillMenu(MenuItem mi, int FuncID)
{
DataTable dt = new DataTable();
if (CheckFuncID(ref dt, FuncID) > 0)
{
for (int i = 0;
i <= dt.Rows.Count - 1;
++i)
{
if (dt.Rows["Func_cat"].ToString().ToLower() != "CS".ToLower())
{
continue;
}
MenuItem mi2 = new MenuItem(dt.Rows["func_no"].ToString());
mi2.Text = dt.Rows["func_name"].ToString();
mi2.Click += new System.EventHandler(NewForm);
mi.MenuItems.Add(mi2);

fHT.Add(mi2.Text, dt.Rows["func_no"].ToString());
fHTParameter1.Add(mi2.Text, Func.StrToStr(dt.Rows["func_text_1"].ToString()));
fHTParameter2.Add(mi2.Text, Func.StrToStr(dt.Rows["func_text_2"].ToString()));
FillMenu(mi2, Func.StrToInt(dt.Rows["func_id"].ToString()));
}
}

}
private int CheckFuncID (ref DataTable dt, int id)
{
int i = 0;
dt = COMM.GetInstance().GetAuth(id);
i = dt.Rows.Count;
return i;
}
private void FormBaseMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Func.ShowYesNoMes(COMM.GetInstance().ReadResText("comm.IsEscape"), 2) == DialogResult.No)
{
e.Cancel = true;
}
}
protected override void Init()
{
base.Init();
COMM.GetInstance().AppMdiParent = this;
}
}
}
我以前写的
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部