我正在处理C#窗体应用程序。
我已经将表格布局面板拖放到窗口中,然后继续动态添加组件 在自动生成Form1.Designer.cs
文件之外,代码位于 Form1.cs 文件中。
在那里,我用几个 for 循环添加了所有应该填充布局表的内容,这个成功完成了。
现在的问题是我还想动态创建表格布局,所以我所做的只是将自动生成的代码从设计器文件中复制并粘贴出来,并从[设计]中删除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);
}
您不会将 TableLayoutPanel 添加到窗体的控件中。 即您错过了:
this.Controls.Add(this.tableLayoutPanel);