回到前面的表格,(C#) [英] go back to the previous form (c#)

查看:223
本文介绍了回到前面的表格,(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何去模态模式的另一种形式,就像我下面所做的:

 公共部分Form1类:表
{
公共Form1中()
{
的InitializeComponent();
}

窗体2 myNewForm =新Form2的();
私人无效的button1_Click(对象发件人,EventArgs五)
{
this.Hide();
myNewForm.ShowDialog();


}
}

这是我的第二种形式了,我怎么回到以前的形式

 公共部分类窗体2:表
{
公共窗体2()
{
的InitializeComponent();
}

私人无效Form2_Load(对象发件人,EventArgs五)
{

}
私人无效的button1_Click(对象发件人,EventArgs的)
{
this.Hide();
//我应该怎么把这里展现Form1中再次
}

}


解决方案

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

 公共部分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.

这篇关于回到前面的表格,(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆