using System.Collections.Generic; using System.Windows.Forms; /// /// Manages navigation between different views. /// public class NavigationManager { private Stack navigationHistory = new Stack(); // Holds previous views private Panel panelContainer; // Reference to the panel container where views are switched private Control mainPanel; // Reference to the main panel (start page) public NavigationManager(Panel panel) { this.panelContainer = panel; this.mainPanel = panel.Controls.Count > 0 ? panel.Controls[0] : null; } /// /// Checks if there are views in the navigation history to go back to. /// public bool CanGoBack() { return navigationHistory.Count > 0; } /// /// Adds the current control to the navigation history. /// private void AddToHistory() { if (panelContainer.Controls.Count > 0) { // Add the currently visible control to the history stack navigationHistory.Push(panelContainer.Controls[0]); } } /// /// Switches to a new view and adds the current view to the navigation history. /// /// The new view to display. public void SwitchView(Control newView) { AddToHistory(); // Save the current view before switching // Clear the container and display the new view panelContainer.Controls.Clear(); panelContainer.Controls.Add(newView); newView.Dock = DockStyle.Fill; newView.BringToFront(); } /// /// Navigates back to the previous view in the history, if available. /// public void GoBack() { if (CanGoBack()) { // Pop the last view from the history stack and display it Control previousView = navigationHistory.Pop(); panelContainer.Controls.Clear(); panelContainer.Controls.Add(previousView); previousView.Dock = DockStyle.Fill; previousView.BringToFront(); } } /// /// Clears the navigation history. /// public void ClearHistory() { navigationHistory.Clear(); } /// /// Switches directly to the main panel, clearing the navigation history. /// public void GoToMain() { if (mainPanel != null) { navigationHistory.Clear(); // Clear the navigation history panelContainer.Controls.Clear(); panelContainer.Controls.Add(mainPanel); mainPanel.Dock = DockStyle.Fill; mainPanel.BringToFront(); } } }