C# 所有WinForm窗体的单个实例

C# 所有WinForm窗体的单个实例,c#,winforms,C#,Winforms,我正在开发一个C#应用程序。我想有一个所有形式的单一实例 因此,当用户点击联系人按钮时,例如,两次,而不是有两个联系人表单,我希望将单实例联系人表单放在前面 如何实现这一点?在使用创建和显示表单之前,请检查表单是否存在于打开的表单集合中 试试这个套餐 首先使接触形成一个全局对象 private ContactForm contactForm; 然后您的联系人按钮处理程序: private void contactButton_Click(object sender, EventArgs e)

我正在开发一个C#应用程序。我想有一个所有形式的单一实例

因此,当用户点击联系人按钮时,例如,两次,而不是有两个联系人表单,我希望将单实例联系人表单放在前面


如何实现这一点?

在使用创建和显示表单之前,请检查表单是否存在于打开的表单集合中

试试这个套餐

首先使接触形成一个全局对象

private ContactForm contactForm;
然后您的联系人按钮处理程序:

private void contactButton_Click(object sender, EventArgs e)
{
    if (contactForm == null)
    {
       contactForm = new ContactForm();
       contactForm.FormClosing += new FormClosingEventHandler(contactForm_FormClosing);
    }
    contactForm.Show();
}
然后处理ContactForm的FormClosing事件以隐藏它,而不是关闭它:

private void contactForm_FormClosing(object sender, FormClosingEventArgs e)
{
    contactForm.Hide();
    e.Cancel = true;
}
或者,如果希望联系人表单关闭,并在下次作为新表单打开,请改为处理已关闭的表单:

private void contactForm_FormClosed(object sender, FormClosedEventArgs e)
{
    contactForm = null;
}
然后,下次单击按钮时,将捕获null if子句,并将表单设置为新实例并打开。

简单如下:

Form fc = Application.OpenForms["Form1"];
            if (fc != null)
            {
                fc.Focus();
            }
            else
            {
                Form1 f1 = new Form1();
                f1.Show();
            }

单击
联系人按钮时,您可以禁用该按钮
,然后打开
联系人表单
-

private void contactButton_Click(object sender, EventArgs e)
{
  contactButton.Enabled=false;
  //code to open the contactForm
}
联系人表单
关闭时,您可以重新启用该按钮-

contactButton.Enabled=true;

我同意奥蒂尔的回答。您还可以添加一个WindowsState,因为如果表单最小化,它将不会显示。答案可用于获取还原方法

ContactForm form = (ContactForm) GetOpenedForm<ContactForm>();
if (form == null) {
    form = new ContactForm();
    form.Show();
} else {
    //use this (if you want it to be restored to normal and focused)
    //no need of extension method
    //form.WindowState = FormWindowState.Normal;
    //form.Select();

    //or use this if you want it to be restored as previous state and focused
    //You need a Restore extension method that can be found in the link above
    form.Focus();
    form.Restore();
}
ContactForm=(ContactForm)GetOpenedForm();
if(form==null){
form=新的ContactForm();
form.Show();
}否则{
//使用此选项(如果您希望它恢复正常并聚焦)
//不需要扩展方法
//form.WindowState=FormWindowState.Normal;
//form.Select();
//或者,如果希望将其恢复为以前的状态并聚焦,请使用此选项
//您需要一个可以在上面的链接中找到的还原扩展方法
form.Focus();
form.Restore();
}

生成静态方法的返回值
T
。在这种情况下,您的用法中不需要强制转换:
ContactForm form=GetOpenedForm()
使用LINQ,您可以使助手方法变成一行:
返回Application.OpenForms.OfType.FirstOrDefault()
如果表单最小化,这将不起作用,因此您可以添加WindowsState Normal。看到我的答案了吗below@AlejandrodelR皮奥:接得好!当
WindowState
被最大化(或在被最小化之前被最大化)时,这将不能完美地工作。您可以使用
Restore
方法,而不是修改
WindowState
属性。是的,在这种情况下,它将被迫进入正常状态。这取决于你想要什么。谢谢。我测试了恢复功能。如果我们使用恢复,我们还必须将焦点添加到表单中。否则,它会后退。我编辑了我的答案。事实上,我保留了
Select
。但我想这是一样的<代码>form.Restore();form.Select()
Form2 form2 = null;
private void button1_Click(object sender, EventArgs e)
{
 bool isFormExists = false;
 foreach (Form openForm in Application.OpenForms)
 {
  if (openForm == form2 && openForm!=null)
  {
   openForm.Focus();
   isFormExists = true;
   break;
  }
 }

 if (!isFormExists)
 {
  form2 = new Form2();
  form2.Show();
 }
}
private void contactButton_Click(object sender, EventArgs e)
{
  contactButton.Enabled=false;
  //code to open the contactForm
}
contactButton.Enabled=true;
ContactForm form = (ContactForm) GetOpenedForm<ContactForm>();
if (form == null) {
    form = new ContactForm();
    form.Show();
} else {
    //use this (if you want it to be restored to normal and focused)
    //no need of extension method
    //form.WindowState = FormWindowState.Normal;
    //form.Select();

    //or use this if you want it to be restored as previous state and focused
    //You need a Restore extension method that can be found in the link above
    form.Focus();
    form.Restore();
}