Click here to Skip to main content
15,071,262 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I am creating a TableLayout in which the rows and columns are set by the user at runtime.

There initial controls on the form are rowsTextBox, columnsTextBox, button1 and tableLayoutPanel1.

I modified some of the code from my initial program. Here's the old code.

C#
private void button1_Click(object sender, EventArgs e)
 {
  tableLayoutPanel1.RowCount = int.Parse(rowsTextBox.Text);
  tableLayoutPanel1.ColumnCount = int.Parse(columnsTextBox.Text);

  for (int col = 0; col < tableLayoutPanel1.ColumnCount; col++)
  {
  for (int rows = 0; rows < tableLayoutPanel1.RowCount; rows++)
  {
   Panel p = new Panel();
   TextBox tb = new TextBox();
   Button btn = new Button();
   p.Controls.Add(tb);
   p.Controls.Add(btn);
   btn.Location = new Point(0, tb.Top + 20);
   tableLayoutPanel1.Controls.Add(p, col, rows);
  }
  }


Here's the new code in the same button click event. Just some additions , the basic code remains the same but I dont know the number of rows and columns being formed are not correct. (try 5 x5 or 6. x6 etc. greater than 3x3 )

C#
output.Person.Clear();
        output.Row = int.Parse(rowsTextBox.Text);
        output.Column = int.Parse(columnsTextBox.Text);
        tableLayoutPanel1.Controls.Clear();
        tableLayoutPanel1.RowCount = int.Parse(rowsTextBox.Text);
        tableLayoutPanel1.ColumnCount = int.Parse(columnsTextBox.Text);
        tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
        for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++)
        {
          for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++)
          {
            Panel p = new Panel();
            PictureBox picb = new PictureBox();
            Label lb = new Label();
            p.Controls.Add(lb);
            p.Controls.Add(picb);
            //Set the label properties            
            lb.BackColor = Color.White;
            lb.Size = new Size(88, 15);
            lb.Location = new Point(panel1.Left + 1, panel1.Top + 1);
            //Set the picture box properties            
            picb.Location=new Point(0,lb.Top + 20);
            picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            picb.BackColor = Color.White;
            picb.Dock = DockStyle.Fill;                                              
            tableLayoutPanel1.Controls.Add(p, col, rows);
            p.Dock = DockStyle.Fill;            
            picb.MouseClick += pb_Click;            
            LayoutItem item = new LayoutItem()
            {
              //This assignment is new in Visual studio 2008
              ItemLabel = lb,
              ItemPcitureBox = picb,
              pnlcolor=p
            };
            m_items.Add(item);            
            Customer c = new Customer();
            c.Index = col * tableLayoutPanel1.RowCount + rows;     


Screenshot: http://img229.imageshack.us/img229/429/captureyv.png[^]

I tried many things but still lingering with the problem.
However, I noticed that the TableLayoutPanel on the Form inside a PlaceHolder i.e. Panel and set the TableLayoutPanel dockstyle to Fill so that the TableLayoutPanel covers the whole form. Panel is anchored to Top, Left, Bottom, Right. I think this panel has to do something with the problem.

The rows adjust themselves according to the size of the Form and not to the size of the Panel (which in this case is less than the size of the form).

In my case, it was essential to use the Panel since I had to Fill the TableLayout on the Form.
Also, I may be wrong since I tried to remove the Panel from the problematic program but the cells still weren't becoming equal.
Please HELP!!!
Posted
Updated 14-Aug-21 5:35am

Assuming this is WinForms ..

I've worked with the TableLayoutPanel for years, and, I find that configuring it at run-time requires a certain series of steps. In your code, you are not creating the necessary ColumnStyle and RowStyle objects that determine the run-time presentation/structure.

You need to make choices about whether Row heights and Column widths are set to exact sizes, or set automatically, or set to percentages of the TableLayoutPanel's height and width.

Hopefully this example will get you going:
public void InitializeTLayoutPanel(TableLayoutPanel tbllp, int rowcount, int colcount)
{
    tbllp.Controls.Clear();

    tbllp.ColumnStyles.Clear();
    tbllp.RowStyles.Clear();

    tbllp.ColumnCount = colcount;
    tbllp.RowCount = rowcount;

    float collpcent = colcount / 100.0f;
    float rowpcent = rowcount / 100.0f;

    float colabswidth = 100.0f;

    tbllp.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
    tbllp.AutoScroll = true;
    tbllp.AutoSizeMode = AutoSizeMode.GrowAndShrink;

    for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++)
    {
        tbllp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, collpcent));

        //tbllp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, colabswidth));

        for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++)
        {
            tbllp.RowStyles.Add(new ColumnStyle(SizeType.Percent, rowpcent));

            // here you can add your whatever ...
        }
    }
}
Note:

1) very unlikely anyone here will go to a sote that required creating an account to see a screenshot

2) in your code;

a) you create PictureBoxes and Labels, but, you don't keep a reference to them; you can know which PictureBox is clicked in the Click event, but, getting access to the Label ... like when you want to set its Text ...is going to require needless effort. Solution: keep references !

b) what's a LayoutItem ?
   
Comments
Richard MacCutchan 14-Aug-21 11:39am
   
A bit late Bill.
BillWoodruff 14-Aug-21 11:41am
   
Hi Richard, I am puzzled ... why: late ?
Richard MacCutchan 14-Aug-21 11:50am
   
The question was posted: Posted 12-Oct-10 18:27pm
BillWoodruff 14-Aug-21 12:06pm
   
Oh gosh ... where did that decade go :)

The question did appear as "new" in QA because Member 15323333 responded 20 hours ago.
Richard MacCutchan 14-Aug-21 12:15pm
   
We've all done it at least once.
CHill60 16-Aug-21 6:11am
   
Of course, now that Member 1532333's non-solution has gone, it makes you look even worse :laugh:
Like Richard ... I have also done this ... more than once!
BillWoodruff 16-Aug-21 9:47am
   
I'm really glad my mistake gives you pleasure, and that you feel no urge to up-vote an excellent answer.
CHill60 16-Aug-21 11:10am
   
That was very remiss of me and I have rectified my omission :-)
BillWoodruff 16-Aug-21 11:26am
   
:)
try to setting the panels' AutoSize property to 'true' and AutoSizeingMode to 'GrowAndShrink'. Hope this will help.
Regards,
Kemo
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900