M
michael sorens
When I set a label to span 2 columns of a 2-column TableLayoutPanel, set both
of these plus the containing form to AutoSize, then at runtime fill in the
label, the autosizing does not take the column spanning into account. That
is, it autosizes so that the label text is entirely in column 1 even though
the label itself does physically span both columns. This seems to only occur
when I dynamically add a row. For rows that I added at design time it does
not exhibit this problem. The code below is a very short program to
demonstrate this.
(1) Run the program. Observe that the label in the first row spans both
columns of the 2-column TableLayoutPanel. Click on the label to add a word to
it. Do this repeatedly and you will observe that the label correctly spans
both columns. Once the label runs out of room, the label, its enclosing
panel, and the form itself then grow to accommodate, as desired.
(2) Now shift-click on the same label. This still adds a word to the
original row but it also dynamically adds a row with the same text in a
smaller font. Note the peculiar way that autosizing now affects everything.
No matter how many times you shift-click, the text of the label in new rows
will never stretch into column 2. My conclusion is that autosizing is not
taking into account the column span of the control. (Actually, the growth
seems to occur well before the text even reaches the limit of column 1, which
is also puzzling.)
Am I missing some settings in my dynamic row creation??
***************************** Program.cs *****************************
using System;
using System.Windows.Forms;
namespace ColumnSpanTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
***************************** Form1.cs *****************************
using System;
using System.Windows.Forms;
namespace ColumnSpanTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int iteration = 0;
string[] word = { "one", "two", "three", "four", "five" };
private void label1_Click(object sender, EventArgs e)
{
label1.Text += ' ' + word[iteration++ % word.Length];
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
AddRow(label1.Text);
}
private void AddRow(string text)
{
Label categoryLabel = new Label();
categoryLabel.AutoSize = true;
categoryLabel.Text = text;
categoryLabel.Dock = DockStyle.Fill;
tableLayoutPanel1.RowCount++;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute,
24));
tableLayoutPanel1.Controls.Add(categoryLabel, 0,
tableLayoutPanel1.RowCount - 1);
tableLayoutPanel1.SetColumnSpan(categoryLabel,
tableLayoutPanel1.ColumnCount);
}
}
}
***************************** Form1.Designer.cs *****************************
namespace ColumnSpanTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new
System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AutoSize = true;
this.tableLayoutPanel1.CellBorderStyle =
System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 78);
this.tableLayoutPanel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.tableLayoutPanel1.SetColumnSpan(this.label1, 2);
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 20F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(4, 1);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(284, 37);
this.label1.TabIndex = 0;
this.label1.Text = "Click here";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(292, 78);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label label1;
}
}
*****************************************************************************
of these plus the containing form to AutoSize, then at runtime fill in the
label, the autosizing does not take the column spanning into account. That
is, it autosizes so that the label text is entirely in column 1 even though
the label itself does physically span both columns. This seems to only occur
when I dynamically add a row. For rows that I added at design time it does
not exhibit this problem. The code below is a very short program to
demonstrate this.
(1) Run the program. Observe that the label in the first row spans both
columns of the 2-column TableLayoutPanel. Click on the label to add a word to
it. Do this repeatedly and you will observe that the label correctly spans
both columns. Once the label runs out of room, the label, its enclosing
panel, and the form itself then grow to accommodate, as desired.
(2) Now shift-click on the same label. This still adds a word to the
original row but it also dynamically adds a row with the same text in a
smaller font. Note the peculiar way that autosizing now affects everything.
No matter how many times you shift-click, the text of the label in new rows
will never stretch into column 2. My conclusion is that autosizing is not
taking into account the column span of the control. (Actually, the growth
seems to occur well before the text even reaches the limit of column 1, which
is also puzzling.)
Am I missing some settings in my dynamic row creation??
***************************** Program.cs *****************************
using System;
using System.Windows.Forms;
namespace ColumnSpanTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
***************************** Form1.cs *****************************
using System;
using System.Windows.Forms;
namespace ColumnSpanTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int iteration = 0;
string[] word = { "one", "two", "three", "four", "five" };
private void label1_Click(object sender, EventArgs e)
{
label1.Text += ' ' + word[iteration++ % word.Length];
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
AddRow(label1.Text);
}
private void AddRow(string text)
{
Label categoryLabel = new Label();
categoryLabel.AutoSize = true;
categoryLabel.Text = text;
categoryLabel.Dock = DockStyle.Fill;
tableLayoutPanel1.RowCount++;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute,
24));
tableLayoutPanel1.Controls.Add(categoryLabel, 0,
tableLayoutPanel1.RowCount - 1);
tableLayoutPanel1.SetColumnSpan(categoryLabel,
tableLayoutPanel1.ColumnCount);
}
}
}
***************************** Form1.Designer.cs *****************************
namespace ColumnSpanTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new
System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AutoSize = true;
this.tableLayoutPanel1.CellBorderStyle =
System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new
System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 78);
this.tableLayoutPanel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.tableLayoutPanel1.SetColumnSpan(this.label1, 2);
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 20F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(4, 1);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(284, 37);
this.label1.TabIndex = 0;
this.label1.Text = "Click here";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(292, 78);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label label1;
}
}
*****************************************************************************