简体   繁体   English

返回面板(C#)winform中的上一个表单

[英]Go back to Previous Form inside Panel (C#) winform

i Have a Dashboard Panel into which i send and dispose forms. 我有一个仪表板面板,我可以在其中发送和处理表格。 On side Buttons they Send Form to the Dashboard Panel, After User complete their work they click on another Button which send another Form to the Dashboard. 在侧面的按钮上,他们将表单发送到仪表板面板,在用户完成工作之后,他们单击另一个按钮,将另一个表单发送到仪表板。 How can i go back to the Previous Form using Back Button. 我如何使用“后退”按钮返回上一个表格。 Suppose (Employee Detail) Button send Form to Dashboard Panel, After Work done User Click (All Employees) and new Form Comes to the Panel. 假设(员工详细信息)按钮将表格发送到仪表板面板,工作完成后,用户单击(所有员工) ,新表格进入面板。 Now Problem is That i have a "Back Button" So how to get previous Form (in this case "Employee Detail") into the Dashboard Panel. 现在的问题是我有一个“后退按钮”,那么如何将以前的表单(在本例中为“ Employee Detail”)放入仪表板。

I have tried having a static form in the Main Form but in this way i can only go back only one stage. 我尝试过在主表单中使用静态表单,但是以这种方式,我只能返回一个阶段。 For Example: (Employee Detail) (All Employees) 例如:(员工详细信息)(所有员工)

But What if a have multiple forms in row 但是如果一行中有多个表单怎么办

(Employee Detail Form) (All Employees Form) (Remove Employee Form) (Etc) (员工详细信息表格)(所有员工的表格)(删除员工的表格)(等等)

namespace Agency.Forms { 命名空间Agency.Forms {

public partial class MainMenuForm : Form
{
    public static Form StaticForm;       

    private void UpdateDashBoardForm(Form myform)    //use to get form and add to panel
    {
        if (myform == null)
        {
            return;
        }
        ClearDashBoard();
        myform.TopLevel = false;
        DashBaordPanel.Controls.Add(myform);
        myform.Dock = DockStyle.Fill;
        myform.Show();
    }

    private void ClearDashBoard()
    {

        foreach (Form item in DashBaordPanel.Controls)
        {

            if (!item.Equals(StaticForm))
            {
                item.Dispose();
            }
            else
            {
                item.Visible = false;
            }

        }
    }
}

i Expect the the Back Button should get all the forms which are previously added to the dashboard, Like Forms Should store in a Stack and After Clicking back Button should Get the Top most Form from the Stack and Pop it out from stack. 我希望后退按钮可以获取以前添加到仪表板的所有表单,例如表单应存储在堆栈中,单击后退按钮应从堆栈中获取最顶部的表单并从堆栈中弹出。 在此处输入图片说明

Help: I need 2 Functions 1) will Add Form to a Static List. 帮助:我需要2个功能1)将表单添加到静态列表中。 void addForm(Form obj);

2) Will Return the recent most Form from list and Remove it from the list Form getForm(); 2)从列表中返回最近的Form并将其从列表中删除Form getForm();

So ar this point you are giving your next form ( myform ) to this class and then showing it and disposing of the current class which is StaticForm , am I right? 所以在这一点上,您StaticForm该类提供下一个表单( myform ),然后显示它并处理当前的类StaticForm ,对吗?

What you can do is add your StaticForm to a list, which keeps adding lists when you "build up" your history. 您可以做的是将StaticForm添加到列表中,当您“建立”历史记录时,它会继续添加列表。 Then when you want to use the back button you can just lookup the last form that is in the history and call back to that form. 然后,当您要使用“后退”按钮时,您只需查找历史记录中的最后一个表格,然后回调该表格即可。

Example code: 示例代码:

List<Form> FormHistory = new List<Form>( );
public static Form StaticForm;

private void UpdateDashBoardForm(Form myform)    //use to get form and add to panel
{
    FormHistory.Add( StaticForm );

    // Plus Your code.
}

private void GoBackToPreviousForm()
{
    if( FormHistory.Count > 0 )
    {
        // Plus what ever you need to do to go to the next form.
        FormHistory[ FormHistory.Count - 1 ].Show( );
        FormHistory.RemoveAt( FormHistory.Count - 1 );
    }
    else
    {
        // You are at the first loaded form.
    }
}

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

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