TableLayoutPanel and AutoSize...

G

Guest

Hi there

I have an TableLayoutPanel with one row and two Columns.
On the first Column is a Label with AutoSize on and Anchored to the Left.
On the 2nd Column is, let's say, a Listbox with Docking set to "Fill".

Now, if I set the Text from the Label immediately after the
InitializeComponent(), the first Column grows but the Listbox from the 2nd
doesn't so it gets cut off at the end from the TableLayoutPanel.

Is there any workaround? Is this a bug or working as intended?

Thanks for help!
//Roman
 
L

Linda Liu [MSFT]

Hi Roman,

Based on my understanding, you have a TableLayoutPanel control with one row
and two columns on your form. Then you do the following things:

1. Add a label on the first column and a listbox on the second column.
2. Set the AutoSize property of the label to True and anchor it to the left
side.
3. Set the Dock property of the listbox to Fill.
4. Set the Text property of the label immediately aftert the
InitializeComponent method.

When the program is run, the first column grows but the second column
doesn't grow and the listbox within the second column gets cut off at the
right side.

If I'm off base, please feel free to correct me.

Do you set the size type of the first column to AutoSize and the AutoSize
property of the table layout panel to False? (FYI, to set the size type of
the columns in a table layout panel, select the table layout panel on the
form and click the smart tag on the top-right corner and select 'Edit Rows
and Columns'. You could set the size type of each column in the 'Column and
Rows Styles' window.)

If so, what you see when the program is run makes sense. In order to ensure
the second column remain its width when the first column grows, you should
set the AutoSize property of the table layout panel to True.

If I misunderstand you, please feel free to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
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.
 
G

Guest

Hi Linda Liu

Thanks for your answer.

I did set all columns and rows to Autosize but that doesn't help.

To show you the Problem, I setup a Project which you can download here:
http://www.flauschig.ch/transfer/projects/TableLayoutTest/TableLayoutTest.zip

Here are some screenshots of it:

This is the Form on the Designer:
http://www.flauschig.ch/transfer/projects/TableLayoutTest/form_def.jpg

This is the Form when the Program is started:
http://www.flauschig.ch/transfer/projects/TableLayoutTest/form_set.jpg
You see that the TableLayoutPanel did grow because the Label Text is longer
than before. But the TableLayoutPanel is Anchored to the Top,Left,Bottom and
Right of the Form so it (theoretically) shouldn't get bigger, instead, the
2nd column should shrink.

This is the Form when Resized:
http://www.flauschig.ch/transfer/projects/TableLayoutTest/form_set_res.jpg
As you see, when resized enough, the TableLayoutPanel looks "correct" again.
But as soon as the Form is resized to it's original size or smaller, the
TableLayoutPanel doesn't get any smaller and gets cut off at the right side
of the Form.

I hope that shows you the Problem I have.
 
L

Linda Liu [MSFT]

Hi Roman,

Sorry for my delayed reply.

I have downloaded your sample project and run the program on my machine. I
did see the problem you described.

In your sample project, since you have set the AutoSize property of the
TableLayoutPanel control true, the TableLayoutPanel control is enlarged
when the first column becomes wider. At this time, the width of the
TableLayoutPanel is bigger than that of the form, so the table layout panel
is cut off at the right side of the form.
You see that the TableLayoutPanel did grow because the Label Text is longer
than before. But the TableLayoutPanel is Anchored to the Top,Left,Bottom and
Right of the Form so it (theoretically) shouldn't get bigger, instead, the
2nd column should shrink.

In this case, the AutoSize property of the TableLayoutPanel control takes
precedence of the Anchor property.

Based on my understanding, what you want is that when the first column
becomes wider, the second column should shrink and the width of the
TableLayoutPanel remains unchanged. If I misunderstand you, please feel
free to correct me.

Firstly, we should set the AutoSize property of the TableLayoutPanel
control false.

Build the project and run it. I see that the second column shrinks but the
listbox contained in it doesn't. On the contrary, if I reduce the label's
width by setting a text with a shorter length to the label's Text, the
first column shrinks and listbox gets bigger as well as the second column.

In short, when a TableLayoutPanel control's column resizes, the control
which is contained and anchored to the 4 sides or docked as fill in the
column, can only become bigger than its initial size(when the program
starts up).

In fact, TableLayoutPanel has a property called AutoScroll. When this
property is set true, scroll bars automatically appear when the control
contents are larger than its visible area. If you set the AtoScroll
property true and run the program, you will see scroll bars appear on the
table layout panel. The scroll bars are useful, but it may not what you
really want. But at least, we realize that the above behavior is by design.

As for a workaround, we could handle the SizeChanged event of the
TableLayoutPanel control and set the size of the listbox to a proper value
according to the width of TableLayoutPanel and the label.

I have performed a test and get the following result. When both the label
and listbox contained in the table layout panel look "correct"(not cut
off), the width of the TableLayoutPanel is 15 pixels bigger than the sum of
the width of the label and the listbox. When the table layout panel or the
columns are resized, we could calculate the "correct" width of the listbox
according to this formula.

The following is a sample.

public Form1()
{
InitializeComponent();

this.tableLayoutPanel1.SizeChanged += new
EventHandler(tableLayoutPanel1_SizeChanged);
this.label1.Text = "Longer Label Text";
// the first column gets bigger now, so calculate the correct
width for the listbox
tableLayoutPanel1_SizeChanged(this,new EventArgs());
}

void tableLayoutPanel1_SizeChanged(object sender, EventArgs e)
{
this.listBox1.Width = this.tableLayoutPanel1.Width -
this.label1.Width - 15;
}

To sum up, set the AutoSize property of the TableLayoutPanel control false
and add the above code to your project.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
G

Guest

Hi Linda
In short, when a TableLayoutPanel control's column resizes, the control
which is contained and anchored to the 4 sides or docked as fill in the
column, can only become bigger than its initial size(when the program
starts up).

That's what I wanted to hear.
So this is "by Design". Thanks for clarifying this.

I first implemented something like your workaround and found another one
that seems to work fine:
On the Constructor of my Forms, I set all the "Width"s of the Controls in
the TableLayoutPanel that may shrink to 1.

Something like "comboBoxCustomers.Width = 1;"
So this Control can shrink to 1 pixel which should be enough :)
Depending on the sitiuation, I will use this workaround or use the Property
"AutoScroll" for the TableLayoutPanel you described in your last post.

Thanks for helping and clarifying!
 
G

Guest

Hi Linda
In short, when a TableLayoutPanel control's column resizes, the control
which is contained and anchored to the 4 sides or docked as fill in the
column, can only become bigger than its initial size(when the program
starts up).

That's what I wanted to hear.
So this is "by Design". Thanks for clarifying this.

I first implemented something like your workaround and found another one
that seems to work fine:
On the Constructor of my Forms, I set all the "Width"s of the Controls in
the TableLayoutPanel that may shrink to 1.

Something like "comboBoxCustomers.Width = 1;"
So this Control can shrink to 1 pixel which should be enough :)
Depending on the sitiuation, I will use this workaround or use the Property
"AutoScroll" for the TableLayoutPanel you described in your last post.

Thanks for helping and clarifying!
 
G

Guest

Hi Linda
In short, when a TableLayoutPanel control's column resizes, the control
which is contained and anchored to the 4 sides or docked as fill in the
column, can only become bigger than its initial size(when the program
starts up).

That's what I wanted to hear.
So this is "by Design". Thanks for clarifying this.

I first implemented something like your workaround and found another one
that seems to work fine:
On the Constructor of my Forms, I set all the "Width"s of the Controls in
the TableLayoutPanel that may shrink to 1.

Something like "comboBoxCustomers.Width = 1;"
So this Control can shrink to 1 pixel which should be enough :)
Depending on the sitiuation, I will use this workaround or use the Property
"AutoScroll" for the TableLayoutPanel you described in your last post.

Thanks for helping and clarifying!
 
G

Guest

Hi Linda
In short, when a TableLayoutPanel control's column resizes, the control
which is contained and anchored to the 4 sides or docked as fill in the
column, can only become bigger than its initial size(when the program
starts up).

That's what I wanted to hear.
So this is "by Design". Thanks for clarifying this.

I first implemented something like your workaround and found another one
that seems to work fine:
On the Constructor of my Forms, I set all the "Width"s of the Controls in
the TableLayoutPanel that may shrink to 1.

Something like "comboBoxCustomers.Width = 1;"
So this Control can shrink to 1 pixel which should be enough :)
Depending on the sitiuation, I will use this workaround or use the Property
"AutoScroll" for the TableLayoutPanel you described in your last post.

Thanks for helping and clarifying!
 
G

Guest

Sorry for the multiple posts, I always got an "Unknown Error" when posting
but it seems like the Text got posted anyway.
 

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