go back to the previous form (c#)

风流意气都作罢 提交于 2019-12-01 00:25:21

When you call ShowDialog on a form, it runs until the form is closed, the form's DialogResult property is set to something other than None, or a child button with a DialogResult property other than None is clicked. So you could do something like

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

Although if you're not doing anything but returning from the form when you click the button, you could just set the DialogResult property on Form2.button1 in the form designer, and you wouldn't need an event handler in Form2 at all.

I use a static Form value Current in all my multiple form apps.

public static Form1 Current;

public Form1()
{
    Current = this;

    // ... rest of constructor
}

Then in Form2

public static Form2 Current;

public Form2()
{
    Current = this;

    // ... rest of constructor
}

Then you can from your button click do,

private void button1_Click(object sender, EventArgs e)
{
    this.Hide();
    // what should i put here to show form1 again
    Form1.Current.ShowDialog(); // <-- this
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!