Autosizing and column spanning conundrum

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;
}
}

*****************************************************************************
 
L

Linda Liu[MSFT]

Hi Michael,

I performed a test based on your sample code and did see the problem on my
side.

If I set the column span of both the label1 and the label created at run
time to 1, the problem doesn't exist.

To observe results more clearly, I set the BackColor of the label1 to the
color of ActiveCaption and set the Dock property of the label1 to None. To
isolate the problem, I copy the label1 and paste it to the row 1 of the
TableLayoutPanel and the new Label named label2. Attach the label1_Click
event handler to the Click event of the label2 and modify it as follows:

private void label1_Click(object sender, EventArgs e)
{
(sender as Label).Text += ' ' + word[iteration++ % word.Length];
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
AddRow((sender as Label).Text);
}

Run the application and resize the form to its minimum width with the Mouse
and then click the label2 for several times. I see that the label2 extends
with its text and the form extends as well. But there's a space between the
right border of the label2 and that of the TableLayoutPanel, i.e. the
label2 doesn't use up the entire space of the row 1.

So it seems that the TableLayoutPanel doesn't calculate the expected space
very accurately when the hosted controls span more than 1 column in the
above case. I think this behavior is by design.

Normally, we only make those controls whose sizes won't change at run time
span more than 1 column within a TableLayoutPanel.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu[MSFT]

Hi Michael,

Thank you for your quick reply!

IMO, this behavior is by design.

Thank you for submitting a feedback on this issue in the Microsoft Connect
web site. It will definitely help improve our product.

Have a good weekend!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top