Winform窗体设计 实现任何时候在主容器中存在一个子窗体
为了减轻服务器的压力和提高程序的性能和操作习惯,需要实现 以下功能
Winform窗体设计 实现任何时候在主容器中存在一个子窗体?
就是打开新窗体先关闭已经打开的但是实现 的代码 ,达不到我要的效果!??大家帮帮!o
private static void openChildrenWindow(Form ParentForm, Form ChildrenForm)--------------------编程问答--------------------
{
bool Flag = false;
Form[] CForm = ParentForm.MdiChildren;
foreach (Form ff in CForm)
{
//如果同名的子窗体已经存在那么Flag=ture并将子窗体激活,之后if判断中的ChildrenForm.Show()也就不会再次执行了。
if (ff.Name==ChildrenForm.Name)
{
Flag = true;
ff.Activate();
// ff.Size = ParentForm.Size;
ff.WindowState = FormWindowState.Normal;
break;
}
}
if (Flag == false)
{
ChildrenForm.MdiParent = ParentForm;
ChildrenForm.WindowState = FormWindowState.Normal;
ChildrenForm = new Form();
ChildrenForm.Show();
}
}
class TestForm: Form--------------------编程问答--------------------
{
private static TestForm _instance;
public static TestForm Instance
{
get
{
if(_instance == null) _instance = new TestForm();
return _instance;
}
}
private TestForm() { //...}
}
foreach (Form tempChildForm in MdiForm.MdiChildren)--------------------编程问答-------------------- 我需要一个通用的方法 在每个窗体 代码中调用下 传几个参数 这样才方便啊!要是没个窗体都要挟判断逻辑 岂不是很麻烦吗? 我那个代码的意思就是实现这个效果!但是调用我的那个方法 传两个参数 但是 没任何效果 连窗体都不 弹出了?郁闷好几天了!今天又想起来了!希望帮忙解决。。。。。。 --------------------编程问答--------------------
{
tempChildForm.Close();
}
//再打开你的窗体
#region 窗体操作--------------------编程问答-------------------- class TestForm: Form
/// <summary>
/// 名称:CheckChildOpenState
/// 功能:用子窗体的Name进行判断是否已实例化,如果存在则将他激活
/// </summary>
/// <param name="MdiForm">容器窗体</param>
/// <param name="ChildForm">子窗体</param>
public static void CheckChildOpenState(Form MdiForm, Form ChildForm)
{
foreach (Form tempChildForm in MdiForm.MdiChildren)
{
if (tempChildForm.Name == ChildForm.Name.ToString())
{
if (tempChildForm.WindowState == FormWindowState.Minimized)
{
tempChildForm.WindowState = FormWindowState.Normal;
}
tempChildForm.Activate();
return;
}
else
{
tempChildForm.Close();
}
}
ChildForm.MdiParent = MdiForm;
ChildForm.Show();
}
#endregion
{
static Test Form() { _instances = new Dictionary<string,TestForm>(); }
private static Dictionary<string,TestForm> _instances;
public static TestForm GetInstance(string param)
{
get
{
if(_instance.ContainsKey(param)) return _instances[param];
var t = new Test Form();
_instances.Add(param, t);
return t;
}
}
private TestForm() { //...}
} --------------------编程问答--------------------
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
Child chi = new Child();
CheckChildOpenState(main, chi);
}
调用没有任何效果默认加载时 触发上面的事件 没任何效果! --------------------编程问答--------------------
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)--------------------编程问答--------------------
{
Child chi = new Child();
CheckChildOpenState(this,chi);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 父子窗体
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Child chi = new Child();
CheckChildOpenState(this,chi);
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
FirstForm first = new FirstForm();
CheckChildOpenState(this, first);
}
#region 窗体操作
/// <summary>
/// 名称:CheckChildOpenState
/// 功能:用子窗体的Name进行判断是否已实例化,如果存在则将他激活
/// </summary>
/// <param name="MdiForm">容器窗体</param>
/// <param name="ChildForm">子窗体</param>
public static void CheckChildOpenState(Form MdiForm, Form ChildForm)
{
foreach (Form tempChildForm in MdiForm.MdiChildren)
{
if (tempChildForm.Name == ChildForm.Name.ToString())
{
if (tempChildForm.WindowState == FormWindowState.Minimized)
{
tempChildForm.WindowState = FormWindowState.Normal;
}
tempChildForm.Activate();
return;
}
else
{
tempChildForm.Close();
}
}
ChildForm.MdiParent = MdiForm;
ChildForm.Show();
}
#endregion
}
}
感谢楼上的 好友们!问题已解决 代码实现和调用!
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Child chi = new Child();
CheckChildOpenState(this,chi); }
调用的时候 父窗体 必须写 this 写 父窗体的 new 的对象没效果!这个不知为何!!大家有些兴趣继续探讨
--------------------编程问答--------------------
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
一,可以使用单件模式。
二,可以使用Win32API,但是这种东西耗性能。以上是调用的两个API。 --------------------编程问答-------------------- [color=#FF0000]#9楼 你好 !谢谢你的支持!看不懂 有例子吗?
加我好不QQ: 1040933313[/color] --------------------编程问答-------------------- 试试 建立一个Form类型的成员变量
if(this._form != null)
{
this._form.Close();
this._form = null;
}
this._form = new NewTypeForm();
this._form.Show();
补充:.NET技术 , C#