【问题标题】:go back to the previous form (c#)返回上一个表单(c#)
【发布时间】:2011-09-09 18:05:35
【问题描述】:

我知道如何在模态模式下转到另一个表单,就像我在下面所做的那样:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Form2 myNewForm = new Form2();
    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        myNewForm.ShowDialog();


    }
}

这是我的第二个表格,我该如何回到之前的表格?

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        // what should i put here to show form1 again
    }

}

【问题讨论】:

    标签: c# forms modal-dialog back-button


    【解决方案1】:

    当您在表单上调用 ShowDialog 时,它会一直运行,直到表单关闭,表单的 DialogResult 属性设置为 None 以外的值,或者具有 DialogResult 属性而不是 @ 的子按钮点击 987654325@。所以你可以做类似的事情

    public partial class Form1
    {
        ...
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            newform.ShowDialog();
            // We get here when newform's DialogResult gets set
            this.Show();
        }
    }
    
    public partial class Form2
    {
        ...
        private void button1_Click(object sender, EventArgs e)
        {
            // This hides the form, and causes ShowDialog() to return in your Form1
            this.DialogResult = DialogResult.OK;
        }
    }
    

    虽然如果您在单击按钮时除了从表单返回之外什么都不做,您可以在表单设计器中设置 Form2.button1 上的 DialogResult 属性,并且您不需要事件处理程序完全是Form2。

    【讨论】:

      【解决方案2】:

      我在所有多表单应用程序中都使用静态表单值 Current。

      public static Form1 Current;
      
      public Form1()
      {
          Current = this;
      
          // ... rest of constructor
      }
      

      然后在Form2中

      public static Form2 Current;
      
      public Form2()
      {
          Current = this;
      
          // ... rest of constructor
      }
      

      然后你可以从你的按钮点击做,

      private void button1_Click(object sender, EventArgs e)
      {
          this.Hide();
          // what should i put here to show form1 again
          Form1.Current.ShowDialog(); // <-- this
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        相关资源
        最近更新 更多