2

我正在使用 C# Forms 应用程序。

我已经将一个表格布局面板拖放到窗口中,然后在Form1.Designer.cs自动生成的文件之外动态添加组件,代码在 Form1.cs 文件中。

在那里,我用几个 for 循环添加了所有应该填充布局表的内容,这个成功完成了。

现在的问题是我还想动态创建表格布局,所以我所做的只是从设计器文件中复制并粘贴自动生成的代码,并从 [design] 中删除 d&d 表格布局。

这就是我为其他组件所做的,它已经无缝地工作了,现在,由于某种原因,这不起作用。

预期的结果将是(不可见的布局面板)在其中显示所有图像框。

输出仅显示一个空窗口。

代码如下:

private void paintLayoutTableOnScreen()
    {
        this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
        this.SuspendLayout();
        // 
        // tableLayoutPanel
        // 
        this.tableLayoutPanel.ColumnCount = 6;
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
        this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
        this.tableLayoutPanel.Location = new System.Drawing.Point(12, 12);
        this.tableLayoutPanel.Name = "tableLayoutPanel";
        this.tableLayoutPanel.RowCount = 5;
        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
        this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
        this.tableLayoutPanel.Size = new System.Drawing.Size(992, 460);
        this.tableLayoutPanel.TabIndex = 0;
        this.tableLayoutPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.tableLayoutPanel_Paint);
    }

就在下面:

private System.Windows.Forms.TableLayoutPanel tableLayoutPanel;

所以,我确定问题就在这里,因为在我更改代码位置之前,它正在打印以筛选图像框。所以问题应该存在,但以防万一:

我打电话paintLayoutTableOnScreen()给以下方式:

public Form1()
    {
        InitializeComponent();
        paintLayoutTableOnScreen();
        paintTablesInScreen();//This was already there and worked just fine.
    }

paintTablesInScreen()执行以下操作:

private void paintTablesInScreen()
    {
        for (int i = 0; i < Info.NumberOfColumns; i++)
        {
            for (int j = 0; j < Info.NumberOfRows; j++)
            {
                drawTable(i, j);

            }
        }
    }

drawTable()

private void drawTable(int column, int row)
    {
        int tableNumber = Info.TableNumber(column, row);

        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.pictureBox1.Image = Image.FromFile("C:\\Users\\Trufa\\Documents\\Visual Studio 2010\\Projects\\Viernes 7\\table.png");
        this.pictureBox1.Location = new System.Drawing.Point(3, 3);
        this.pictureBox1.Name = "pictureBox" + tableNumber.ToString();
        this.pictureBox1.Tag = tableNumber.ToString();
        this.pictureBox1.Size = new System.Drawing.Size(128, 86);
        this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Click += new EventHandler(AnyPictureBoxClickHandler);

        this.pictureBox1.Paint+=new PaintEventHandler((sender, e) =>
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            e.Graphics.DrawString(tableNumber.ToString(), Font, Brushes.Black, 58, 20);
        });


        this.tableLayoutPanel.Controls.Add(this.pictureBox1, column, row);
    }
4

1 回答 1

2

您没有将 TableLayoutPanel 添加到表单的控件中。即你想念:

this.Controls.Add(this.tableLayoutPanel);
于 2012-05-30T06:05:08.063 回答