TableLayoutPanel problem

W

WP

Hello, I'm trying to write a C# version of a simple Java program (that
uses swing for its gui). I'm using Visual C# 9.0 SP1 (2008) and I'm
using winforms for the gui.

Here's my problem. I want a TableLayoutPanel to cover the entire client
area of my form. The TableLayoutPanel should have two rows and five
columns and each cell should have the same size. Most cells will contain
buttons but some cells will contains labels. The control in a cell
should stretch across the entire cell. Some buttons will have an image
instead of text and I want the image to be centered when the cell is
bigger than the image, if that's possible. Labels should have
left-aligned text in the vertical middle.

Here's my attempt:

Here's what I do with the TableLayoutPanel:
tblLayoutPanel.Dock = DockStyle.Fill;
tblLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
tblLayoutPanel.ColumnCount = 5;
tblLayoutPanel.RowCount = 2;
tblLayoutPanel.AutoSize = true;
tblLayoutPanel.ColumnStyles.Clear();
tblLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20));
tblLayoutPanel.RowStyles.Clear();
tblLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50));

Here's how buttons and labels are created:
private Button CreateButton(string fileName, string buttonText) {
Button b = new Button();

if (fileName != null)
{
b.BackgroundImage = Image.FromFile(fileName);
}
else
{
b.Text = buttonText;
}

b.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |
AnchorStyles.Left | AnchorStyles.Right;

return b;
}

private Label CreateLabel(string initialText) {
Label l = new Label();

l.Text = initialText;
l.TextAlign = ContentAlignment.MiddleLeft;

l.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |
AnchorStyles.Left | AnchorStyles.Right;

return l;
}

With properties above I dont get the desired behavior, here's how it looks:
The TableLayoutPanel fills the entire client area of the form: Good.
The TableLayoutPanel has two rows with five columns: Good.
The contents of the cells (a label or a button) seems to fill the entire
cell: Good.
However, the cells are not of equal size (neither width nor height). The
top row, which contains just buttons with images, is much much higher
than the bottom row (which contains labels and buttons). The first
column is much, much wider than the rest. The second column is the
middle of the bunch when it comes to width and the last three columns
have the same width I think. Also, the button image is repeated when the
cell is bigger than the image itself but maybe there's no simple way
around that..The other problems I described I think are simple, I just
don't know how to fix them. I'm not really a C#-programmer and haven't
touched it for seven-eight months and it took quite a long time just to
get this far.

I can post the complete program if requested. Hope you understood my
explanation of how I want it to look. I can also post screenshots if needed.

Thanks for any replies!

- WP
 

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