导航到新窗口 [英] Navigate to new window

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

问题描述

在c#windows窗体中,我在主窗口窗体中添加了一个按钮,当我点击该按钮时,它应该转到一个新的窗体窗口,当新窗口打开时,主窗口应该隐藏,即单击按钮时它只显示新窗体。在新的窗体窗口中,它有一个按钮可以返回主窗体窗口。任何人都可以帮助编写代码。

In c# windows form, i added a button in main windows form and when i click on that button it should go to a new form window and when the new window is opened the main window should hide, ie when the button is clicked it shows only new form window. And in new form window it have a button to go back to main form window. Anyone help to code this.

推荐答案

首先,您需要创建第二个表单,您可以将主表单作为参考传递:

First you need to create a a second form where you can pass the main form as a reference:
public partial class Form2 : Form {
private Form _parentForm;
   public Form2(Form parentForm) {
      InitializeComponent();
      _parentForm = parentForm; // store the parent form for later use
   }
}





然后你可以创建一个实例第一个表单上的表单:



then you can create an instance of that form on the first form:

private void button1_Click(object sender, EventArgs e) {
   var form = new Form2(this);
   form.Show(); // Set it to visible
   form.Activate(); // Bring it to the front
   Hide(); // Make this form invisible
}





要回到第一个表格你可以在第二个表格上做类似的事情。



And to go "back" to the first form you can do something similiar on the second form.

private void button1_Click(object sender, EventArgs e) {
   _parentForm.Show(); // Set it to visible
   _parentForm.Activate(); // Bring it to the front
   Dispose(); // Release the form and all of it's resources
}


希望这个帮助,





在第一种形式中您已经编写了以下代码< br $>


Hope this help,


in the First form you have write the following code

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

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





和第二种形式这样写。





and the second form write like this.

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

        private void back_Click(object sender, EventArgs e)
        {
           Form1 ob = new Form1();
           ob.ShowDialog();
       this.Hide();
        }
    }





所以这就是我们从一个窗口导航到另一个窗口的方式。



so this is how we navigate from one window form to another.


看看这个Stack Overflow问题:在表单之间切换 [ ^ ]。
Have a look at this Stack Overflow question: "Switching between forms"[^].


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

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