gpt4 book ai didi

c# - 当代码在 Form1.Designer.cs 之外时,表格布局面板不会在屏幕上绘制(或工作)

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:35 25 4
gpt4 key购买 nike

我在 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);
}

最佳答案

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

this.Controls.Add(this.tableLayoutPanel);

关于c# - 当代码在 Form1.Designer.cs 之外时,表格布局面板不会在屏幕上绘制(或工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com