简体   繁体   English

如何使用C#返回上一个表格?

[英]How to go back to the previous form in c#?

I've been looking for a way to go back to the previous form in c#. 我一直在寻找一种方法来返回到C#中的上一个表单。 I am building a Bike Builder program, and basically it let you view your desired parts, and go back to previous form and edit your shopping cart. 我正在构建一个Bike Builder程序,基本上,它可以让您查看所需的零件,并返回到先前的表格并编辑购物车。

Form 2 will be the Shopping form, where you can select/deselect your product. 表格2将是“购物”表格,您可以在其中选择/取消选择您的产品。 Form 3 will be a review form, it will display how many parts you have been put in your cart so far, display the total. 表格3将是一个检查表,它将显示您到目前为止已放入购物车中的零件数量,并显示总数。 In form 3, there will be a button called "BACK", it will let you to go back to form 2 and update your cart. 在表格3中,将有一个名为“返回”的按钮,它将使您返回表格2并更新购物车。

// This is form 2
public partial class frmSelectParts : Form
{
// variables and functions
// .......

    public frmSelectParts()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Maximized;
    }
}

//This is form 3
public partial class frmYourCart : Form
{
    public frmYourCart()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Maximized;            
        cbbShipping.Items.Add("Standard 5 - 10 days");
        cbbShipping.Items.Add("Express 3 - 5 days");
        cbbShipping.Items.Add("Over night 1 day");
    }
}

You could do as Justin has suggested and Close or you could use frmYourCart.Hide() and frmSelectParts.Show() . 您可以按照Justin的建议Close ,也可以使用frmYourCart.Hide()frmSelectParts.Show() Using Close will result in frmSelectParts form being shown, it depends on whether you would like to keep their changes there hidden. 使用“ Close将显示frmSelectParts表单,这取决于您是否要隐藏它们的更改。

It's not exactly what you asked for but.. if it's possible, you could create only one form and add a TabControl. 它不是您所要求的,但是..如果可能的话,您只能创建一个表单并添加一个TabControl。 Then you can have one tab for each task. 然后,您可以为每个任务使用一个选项卡。

The user could go back by using the tab headers or you can still have your back button and use the SelectTab() method to show the previous tab. 用户可以通过使用选项卡标题返回,或者仍然可以使用后退按钮并使用SelectTab()方法显示上一个选项卡。

To avoid having more than one back button, you could place it just before the TabControl on your form. 为避免有多个后退按钮,可以将其放在窗体上TabControl的前面。 On the click event of the back button you would do : 在返回按钮的click事件中,您将执行以下操作:

    if (tabControl.SelectedIndex > 0) // Higher than first tab
       tabControl.SelectTab(tabControl.SelectedIndex - 1);

If I were you, I would simply call the .Show() method on the form that you want to show and then .Hide() (or .Close() ) on the form you are leaving. 如果我是你,我只需在要显示的表单上调用.Show()方法,然后在要离开的表单上.Hide() (或.Close() )。

Shopping Form: 购物形式:

private void btnReview_Click(object sender, EventArgs e)
{
   reviewForm frmReview = new reviewForm();
   frmReview.Show();
   this.Hide();  // You could also call this.Close() instead.
}   

Summary Form: 摘要表格:

private void btnBack_Click(object sender, EventArgs e)
{
   frmSummary frmSummary = new frmSummary();
   frmSummary.Show();
   this.Hide();  // You could also call this.Close() instead.
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM