Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a TableLayout on the Form in which the user specifies the number of rows and columns at runtime. When 3 rows or columns are specified, the rows and columns are created fine. However, if the number of rows and columns are specified as 5 or above, something goes wrong and the required number fo rows and columns are not created.

Here's the small app to show the problem.
Sample on Skydrive [removed the hyperlink]

Here's the button click event code, if the problem can be identified form here.

C#
private void button1_Click(object sender, EventArgs e)//To Create TableLayout on button1_Click
{
    if (rowsTextBox.Text=="" && columnsTextBox.Text == "")
    {
        MessageBox.Show("Plese specify the rows and columns");
    }
    else
    {
        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();
                Label lb = new Label();
                PictureBox picb = new PictureBox();
                //Set the picture box properties
                picb.Size = new Size(150, 150);
                picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                picb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                //Set the label properties
                lb.BackColor = Color.Transparent;
                p.Controls.Add(lb);
                p.Controls.Add(picb);
                picb.Location = new Point(0, lb.Top + 20);
                tableLayoutPanel1.Controls.Add(p, col, rows);

                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;
            }
        }
    }
}
Posted
Updated 11-Oct-10 1:24am
v3
Comments
OriginalGriff 9-Oct-10 5:54am    
Nobody (with an IQ bigger that their shoe size) is about to download unknown zip files to look at your problem.
Edit your question, and add the relevant code (using the "code block" widget to preserve the formatting) so we can read it here.

Hi,
as rajivlipu wrote. you must change order of nested loop. in first loop you must loop through rows.

first loop gets one row in every step and then you can loop through columns for every row.

rogards
Robert
 
Share this answer
 
Comments
optimus_prime1 11-Oct-10 9:12am    
As I answered to Rajiv Lipu's post, the problem remains the same.
just replace the code
C#
for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++)
               {
                   for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++)
                   {



to


C#
for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++)
    {
        for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++)

{
 
Share this answer
 
Comments
optimus_prime1 10-Oct-10 8:39am    
Tried, but it remains the same. :-|

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