473,511 Members | 15,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TableLayoutPanel problem

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
Oct 27 '08 #1
0 4514

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11656
by: Sudha Pune | last post by:
Hi all i am using vs2005 C# window application i am trying to add the controls dynamically inside the "tablelayoutpanel", i need to add row by row and each row i will have to have 10 columns...
2
17533
by: steve | last post by:
Hi All I need to change the rowspan, columnspan properties of a control when they are within a TableLayoutPanel (VB.net 2005) At runtime the property is not available, but is at design time ...
3
3060
by: swartzbill2000 | last post by:
Hello, I have an array of Buttons down a column of a TableLayoutPanel. They all share a common click handler. In the handler, how can I determine the row of the TableLayoutPanel for the Button...
2
30748
by: avanti | last post by:
Hi, I am trying to delete rows from a TableLayoutPanel at Runtime. I tried editing the RowStyles collection but doesn't do what I want it to. I have a table with 10 rows. I want to delete rows 2...
0
1613
by: CodeChamp | last post by:
Hi, I am having a problem in my WinForm control which is a control derived from "ComboBox". Control adds a Text "Label" control in the parent control's collection when the control handle is...
0
844
by: Code Monkey | last post by:
I've got Windows Form which has a TableLayoutPanel on it. I'm dynamically adding Labels to the cells, binding their values from a DataTable. What I want to do, is if a certain value of the...
0
4166
by: ponyryd | last post by:
It appears the default behavior for the final row/column is to autofill the "avaialble space" I set the number of rows and columns and then dynamically add my controls to each "cell". The...
0
1781
by: strattonn | last post by:
I have a TableLayoutPanel on a form and I would like to freeze the first row and column so they do not scroll off the screen. Just like Excel's Fixed Row/Column feature. I have a feeling the...
4
3619
by: Jeff | last post by:
Hello, I am trying to drag and drop a label control from one cell in a tablelayoutpanel to another (VB2005). There is no problem if both cells are visible, but i cannot get the tablelayoutpanel...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.