我如何打开一个表单并通过一个按钮将其关闭

时间:2018-07-23 09:47:17

标签: c# winforms

我打开了一个表单,但无法通过相同按钮关闭。我该怎么做? 我尝试这样做:

...
{
    var openform = new Form2();

    if (openform != null) openform.Show(); 
    else openform.Hide();
}

5 个答案:

答案 0 :(得分:1)

看来您必须实现以下逻辑:

  1. 如果有打开 Form2实例,请 close
  2. 否则 create show 显示新的Form2实例。

如果是您的情况,我们应该寻找打开的Form2实例;然后使用new创建它(如果需要)

  using System.Linq;

  ... 

  // Search: do we have opened Form2 instances?
  Form2 openform = Application
    .OpenForms        // among all opened forms 
    .OfType<Form2>()  // of type Form2
    .LastOrDefault(); // in case we have several instances, let's choose the last one

  if (null == openform) {   // no Form2 instance has been found
    openform = new Form2(); 

    openform.Show();
  }   
  else {                    // Instance (openform) has been found 
    openform.Close(); // Or openform.Hide();   
  }

答案 1 :(得分:0)

private Form2 form2;

private void button1_Click(object sender, EventArgs e)
{
    if (form2 == null || form2.IsDisposed)
    {
        // Either no form has been created or the last one created has been closed.
        form2 = new Form2();
        form2.Show();
    }
    else
    {
        form2.Close();
    }
}

如果您喜欢空传播:

if (form2?.IsDisposed == false)
{
    form2.Close();
}
else
{
    // Either no form has been created or the last one created has been closed.
    form2 = new Form2();
    form2.Show();
}

答案 2 :(得分:0)

像下面一样使用bool变量作为打开按钮,并使openform为全局。

<div id="app">
  <v-app id="inspire">
    <v-container mt-1 grid-list-xl">
      <v-layout row wrap justify-space-around>
        <v-flex d-flex sm6 md6>
          <v-layout row wrap>
            <v-flex d-flex>
              <v-card class="flexcard" dark tile flat>
                <v-card-title class="card__title justify-center">PROFILE</v-card-title>
                <form @submit.prevent="onSubmit()">
                <v-card-text class="card__text grow">
                  <v-text-field label="Email" v-model="email" data-vv-name="email" 
                     v-validate="'required|email'" :error-messages="errors.collect('email')" prepend-icon="email"></v-text-field>
                 <v-text-field type="password" v-model="password" label="Password" v-validate="'required|min:6'"  data-vv-name="password" :error-messages="errors.collect('password')" ref="password" prepend-icon="lock"></v-text-field>
                 <v-text-field type="password" v-model="match_password" label="Match Password" v-validate="'required|confirmed:password'" :error-messages="errors.collect('match_password')"data-vv-as="password"data-vv-name="match_password" prepend-icon="lock"></v-text-field>
                </v-card-text>
                <v-card-actions>
                <v-btn round  type="submit">SUBMIT</v-btn>
                </v-card-action>
                </form>
              </v-card>
            </v-flex>
          </v-layout>
        </v-flex>
     </v-layout>
      </v-container>
  </v-app>

方法,并在类中作为全局变量。

var openform = new Form2();      // it must be defined out 

按钮内点击方法

private static bool isOpen = true; 

经过测试的代码

if (isOpen) openform.Show();
else openform.Hide();

isOpen = !isOpen;

选中此一项进行测试。 https://youtu.be/o9I77dhEvYg

答案 3 :(得分:0)

如果其他形式的窗口像窗口中的所有其他形式一样被关闭,我认为对操作员来说将更加直观:按下“关闭”按钮,单击右上角的十字,选择ALT-F4,等等。

但是,如果您真的想从主窗体中关闭另一个窗体,则不应该只关闭它,而应该很好地询问另一个窗体是否足以关闭自己。这样,您就可以让另一种形式向操作员询问一些问题,例如询问是否需要保存更改的项目。

private Form myForm = null;

public void OnButton1_Clicked(object sender, ...)
{
    if (this.myform == null)
    {   // not shown yet. Show it now:
        this.myForm = new Form2()
        this.myForm. properties = ...

        // make sure I get notified if the Form closes in any way:
        this.myForm.Closed += onMyFormClosed;
        // show the form
        this.myform.Show(this);
    }
    else
    {   // ask the form nicely to close itself
        this.CloseForm(); 
        // this might (or might not) lead to the event Form.Closed
    }
}

private void OnMyFormClosed(object sender, ...)
{
     if (!object.ReferenceEquals(sender, this.myForm))
     {    // someone else is closed. I have nothing to do with this
          return;
     }

     // if here, my Form is closed. Save to Dispose and assign Null
     this.myForm.Dispose();
     this.myForm = null;
}
}

public void ShowFo

答案 4 :(得分:-1)

也许这样会帮助您:

Form2 openform = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() > 0)
    openform.Hide();

openform.Show();

尽管我不太了解您所提出问题的内容-您可能需要修改代码。您也可以选中此How to check if a windows form is already open, and close it if it is?