表格布局出了点问题 [英] Something wrong with Table Layout

查看:65
本文介绍了表格布局出了点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Form上有一个TableLayout,其中用户在运行时指定行数和列数.当指定3行或列时,行和列的创建都很好.但是,如果将行数和列数指定为5或以上,则会出现问题,并且不会创建所需的行数和列数.

这是显示问题的小型应用程序.
Skydrive上的示例 [已删除超链接]

如果可以在此处找到问题,则为按钮单击事件代码.

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.

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;
            }
        }
    }
}

推荐答案


如rajivlipu所写.您必须更改嵌套循环的顺序.在第一个循环中,您必须遍历行.

首先循环在每一步中获得一行,然后您可以循环遍历每一行的列.

rogards
罗伯特
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


只需替换代码
just replace the code
for (int col = 0; col <= tableLayoutPanel1.ColumnCount - 1; col++)
               {
                   for (int rows = 0; rows <= tableLayoutPanel1.RowCount - 1; rows++)
                   {










to


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


{


{


这篇关于表格布局出了点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆