我正在使用C#Forms应用程序。
我将一个表格布局面板拖放到窗口中,然后继续动态添加组件在de 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);
}
答案 0 :(得分:2)
您没有将TableLayoutPanel添加到表单的控件中。即你错过了:
this.Controls.Add(this.tableLayoutPanel);