Close UserControl in Form Panel from button in this UserControl in C#

39 Views Asked by At

In my project, there is a Form and some UserControls, and in the Form is a ToolStrip with some buttons and one Panel, and in the UserControls is a button to close it. When click on each button in the ToolStrip, the backColor for this button changes, and open one UserControl in the Panel. Now I have two questions:

  1. How to close UserControl from the button in it? I do not want to hide it, but close it.
  2. When UserControl is closed, how to change the backColor for the ToolStrip button to the default color again?

I tried this in Form and work fine:

UserControl1 uc1;

private void toolStripBtn1_Click(object sender, EventArgs e)
        {
pnl.Controls.Add(uc1);
uc1.Dock = DockStyle.Fill;
uc1.BringToFront();
uc1.Show();
        // To change all button backColor in ToolStrip to default except Btn1
foreach (ToolStripItem btn in toolStrip.Items)
            {
                if (btn is ToolStripButton)
                {
                    btn.BackColor = Color.White;
                }
            }
            toolStripBtn1.BackColor = Color.Green;
        }

What is the code in UserControl to close it and change backColor for ToolStrip Btn1 to default again.

0

There are 0 best solutions below