MDI子窗体修改MDI父窗体中的变量,来实现子窗体实例的只出现一次,各位高手请大家指点一下,刚学习C#

huazaimh 2009-05-10 01:21:19
在主窗体中,我写了这样的语句,定义了一个变量

public StorageFrm frmStorage;

private void DressManageItem_Click(object sender, EventArgs e)
{
if (frmStorage == null)
{
frmStorage = new StorageFrm();
frmStorage.Show();
}
else
{
frmStorage.Activate();
frmStorage.WindowState = FormWindowState.Normal;
frmStorage.BringToFront();
}
}
这里是可以实现唯StorageFrm窗体只弹出一次,可是关闭之后,再想弹出就不可以了,我知道用全局变量应该是可以实现的,请问各位高手,有没有别的办法,不用全局变量,能不能实现?谢谢!
...全文
156 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlzx21 2009-05-10
  • 打赏
  • 举报
回复
用集合来存储所有子窗体实例(或指定一个什么标记),再运行子窗体实例时,先判断相应
实例是否在集合存在,若存在,则不在运行;在关闭子窗体,从集合中将相应实例(或标记)删除
pricks 2009-05-10
  • 打赏
  • 举报
回复
运用单例模式。
huazaimh 2009-05-10
  • 打赏
  • 举报
回复
private static 窗体 instance = null;
//添加一个属性
public static 窗体 Instance
{
set{
}
get{
if(instance == null){
new 窗体();
}
return instance;
}
}在窗体的构造函数中加入如下代码
instance = this;创建窗体Closed事件
private void 窗体_FormClosed(object sender, FormClosedEventArgs e)
{
instance = null;
}
使用方法:
在要调用该窗体的地方加入如下代码
窗体 myfrm = 窗体.Instance;
myfrm.Show();
myfrm.Activate();

该办法不错,挺简单的,谢谢!
huazaimh 2009-05-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
private T OpenUniqueMDIChildWindow <T>(Form mdiParent) where T : Form, new()
{
foreach (Form subForm in mdiParent.MdiChildren)
{
if (subForm.GetType().Equals(typeof(T)))
{
subForm.Activate();
return subForm as T;
}
}
T newForm = new T();
newForm.MdiParent = mdiParent;
newForm.Show();
return newF…
[/Quote]
能行不?
ZJ159 2009-05-10
  • 打赏
  • 举报
回复
wuyq11 2009-05-10
  • 打赏
  • 举报
回复
private T OpenUniqueMDIChildWindow<T>(Form mdiParent) where T : Form, new()
{
foreach (Form subForm in mdiParent.MdiChildren)
{
if (subForm.GetType().Equals(typeof(T)))
{
subForm.Activate();
return subForm as T;
}
}
T newForm = new T();
newForm.MdiParent = mdiParent;
newForm.Show();
return newForm;
}

private Form FindChildRenForm(Type type)
{
Form frm = null;
foreach(Form f in this.MdiChildren)
{
if ( f.GetType() == type )
{
frm = f;
if ( frm.WindowState != FormWindowState.Maximized )
frm.WindowState = FormWindowState.Maximized;
frm.Activate();
break;
}
}
return frm;
}
qq69524898 2009-05-10
  • 打赏
  • 举报
回复
期待中,帮顶一下.
冷月孤峰 2009-05-10
  • 打赏
  • 举报
回复

#region 不重复打开MDI子窗体
/// <summary>
/// 不重复打开MDI子窗体
/// 调用:this.OpenMDIWindow(typeof(窗体名).ToString());
/// </summary>
/// <param name="ChildTypeString">窗体名称(typeof(FormName).ToString())</param>
private void OpenMDIWindow(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)
{
this.Refresh();
//Form frmTip = LoadTip.ShowTipForm(this, "正在加载窗体");

myChild = obj as Form;
myChild.MdiParent = this;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus();

//frmTip.Close();

}
}
}

/// <summary>
/// Search mdi child form by specific type string
/// </summary>
/// <param name="ChildTypeString"></param>
/// <returns></returns>
private 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.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
{
return false;
}
}
#endregion

调用:
this.OpenMDIWindow(typeof(窗体名).ToString());
huazaimh 2009-05-10
  • 打赏
  • 举报
回复
up
lijun7788 2009-05-10
  • 打赏
  • 举报
回复
写一个Config类,在Config中
class Config
{
private static StorageFrm frmStorage;
public static void ShowStorageFrm(){
if(frmStorage==null)
frmStorage=new StorageFrm();
frmStorage.show();
}
}

编写StorageFrm窗体的FormeClosing事件
this.Hide();
e.Cancel=True;
让窗体不关闭,只是隐藏

110,533

社区成员

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

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

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