I may not be using the right control for what I want. I\'m filling a table with controls and I want each column to automatically size to the controls contained within it. Fo
You could use multiple splitContainers one inside another's panel. But don't use many nested panels, you might get resize/redraw issues like this.
You can find many examples if you want to use SplitContainer.
Sorry, but what's wrong with having the columns set to Autosize? That's what TableLayoutPanel does, size columns to the fit the controls within it. Expanding the table and having a scrollbar would require you to set the tables Autosize property to true, then sit the TableLayoutPanel within another panel that has scrollbars enabled. But the column sizing should work out of the box if unless I'm misunderstanding your requirements.
Just to make sure, you are going to the columns property and setting each column's SizeType to AutoSize right? Not just the AutoSize property of the table itself?
is this what you want?
-Post code:
Thanks for the code. I'd suggest that you use designer to do a lot of this. At least to set up the columns, set them to autosize, and add the heading labels.
You also might want to check out the Datagrid control and bind that to your location list.
To get this method working though:
1) the reason your columns look the same size is because the heading labels you're using aren't autosizing. They're all x pixels wide and that's stretching the columns. Do this:
Label lab = new Label();
lab.AutoSize = true;
lab.Text = "Location";
tableLayoutPanel1.Controls.Add(lab, 0, 0);
You'll also need to set the AutoSize property to true on the CheckBox control and any other labels you add as content.
2) Setting the RowCount and ColumnCount won't affect the RowStyles or ColumnStyles collection. You've got 7 Columns but only 2 ColumnStyles. Try:
tableLayoutPanel1.ColumnStyles.Clear();
for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
{
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
}
tableLayoutPanel1.RowStyles.Clear();
for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
Only other thing to look out for is that some controls will be misaligned in the rows (labels appear too high for example). To fix that set the Margin property, normally to 3,6,3,0 to align them with textboxes and checkboxes etc.
You'll need to handle the ControlAdded event, then resize the column if the new control's width is greater than the column's width...
You say you don't want to fiddle with measuring, but if you're resizing, you kind of have to. Fonts and such shouldn't come into it if you're careful about the TextAlign of your checkboxes etc...