Click here to Skip to main content
15,937,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hallo everybody,

I just want to put a "UserControl" into a "TableLayoutPanel" at runtime.
The problem is, the UserControl always keeps its original size. So of course you can never see it properly. It doesn't seem to be difficult, but e.g. all the Dock- and Autosize-properties have absolutely no effect!

How can one adjust the size to the container (the TableLayoutPanel) and automatically resize it fitting the container (without loads of resize-eventhandlers)?

Thanks for any answer!
__John_ 11-Oct-12 9:49am    
"without loads of resize-eventhandlers"
In that case, I dont know.

In a very simple example docking works perfectly, so I suspect you need to provide more information about the problem.

using System;
using System.Windows.Forms;
namespace UserControlDocking {
  public class MainForm : Form {
    static void Main() {
      Application.Run(new MainForm());

    TableLayoutPanel tlp;

    protected override void OnShown(EventArgs e) {
      // Configure the panel 2 x 2
      tlp = new TableLayoutPanel();
      tlp.Dock = DockStyle.Fill;
      tlp.RowCount = 2;
      tlp.ColumnCount = 2;
      tlp.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;
      tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
      tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
      tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
      tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
      // Add a control to each cell
      AddDockedControl(0, 0, System.Drawing.Color.Red);
      AddDockedControl(1, 0, System.Drawing.Color.Green);
      AddDockedControl(1, 0, System.Drawing.Color.Blue);
      AddDockedControl(1, 1, System.Drawing.Color.Yellow);

    private void AddDockedControl(int col, int row, System.Drawing.Color colour) {
      UserControl newOne = new UserControl();
      newOne.BorderStyle = BorderStyle.FixedSingle;
      newOne.BackColor = colour;
      newOne.Dock = DockStyle.Fill;
      tlp.Controls.Add(newOne, col, row);
Share this answer
Now I found out my real mistake! The background picture of the control didn't adjust, so the UserControl always looked way too big. I simply forgot it's Dock.Fill property.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900