I want to create a form with columns of buttons. The buttons should fit all the width of form. Also I want to push it to top of form as possible. It should look like this:
|----------------------------------|
| Form caption |
|----------------------------------|
||--------------------------------||
||Button0 ||
||--------------------------------||
||--------------------------------||
||Button1 ||
||--------------------------------||
||--------------------------------||
||Button2 ||
||--------------------------------||
| |
| |
| free space |
| |
|----------------------------------|
Usually I work with C++/Qt and it's rich of layouts. As I understand c# is not so good in that. I've found that TableLayoutPanel
with 1 column can do that. The only thing I want is to push all the buttons to top.
So I've created the following code:
// panelButton was created by VS with following params:
this.panelButton = new System.Windows.Forms.TableLayoutPanel();
this.panelButton.Dock = System.Windows.Forms.DockStyle.Fill;
this.panelButton.Name = "panelButton";
this.panelButton.RowCount = 1;
for(int i = 0;i < 3;i ++)
{
Button button = new Button();
button.Dock = DockStyle.Fill;
button.Height = 40;
button.Text = "Button" + i;
button.Click += new EventHandler(delegate(object o, EventArgs args) {});
panelButton.Controls.Add(button, 0, i);
}
But the layout I get is wrong - button0
and button1
are 40px height as expected but button2
fills all the space when I expect it will be 40px.
ADDED: I've found a workaround. I add
panelButton.Controls.Add(new Control(), 0, rowIndex);
after the loop and so thet works as expected.