Click here to Skip to main content
11,707,167 members (50,201 online)
Rate this: bad
Please Sign up or sign in to vote.
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!
Posted 11-Oct-12 3:28am
__John_ at 11-Oct-12 9:49am
"without loads of resize-eventhandlers"
In that case, I dont know.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 145
1 OriginalGriff 141
2 chainerlt 110
3 Maciej Los 80
4 ppolymorphe 79
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,512
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100