且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

回到前面的表格,(C#)

更新时间:2023-02-26 10:46:00

当你调用的ShowDialog 窗体上,它运行到这种形式的 DialogResult的属性设置为除,或者直到用的DialogResult 子键>比无其他财产。所以,你可以不喜欢

 公共部分Form1类
{
...
私人无效的button1_Click(对象发件人,EventArgs五)
{
this.Hide();
newform.ShowDialog();
//我们这里得到时newform的DialogResult的获取设置
this.Show();
}
}

公共部分类窗体2
{

私人无效的button1_Click(对象发件人,EventArgs五)
{
//这个隐藏表单,并导致的ShowDialog()在Form1中
this.DialogResult = DialogResult.OK返回;
}
}



但如果你没有做任何事情,但无法返回表单当您单击该按钮,你可以只设置Form2.button1的的DialogResult 属性窗体设计器,你就不需要在Form2的事件处理程序在所有


I know how to go to another form in modal mode just like what I did below:

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

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


    }
}

This is my second form, how do I go back to the previous form?

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
    }

}

When you call ShowDialog on a form, it runs until that form's DialogResult property is set to something other than None, or until 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.