3

我正在开发一个 C# Windows 窗体应用程序。我想要所有表单的一个实例。

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

我怎样才能做到这一点?

4

7 回答 7

8

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

if (System.Windows.Forms.Application.OpenForms["Form1"] as Form1 != null)
       MessageBox.Show("Form1 is opened");
 else
       MessageBox.Show("Form1 is not opened");
于 2012-10-19T05:51:54.700 回答
3
public static Form GetOpenedForm<T>() where T: Form {
    foreach (Form openForm in Application.OpenForms) {
        if (openForm.GetType() == typeof(T)) {
            return openForm;
        }
    }
    return null;
}

在您的代码中,您在其中创建ContactForm

ContactForm form = (ContactForm) GetOpenedForm<ContactForm>();
if (form == null) {
    form = new ContactForm();
    form.Show();
} else {
    form.Select();
}
于 2012-10-19T06:04:15.447 回答
2

就那么简单:

Form fc = Application.OpenForms["Form1"];
            if (fc != null)
            {
                fc.Focus();
            }
            else
            {
                Form1 f1 = new Form1();
                f1.Show();
            }
于 2012-10-19T06:02:55.430 回答
2

您可以在单击它时禁用contactButton它并打开contactForm-

private void contactButton_Click(object sender, EventArgs e)
{
  contactButton.Enabled=false;
  //code to open the contactForm
}

关闭contactForm后,您可以重新启用按钮-

contactButton.Enabled=true;
于 2012-10-19T06:32:44.013 回答
1
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();
 }
}
于 2012-10-19T06:10:40.990 回答
1

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

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();
}
于 2014-02-04T19:12:13.797 回答
1

试试这个组合

首先使联系表格成为一个全局对象

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;
}

或者,如果您希望联系表单关闭并在下次打开为新表单,请改为处理 FormClosed:

private void contactForm_FormClosed(object sender, FormClosedEventArgs e)
{
    contactForm = null;
}

然后下次单击按钮时,将捕获 null if 子句,并将表单设置为新实例并打开。

于 2012-10-19T06:00:24.153 回答