【散分】把打开子窗体封装成方法

FlyBee 2009-06-12 01:43:04
 Form2 form2;
private void 窗体1ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (form2 == null || form2.IsDisposed)
{
form2 = new Form2();
form2.MdiParent = this;
form2.Show();
}
else { form2.Activate(); }
}


想把打开子窗体这个事件封装成一个方法提高代码的复用利。大家有什么好的方法学习学习。
...全文
78 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
suifeng55555 2011-10-18
  • 打赏
  • 举报
回复
不错,值得学习!
cloudfang 2009-06-12
  • 打赏
  • 举报
回复
给toolscrip1设置ItemClicked即可,不需要再为每个menuitem都设置单击事件。
cloudfang 2009-06-12
  • 打赏
  • 举报
回复

//toolStrip1的每个item的tag设为要打开的Form。
Hashtable htForms = new Hashtable();

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string FormName=Convert.ToString(e.ClickedItem.Tag);
Type type=Type.GetType(FormName);
object obj = null;
obj=htForms[FormName];
if (obj == null)
{
obj = Activator.CreateInstance(type);
if (obj is Form)
{
htForms.Add(FormName, (Form)obj);
Form frm = (Form)obj;
frm.Show();
}
}
else
{
if (obj is Form)
{
((Form)obj).Activate();
}
}
}
FlyBee 2009-06-12
  • 打赏
  • 举报
回复
自己顶下 期待高人出现
mlliqiushi 2009-06-12
  • 打赏
  • 举报
回复
mark
浏览中!~~
LoveLife_Go 2009-06-12
  • 打赏
  • 举报
回复
学习
nyq1999 2009-06-12
  • 打赏
  • 举报
回复
BD
NX_Soft 2009-06-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yfqvip 的回复:]
C# code#region子窗体只保留一个实例///<summary>///非主窗体打开窗体,不设置子窗体///</summary>///<param name="ChildTypeString"></param>publicvoidOpenWindowNoChild(stringChildTypeString)
{
Form myChild=null;if(!ContainMDIChild(ChildTypeString))
{//Get current process assemblyAssembly assembly=Assembly.GetExecutingAssembly();//Create data type using type stringType ty…
[/Quote]
这个方法试过了,不好使
十八道胡同 2009-06-12
  • 打赏
  • 举报
回复
友情帮顶
angel6709 2009-06-12
  • 打赏
  • 举报
回复
dingding
y82907966 2009-06-12
  • 打赏
  • 举报
回复
来看看
biny237 2009-06-12
  • 打赏
  • 举报
回复
不会 学习。。。
MOmo400 2009-06-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yfqvip 的回复:]
C# code
#region 子窗体只保留一个实例
/// <summary>
/// 非主窗体打开窗体,不设置子窗体
/// </summary>
/// <param name="ChildTypeString"></param>
public void OpenWindowNoChild(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current pr…
[/Quote]

UP
FlyBee 2009-06-12
  • 打赏
  • 举报
回复
有没有什么办法把 Form2 和 form2 用参数传进去
修改一下昵称 2009-06-12
  • 打赏
  • 举报
回复
jf
messi_yang 2009-06-12
  • 打赏
  • 举报
回复
關注
Neil198 2009-06-12
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Test11
{
static class Class1
{
public static void showForm(Form f)
{
f.Show();
}
}
}




 private void button1_Click(object sender, EventArgs e)
{

Class1.showForm(new Form2());


}
TkingCN 2009-06-12
  • 打赏
  • 举报
回复
顶顶顶 关注一下 我知道的方法和你一样
满衣兄 2009-06-12
  • 打赏
  • 举报
回复

#region 子窗体只保留一个实例
/// <summary>
/// 非主窗体打开窗体,不设置子窗体
/// </summary>
/// <param name="ChildTypeString"></param>
public void OpenWindowNoChild(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current process assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(ChildTypeString);
// Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
// Show child form
if (obj != null)
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.Show();
myChild.Focus();
}
}
}
/// <summary>
/// Open child window
/// </summary>
/// <param name="ChildTypeString"></param>
public void OpenWindow(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current process assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(ChildTypeString);
// Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
// Show child form
if (obj != null)
{
myChild = obj as Form;
myChild.MdiParent = this;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus();
}
}
}

/// <summary>
/// Search mdi child form by specific type string
/// </summary>
/// <param name="ChildTypeString"></param>
/// <returns></returns>
public bool ContainMDIChild(string ChildTypeString)
{
Form myMDIChild = null;
foreach (Form f in this.MdiChildren)
{
if (f.GetType().ToString() == ChildTypeString)
{
// found it
myMDIChild = f;
break;
}
}
// Show the exist form
if (myMDIChild != null)
{
myMDIChild.WindowState = FormWindowState.Maximized;
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
return false;
}
#endregion
caorenlong 2009-06-12
  • 打赏
  • 举报
回复
接分
加载更多回复(2)

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧