无法在Windows窗体之间导航 [英] Unable to navigate between windows forms

查看:97
本文介绍了无法在Windows窗体之间导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是Windows应用程序的新手。我有两种形式,我想在这些形式之间导航。当我在form1上单击按钮时,它将移动到form2。问题是当我单击form2上的按钮导航到form1时,它会关闭应用程序。我使用以下代码



Form1:

Hi,
I am new to windows application. I have two forms and i want to navigate between these forms. When I Click button on form1, it moves to form2. The problem is when i click on button on form2 to navigate to form1, it closes the application. I am using the following code

Form1:

private void button1_Click_1(object sender, EventArgs e)
       {
           var frm2 = new Form2();
           frm2.Show();
           this.Close();
       }





Form2:



Form2:

private void button2_Click(object sender, EventArgs e)
        {
            
            var frm1= new Form1();           
            frm1.Show();
            this.Close();
        }





我会感谢你的帮助。

问候

Usama



I Shall be thankful for your help.
Regards
Usama

推荐答案

使用您到目前为止给出的代码,每次单击Form2中的按钮时,您将创建一个Form1的新实例,这是可能不是你想要的。



要显示原始Form1,你应该在Form2中创建一个隐藏的Form1实例订阅的事件,这样它就可以显示出来:< br $> b $ b

Using the code you have been given so far you will be creating a new instance of Form1 each time the button in Form2 is clicked, which is probably not what you are wanting.

To show your original Form1 you should create an event in Form2 that your hidden Form1 instance subscribes to, so it can show itself:

// In Form2
public event EventHandler ButtonClicked;

protected virtual void OnButtonClicked(EventArgs e)
{
    EventHandler eh = ButtonClicked;
    if (eh != null)
        eh(this, e);
}
private void button1_Click(object sender, EventArgs e)
{
    OnButtonClicked(e);
}




// In Form1
private void button1_Click(object sender, EventArgs e)
{
    // A new Form2 instance. Cache this in a field and only create if null if you want the same Form2 instance each time
    Form2 form2 = new Form2();
    form2.ButtonClicked += new EventHandler(form2_ButtonClicked);
    form2.Show();
    Hide();
}
private void form2_ButtonClicked(object sender, EventArgs e)
{
    Form form2 = sender as Form;
    if (form2 != null)
    {
        form2.Close();
        Show();
    }
}





另一种方法是将构造函数注入到Form2中,以便它接收Form1实例,但这是很少有好主意,因为它将Form2耦合到Form1。



Another way is Constructor Injection into Form2 so it receives the Form1 instance, but this is rarely a good idea as it couples Form2 to Form1.


Form1:



Form1:

private void button1_Click_1(object sender, EventArgs e)
        {
            var frm2 = new Form2();
            frm2.Show();
            this.Hide()
        }







Form2


:

Form2

private void button2_Click(object sender, EventArgs e)
        {
            
            var frm1= new Form1();           
            frm1.Show();
            this.Close();
        }



希望这对您有所帮助! :)


Hope this will help you! :)


当然,因为你有主要表单 [ ^ ]。您可以改变可见性,而不是一直实例化表单!
Of course, because you have a main form[^]. You could change visibility instead, and don't instantiate the form all the time!


这篇关于无法在Windows窗体之间导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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