TableLayoutPanel problem

F

Fencer

Hello, I have a simple winform application with a MenuStrip, a
StatusStrip, and a RichTextBox. I want to arrange those controls in the
following manner:
MenuStrip on top, default height. Strecth across entire client area width.
StatusStrip on bottom, default height. Stretch across entire client area
width.
In between the MenuStrip and the StatusStrip I want the RichTextBox and
it should fill all remaining space (it should also resize as needed).
I tried the following code which makes use of a TableLayoutPanel:

this.Text = "TableLayoutPanel test";
this.Size = new Size(800, 600);

TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();

tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.ColumnCount = 1;
tableLayoutPanel.RowCount = 3;


this.MainMenuStrip = new MyMenuStrip();
(MyMenuStrip)this.MainMenuStrip).Create(this);
tableLayoutPanel.Controls.Add(this.MainMenuStrip);

this.textBox.Dock = DockStyle.Fill; // RichTextBox
tableLayoutPanel.Controls.Add(this.textBox);

((MyStatusStrip)this.statusStrip).SetText("Version 0.1");
tableLayoutPanel.Controls.Add(this.statusStrip);

this.Controls.Add(tableLayoutPanel);

However, something is indeed wrong, because the RichTextBox does not
occupy all the remaning vertical space in the client area. I suppose the
cell it's in isn't bigger. Everything else is as it should. How do I fix
this problem?

- Fencer
 
W

Wyrm

Hello, I have a simple winform application with a MenuStrip, a
StatusStrip, and a RichTextBox. I want to arrange those controls in the
following manner:
MenuStrip on top, default height. Strecth across entire client area width..
StatusStrip on bottom, default height. Stretch across entire client area
width.
In between the MenuStrip and the StatusStrip I want the RichTextBox and
it should fill all remaining space (it should also resize as needed).
I tried the following code which makes use of a TableLayoutPanel:

this.Text = "TableLayoutPanel test";
this.Size = new Size(800, 600);

TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();

tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.ColumnCount = 1;
tableLayoutPanel.RowCount = 3;

this.MainMenuStrip = new MyMenuStrip();
(MyMenuStrip)this.MainMenuStrip).Create(this);
tableLayoutPanel.Controls.Add(this.MainMenuStrip);

this.textBox.Dock = DockStyle.Fill; // RichTextBox
tableLayoutPanel.Controls.Add(this.textBox);

((MyStatusStrip)this.statusStrip).SetText("Version 0.1");
tableLayoutPanel.Controls.Add(this.statusStrip);

this.Controls.Add(tableLayoutPanel);

However, something is indeed wrong, because the RichTextBox does not
occupy all the remaning vertical space in the client area. I suppose the
cell it's in isn't bigger. Everything else is as it should. How do I fix
this problem?

- Fencer

Well, as far as I know (and tested), you don't need a TableLayoutPanel
for that.
You can make a MenuStrip and a StatusStrip docked at top and bottom of
your Form, then place a RichTextBox which will fill the remaining
space in the form.
(Though you have to place it last to be sure: I had some troubles with
the StatusStrip overriding the RichTextBox when I placed them in
descending order...)

If you want to use a TableLayoutPanel, make sure to set the height of
the first and last rows to match those of your MenuStrip and
StatusStrip.
(TableLayoutPanel1.RowStyles(0).Height and TableLayoutPanel1.RowStyles
(2).Height)
That way, the middle row will occupy the remaining space, and so will
your RichTextBox.
 
F

Fencer

Wyrm said:
Well, as far as I know (and tested), you don't need a TableLayoutPanel
for that.
You can make a MenuStrip and a StatusStrip docked at top and bottom of
your Form, then place a RichTextBox which will fill the remaining
space in the form.
(Though you have to place it last to be sure: I had some troubles with
the StatusStrip overriding the RichTextBox when I placed them in
descending order...)

I couldn't get that to work (tried without the TableLayoutPanel before
posting), the RichTextBox would appear under the MenuStrip.
If you want to use a TableLayoutPanel, make sure to set the height of
the first and last rows to match those of your MenuStrip and
StatusStrip.
(TableLayoutPanel1.RowStyles(0).Height and TableLayoutPanel1.RowStyles
(2).Height)
That way, the middle row will occupy the remaining space, and so will
your RichTextBox.

Doing this seems to work:
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute,
this.MainMenuStrip.Height));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute,
this.statusStrip.Height));

But if it's the proper way, that's another matter...

- Fencer
 
W

Wyrm

I couldn't get that to work (tried without the TableLayoutPanel before
posting), the RichTextBox would appear under the MenuStrip.




Doing this seems to work:
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute,
this.MainMenuStrip.Height));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute,
this.statusStrip.Height));

But if it's the proper way, that's another matter...

- Fencer

Well, it seems to me the "proper way" is to get rid of the
TableLayoutPanel if you just want these three controls in your Form.
MenuStrip and StatusStrip are made to dock to the top and bottom of
your form, leaving the remaining space to other controls.
As it seems you wish to use that default behavior, I don't see why you
bother with an additionnal container.
 
J

Jeff Johnson

I couldn't get that to work (tried without the TableLayoutPanel before
posting), the RichTextBox would appear under the MenuStrip.

Play with the Z-order of the controls (use Bring to Front) until the RTB no
longer falls under the menu strip. Seriously, this can totally be handled by
docking. The TableLayoutPanel is overkill here.
 
F

Fencer

Wyrm said:
Well, it seems to me the "proper way" is to get rid of the
TableLayoutPanel if you just want these three controls in your Form.
MenuStrip and StatusStrip are made to dock to the top and bottom of
your form, leaving the remaining space to other controls.
As it seems you wish to use that default behavior, I don't see why you
bother with an additionnal container.

Because, as I wrote, I got RichTextBox beneath the menustrip, I might go
back to the code and see if I can make it work without the
TableLayoutPanel. Thanks for your help!

- Fencer
 
F

Fencer

Jeff said:
Play with the Z-order of the controls (use Bring to Front) until the RTB no
longer falls under the menu strip. Seriously, this can totally be handled by
docking. The TableLayoutPanel is overkill here.

Thanks for the hint, I will go back to it. But it was useful exercise in
TableLayoutPanel nontheless (I'm just returning to C#).

- Fencer
 
F

Fencer

Jeff said:
Play with the Z-order of the controls (use Bring to Front) until the RTB no
longer falls under the menu strip. Seriously, this can totally be handled by
docking. The TableLayoutPanel is overkill here.

I used DockStyle.Fill to cover the entire client area and turns out
calling BringToFront() on the textbox after adding it to the form works,
it's no longer under the menustrip. It's not under the StatusStrip either.

Thanks again!

- Fencer
 
J

Jeff Johnson

I used DockStyle.Fill to cover the entire client area and turns out
calling BringToFront() on the textbox after adding it to the form works,
it's no longer under the menustrip. It's not under the StatusStrip either.

Basically, docking behavior is dependent on the order that controls are
added to the form (not necessarily physically by you in design time, but
specifically in code with the Controls.Add() method). I can't remember which
way it is, but for DockStyle.Fill to work as expected, the control it is
applied to must either be the last control added or the first control added.
Bring to Front changes the order of the Controls.Add() statements in the
designer code, ultimately making this work.

Just in case you were interested in the mechanics....
 
F

Fencer

Jeff said:
Basically, docking behavior is dependent on the order that controls are
added to the form (not necessarily physically by you in design time, but
specifically in code with the Controls.Add() method). I can't remember which
way it is, but for DockStyle.Fill to work as expected, the control it is
applied to must either be the last control added or the first control added.
Bring to Front changes the order of the Controls.Add() statements in the
designer code, ultimately making this work.

Just in case you were interested in the mechanics....

Yes, very interesting, thanks for taking your time! Right now, the
textbox is added last (I used to add the controls top-down but changed
because of a hint from someone).
I'm actually struggling quite a lot with layout issues so your help is
appreciated!
I might experiment a bit more as soon as I figure out how to add left
margin to my RichTextBox.
Padding padding = this.textBox.Margin;
padding.Left += 5;
this.textBox.Margin = padding;
doesn't seem to work or it simply doesn't do what I think it does.

- Fencer
 
F

Fencer

Jeff said:
Basically, docking behavior is dependent on the order that controls are
added to the form (not necessarily physically by you in design time, but
specifically in code with the Controls.Add() method). I can't remember which
way it is, but for DockStyle.Fill to work as expected, the control it is
applied to must either be the last control added or the first control added.
Bring to Front changes the order of the Controls.Add() statements in the
designer code, ultimately making this work.

Just in case you were interested in the mechanics....

Heh, couldn't help experimenting right away and, yup, if I add the
textbox first it doesn't fall under the MenuStrip or the StatusStrip and
I don't have to call BringToFront() on the textbox.

- Fencer
 

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

Similar Threads

TableLayoutPanel problem 0

Top