简体   繁体   中英

How to stop a panel loading each time i press it's button

I have one Windows Form application and there are several user controls in it. I show them by pressing their buttons via this method;

private void UC_Bring(Control uc) 
{
  uc.Dock = DockStyle.Fill;
  panelControls.Controls.Clear();
  panelControls.Controls.Add(uc);
}

For example; -First button is "Home" button. (this is the startup page) -Second button is "Operation" button. I click the "Operation" button and i fill some text boxes in this user control. But if i click the "Operation" button again, the user control resets itself and text boxes are being clear.

I want not to bring the user control again if mentioned user control is in use / already brought to screen. How can i do that?

Screenshot

Thanks in advance.

I tried that before

private void UC_Bring(Control uc) 
{
        uc.Dock = DockStyle.Fill;
        if (uc.Visible!=true)
        {
            panelControls.Controls.Clear();
            panelControls.Controls.Add(uc);
        }


    }

I edit this with button click event together with bringing method;

private void UC_getir(Control uc) //User control çağırma metodu
{
  uc.Dock = DockStyle.Fill;
  panelControls.Controls.Clear();
  panelControls.Controls.Add(uc);           
}
private void BtnOp_Click(object sender, EventArgs e)
{
  UC_Op ucOp = new UC_Op();
  UC_getir(ucOp);
}

You are very likely creating a new control every single time. You just need to Clear() the Panel and Add() the new UserControl each different clicks. If the UserControl is not in a container it is not visible. That mean you also don't need to fool around with the visibility. Here's a totally working solution of just that behavior.

public partial class Form1 : Form
{
    private ucHome HomeUserControl = new ucHome();
    private ucOperations OperationsUserControl = new ucOperations();
    private ucMaterials MaterialsUserControl = new ucMaterials();
    private ucSettings SettingsUserControl = new ucSettings();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        HomeUserControl.Dock = DockStyle.Fill;
        OperationsUserControl.Dock = DockStyle.Fill;
        MaterialsUserControl.Dock = DockStyle.Fill;
        SettingsUserControl.Dock = DockStyle.Fill;
    }

    private void UC_Bring(Control uc)
    {
        panelControls.Controls.Clear();
        panelControls.Controls.Add(uc);
    }

    private void btnHome_Click(object sender, EventArgs e)
    {
        UC_Bring(HomeUserControl);
    }

    private void btnOperations_Click(object sender, EventArgs e)
    {
        UC_Bring(OperationsUserControl);
    }

    private void btnMaterials_Click(object sender, EventArgs e)
    {
        UC_Bring(MaterialsUserControl);
    }

    private void btnSettings_Click(object sender, EventArgs e)
    {
        UC_Bring(SettingsUserControl);
    }
}

public class ucHome : UserControl { }
public class ucOperations : UserControl { }
public class ucMaterials : UserControl { }
public class ucSettings : UserControl { }

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