Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Panel won't show up in UserControl

Status
Not open for further replies.

gib99

Programmer
Mar 23, 2012
51
CA
Hello,

I want to add a Panel to a UserControl but it's not showing up.

Here's my code:

Adding the UserControl (to another UserControl -- i.e. QuickStartControl):

Code:
    public partial class QuickStartControl : UserControl
    {
        public QuickStartControl()
        {
            InitializeComponent();
            this.Load += new EventHandler(QuickStartControl_Load);
        }

        private void QuickStartControl_Load(object sender, EventArgs e)
        {
            MasterFlowLayoutPanel masterFlow = new MasterFlowLayoutPanel();
            masterFlow.Dock = DockStyle.Top;
            masterFlow.BackColor = Color.Black;
            this.Controls.Add(masterFlow);
        }
    }

Adding the panel to the MasterFlowLayoutPanel UserControl:

Code:
    public partial class MasterFlowLayoutPanel : UserControl
    {
        public MasterFlowLayoutPanel()
        {
            InitializeComponent();
            this.Load += new EventHandler(MasterFlowLayoutPanel_Load);
        }

        private void MasterFlowLayoutPanel_Load(object sender, EventArgs e)
        {
            AddInitialSafeguardFlowLayoutPanel();
        }

        public void AddInitialSafeguardFlowLayoutPanel()
        {
            Panel p = new Panel();
            p.Location = new Point(0, 0);
            p.Size = new Size(100, 100);
            p.Dock = DockStyle.Fill;
            p.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            p.BackColor = Color.Red;
            this.Controls.Add(p);
	}
    }

The panel won't show up. Any idea why?
 
gibb99, could explain what you mean by the panel "not showing up"?

I used your classes exactly as specified, and then added QuickStartControl to a form and got this:
7507879332_262ab0dfc0.jpg


Your code seems to work fine, other than the fact that by setting the Anchor of the Panel in the MasterFlowLayoutPanel AFTER the Dock - it clears the Dock property.
 
When you add a control programatically, you need to set every property to make sure you create the desired behavior. That includes the Visible property.

So try to set the Visible property on each of the 2 places where you called Control.Add()

[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top