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