I'm working in a C# Forms application.
I had dragged and dropped a table layout panel, into the windows, and the proceeded to add components dynamically Outside de Form1.Designer.cs
auto generated file, the code is in the Form1.cs file.
There I add with a couple of for loops all the content that should fill the layout table, this one done successfully.
The problem now is that I want to also want to dynamically create the table layout, so what I did was just copying and pasting the auto-generated code out of the designer file and deleted the d&d table layout from the [design].
This is what I did for the other components and it has worked seamlessly, and now, for some reason, this doesn't what to work.
The expected result would be the (invisible layout panel) displaying all the imageboxes inside it.
The output, simply shows an empty window.
The code is as follows:
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);
}
And just below:
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel;
So, I'm certain that the problem is here since it was printing to screen the imageboxes before I changed the place of the code. So the problem should be there, but just in case:
I am calling paintLayoutTableOnScreen()
the following way:
public Form1()
{
InitializeComponent();
paintLayoutTableOnScreen();
paintTablesInScreen();//This was already there and worked just fine.
}
paintTablesInScreen()
does the following:
private void paintTablesInScreen()
{
for (int i = 0; i < Info.NumberOfColumns; i++)
{
for (int j = 0; j < Info.NumberOfRows; j++)
{
drawTable(i, j);
}
}
}
and 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);
}
You are not adding your TableLayoutPanel to the Controls of the form. I.e. you miss:
this.Controls.Add(this.tableLayoutPanel);