Click here to Skip to main content
Rate this: bad
good
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 4:28am
oistrez238
Comments
__John_ at 11-Oct-12 9:49am
   
"without loads of resize-eventhandlers"
In that case, I dont know.
Rate this: bad
good
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 {
    [STAThread]
    static void Main() {
      Application.Run(new MainForm());
    }
 
    TableLayoutPanel tlp;
 
    protected override void OnShown(EventArgs e) {
      base.OnShown(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));
      Controls.Add(tlp);
      // 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);
    }
  }
}
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 495
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 200
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2014
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