使用MDI窗体结构,主窗体上的各个菜单项对应各个子窗体。比如点击某个菜单项打开某个子窗体的代码如下:
private void 车辆称重ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Weighing.frmWeighing weighing = null;
foreach (Form f in this.MdiChildren)
{
if (f is Weighing.frmWeighing)
{
weighing = (Weighing.frmWeighing)f;
break;
}
}
if (weighing != null)
{
weighing.Show();
weighing.Focus();
}
else
{
weighing = new Weighing.frmWeighing();
weighing.MdiParent = this;
weighing.Show();
weighing.Update();
}
}而其他的菜单项点击代码大同小异,实际上只是改动上面代码中的weighing和Weighing.frmWeighing。我想写个小函数,如:
private void openMDI(object xxx, object yyy)
{
......
}
这样只要把各个子窗体的对应的xxx、yyy作为参数传进来调用一下函数就可以打开相应的子窗体,但这个小函数我写不出来,请高手帮帮忙吧,谢谢了!
private void 车辆称重ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Weighing.frmWeighing weighing = null;
foreach (Form f in this.MdiChildren)
{
if (f is Weighing.frmWeighing)
{
weighing = (Weighing.frmWeighing)f;
break;
}
}
if (weighing != null)
{
weighing.Show();
weighing.Focus();
}
else
{
weighing = new Weighing.frmWeighing();
weighing.MdiParent = this;
weighing.Show();
weighing.Update();
}
}而其他的菜单项点击代码大同小异,实际上只是改动上面代码中的weighing和Weighing.frmWeighing。我想写个小函数,如:
private void openMDI(object xxx, object yyy)
{
......
}
这样只要把各个子窗体的对应的xxx、yyy作为参数传进来调用一下函数就可以打开相应的子窗体,但这个小函数我写不出来,请高手帮帮忙吧,谢谢了!
解决方案 »
- C# 创建XML并保存到Zip文件中
- SQL表導出到EXCEL時,提示: OLE DB 提供者 "Microsoft.Jet.OLEDB.4.0" 尚未註冊
- 【求助】关于删除过期的目录
- c#新建WINDOWS APPLICATION的问题
- DataGrid中的数据如何更新到Excel中去,请大家帮忙给出点资料
- 请各位好心的高手解释一下"委托"的概念和用途
- 我感觉数据绑定的"数据窗口"很不好用,很麻烦
- 极简单的删除记录问题
- c#.net能写木马吗?如果可以那写了之后怎么运行?可以脱离Framework运行吗
- 谁用C#作过组件,然后在ASP中调用,(注意是ASP不是ASP.NET)
- 类、结构或接口成员声明中的标记“(”无效
- 【在线急等】字符判断问题
使用 Singleton<Form1>.Instance.Show();
/// <summary>
/// generic for singletons
/// </summary>
/// <typeparam name="T"></typeparam>
public class Singleton<T> where T : new()
{
// ctor
protected Singleton()
{
if(Instance != null)
{
throw (new Exception("You have tried to create a new singleton class where you should have instanced it. Replace your \"new class()\" with \"class.Instance\""));
}
} public static T Instance
{
get
{
return SingletonCreator.instance;
}
} class SingletonCreator
{
static SingletonCreator()
{ }
internal static readonly T instance = new T();
}
}
1,首先,通过菜单本身,应该能知道需要控制的窗体的类型,ToolStripMenuItem_Click(object sender, EventArgs e)应该能获取窗体的类型,例如可以使用MenuItem.Tag来记录,这样string typename = (string)sender.Tag;
2,查找现有的窗体,一般不使用类型来匹配,而是根据用途设置一个Key,这个Key同样可以记录到窗体的Tag属性里。
3,查找就直接判断Form.Tag = "Key"
4,找到就容易了,你的代码已经处理了
5,找不到,则需要创建,因为类型已经知道,那就直接使用类型创建一个Form,然后接着处理,你已经处理了。
首先要在主窗体的Load或构造中设置每个子菜单项的Tag为对应的子窗体类型。
如:toolStripMenuItem1.Tag=typeof(frmWeighing);
toolStipMenuItem2.Tag=typeof(frmWeighing2); private void 车辆称重ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
ToolStripMenuItem tsm = sender as ToolStripMenuItem;
Type type=(Type)tsm.Tag;//窗体类型
Form subForm = null; //点击菜单后要打开的窗体
foreach (Form f in this.MdiChildren)
{
if (f is type)
{
subForm = f;
break;
}
}
if (subForm != null)
{
subForm.Show();
subForm.Focus();
}
else
{
subForm = (Form)Activator.CreateInstance(type);
subForm.MdiParent = this;
subForm.Show();
subForm.Update();
}
}
if (f.GetType==type)
{
subForm = f;
break;
}