473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Autosizing and column spanning conundrum

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(fals e);
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.TableLayoutPanelCellBorderSty le.Single;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Fo rms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Fo rms.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;
}
}

************************************************** ***************************

Jul 23 '08 #1
3 2208
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 24 '08 #2
Thanks for validating the issue. Though you say you believe it is by design,
you also seem to agree with me that the behavior is faulty... Just wanted to
let you know that I have therefore submitted this as a product issue in
Connect
(https://connect.microsoft.com/Visual...dbackID=357655)
Jul 24 '08 #3
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:
ms****@microsoft.com.

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

Jul 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
2
by: _mario.lat | last post by:
kruskal:minimum spanning tree. how to do? I'd like to find the minimum spanning tree with kruskal algorithm. There is a code (in C++) written? which contenitor do you suggest (Vector, set, ...)?...
0
by: Daniel Déchelotte | last post by:
Hi, I would like to present one column, extending from the top to the bottom of the viewport, with textured "drop-down shadow" and a textured background. A bit like http://www.blogger.com/, but...
2
by: ASGMikeG | last post by:
Hi, Does anyone have a bit of code that will auto-size the columns on a ListView to fit the column headers AND data in those columns ? Regards Michael
4
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
4
by: Jim Owen | last post by:
I am storing all my application data in the application cache. Anytime I have a method as part of an asp.net form, I need to access the objects in the cache. The only way I can think of to do this...
1
by: Sigmazen | last post by:
Hi I am looking in to utilising the new functionality of DPSIs on zOS UDB (DB2 v8), but I have a question regarding the following scenario. - The first table is an Account table whose columns...
1
by: jenny22 | last post by:
i have a problem i am very new to c++ and want to construct a minimum spanning tree for 8 stocks i have calculated in excel the relevant formulas and know the weights of each of gthe vertices but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.