写了个demo,希望对楼主能有所帮助 CFMain.cs 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 OneFormDemo { public partial class Frmmain : Form { public static List<CFone> openedForm = new List<CFone>(); public Frmmain() { InitializeComponent(); } private bool findForm(CFone fr) { foreach (CFone f in openedForm) { if (f.number == fr.number) { f.Activate(); return true; } } return false; } private void button1_Click(object sender, EventArgs e) { CFone one = new CFone("1"); if (!findForm(one)) { openedForm.Add(one); one.Show(); } } private void button2_Click(object sender, EventArgs e) { CFone two = new CFone("2"); if (!findForm(two)) { openedForm.Add(two); two.Show(); } } } } CFone.cs 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 OneFormDemo { public partial class CFone : Form { public string number; public CFone() { InitializeComponent(); } public CFone(string number) { this.number = number; InitializeComponent(); } private void CFone_FormClosed(object sender, FormClosedEventArgs e) { Frmmain.openedForm.Remove(this); } private void CFone_Load(object sender, EventArgs e) { this.Text = this.number; } } }
支持,实现用getInstance()方法
2.为每个对话框添加一个Id属性原来区别去他类似对话框,比如QQ号码,
3.在每次实例化对话框之前在窗体容器中判断一下是否已经存在相同QQ号的窗体,如果存在就调用该窗体的Form.Show或者Form.Active()方法。如果存在就创建一个,同时保存该创建的窗体到集合中。
4.如果关闭窗体,在窗体关闭前,从集合中移除该窗体项。
可参考myqq.
CFMain.cs
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 OneFormDemo
{
public partial class Frmmain : Form
{
public static List<CFone> openedForm = new List<CFone>();
public Frmmain()
{
InitializeComponent();
} private bool findForm(CFone fr)
{
foreach (CFone f in openedForm)
{
if (f.number == fr.number)
{
f.Activate();
return true;
}
}
return false;
} private void button1_Click(object sender, EventArgs e)
{
CFone one = new CFone("1");
if (!findForm(one))
{
openedForm.Add(one);
one.Show();
}
} private void button2_Click(object sender, EventArgs e)
{
CFone two = new CFone("2");
if (!findForm(two))
{
openedForm.Add(two);
two.Show();
}
}
}
}
CFone.cs
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 OneFormDemo
{
public partial class CFone : Form
{
public string number;
public CFone()
{
InitializeComponent();
} public CFone(string number)
{
this.number = number;
InitializeComponent();
} private void CFone_FormClosed(object sender, FormClosedEventArgs e)
{
Frmmain.openedForm.Remove(this);
} private void CFone_Load(object sender, EventArgs e)
{
this.Text = this.number;
}
}
}
show()
else
(new Form())。show
private void 基础资料物料ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dmmcform == null || !dmmcform.Created)
{
dmmcform = new DmmcForm();
dmmcform.ActivemdichildForm += new FormEventHandler(editToolStaTxt);
dmmcform.MdiParent = this;
dmmcform.TableName = "dmmc";
dmmcform.Show();
}
else
{
dmmcform.Activate();
}
}
//
与Form相关的常见问题大致分为如下的四类问题。第一类问题:如何控制窗体的显示顺序;第二类问题:窗体之间的对象如何相互引用或操作;第三类问题:如何处理窗体唯一性问题;最后一个问题:如何合理的关闭窗体或程序。
return forms["好友1"];