避免在c#中重复加载表单

时间:2016-09-22 10:51:40

标签: c# winforms

我有2个窗体。Form1和form2都有一个按钮。当我单击form1中的按钮时,它显示form2,反之亦然。但是当我每次点击按钮时它会创建一个重复的表单。如何避免它。请告诉我。我的代码如下。

Form1中:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.noesis.bhurmalaidanmal"
        minSdkVersion 9
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true  
    }
    buildTypes {
        ...
    }
}

dependencies {
    ...
}

窗体2:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();
}

5 个答案:

答案 0 :(得分:2)

只需使用此方法常用,并在按钮单击

时传递像'this'这样的参数
 public bool formIsExist(Form frmOpen)
    {
        FormCollection fc = Application.OpenForms;

        foreach (Form frm in fc)
        {
            if (frm.Name == frmOpen.Name)
            {
               return true;
            }
        }

        return false;
    }

答案 1 :(得分:0)

问题是每次单击按钮时都要创建新表单。 要解决此问题,您可以将表单设置为静态。

    static Form1 form = new Form1();
    private void button1_Click(object sender, EventArgs e)
    {
        form.Show();
    }

答案 2 :(得分:0)

我的解决方案,您可以尝试

<强> Form1中

    private void button1_Click(object sender, EventArgs e)
            {
                FormCollection fc = Application.OpenForms;
                bool present = false;

                foreach (Form frm in fc)
                {

                    if (frm.Name == "Form2")
                    {
                        present = true;
                    }
                }

                if (!present)
                {
                    Form2 f2 = new Form2();
                    f2.Show();
                }
            }

<强>窗体2

 private void button1_Click(object sender, EventArgs e)
        {
            FormCollection fc = Application.OpenForms;
            bool present = false;

            foreach (Form frm in fc)
            {

                if (frm.Name == "Form1")
                {
                    present = true;
                }
            }

            if (!present)
            {
                Form1 f1 = new Form1();
                f1.Show();
            }
        }

答案 3 :(得分:0)

最简单的方法是确定目标表单是否已经存在,在这种情况下,打开它。

例如,在Form1执行此操作:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = null;
    for (int i = 0; i < Application.OpenForms.Count; i++)
    {
        if (Application.OpenForms[i] is Form2)
        {
            f2 = Application.OpenForms[i];
            break;
        }
    }

    if (f2 == null)
        f2 = new Form2();

    f2.Show();
}

要为所有表单制作一个通用方法,可以试试这个:

public static T GetForm<T>() where T : Form
{
    for (int i = 0; i < Application.OpenForms.Count; i++)
    {
        if (Application.OpenForms[i] is T)
            return (T)Application.OpenForms[i];
    }

    return null;
}

并称之为:

Form2 f2 = GetForm<Form2>();
if (f2 == null) f2 = new Form2();

答案 4 :(得分:0)

@Mostafiz'的答案几乎是正确的。它只是错过了代码来显示找到的现有形式。 (对不起,还没有50个声誉可以把它写成评论)

试试这段代码:

Form1中:

private void button1_Click(object sender, EventArgs e)
{
    Form form = null;

    //search all opened forms for one with name "Form2"
    foreach( Form frm in Application.OpenForms )
        if( frm.Name == "Form2" ) //this requires Form2 to be named "Form2"
        {
            form = frm;
            break;
        }

    //if no opened Form2 was found, create a new one
    if( form == null )
        form = new Form2();

    form.Show();
}

窗体2:

private void button1_Click(object sender, EventArgs e)
{
    Form form = null;

    //search all opened forms for one with name "Form1"
    foreach( Form frm in Application.OpenForms )
        if( frm.Name == "Form1" ) //this requires Form1 to be named "Form1"
        {
            form = frm;
            break;
        }

    //if no opened Form1 was found, create a new one
    if( form == null )
        form = new Form1();

    form.Show();
}