简体   繁体   中英

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.

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 {

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. void addForm(Form obj);

2) Will Return the recent most Form from list and Remove it from the list 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?

What you can do is add your StaticForm to a list, which keeps adding lists when you "build up" your history. 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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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