我不會將Code鉅細靡遺通通放上來,基本操作是programer的基本功,應該自己想辦法弄懂。
1.依指定的XY數量產生表格:先放一個TableLayoutPanel元件,建立一個Function:
//定益表格參數
int y = 3;
int x = 2;
tableLayoutPanel1.Visible = false;
tableLayoutPanel1.BackColor = Color.FromArgb(192, 255, 192);
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.Refresh();
tableLayoutPanel1.RowCount = y;
tableLayoutPanel1.ColumnCount = x;
for (int co = 0; co < x; co++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / x);
tableLayoutPanel1.ColumnStyles.Add(cs);
}
for (int ro = 0; ro < y; ro++)
{
RowStyle rs = new RowStyle(SizeType.Percent, 100 / y);
tableLayoutPanel1.RowStyles.Add(rs);
}
2.以下範例,可在產生表格後、分別產生第一列及第一欄的Label
//產生第一欄Label
for (int co = 0; co < x; co++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / x);
tableLayoutPanel4.ColumnStyles.Add(cs);
if (co == 0)
{
for (int r = 0; r < y; r++)
{
if (r != 0)
{
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.BackColor = Color.FromArgb(255, 255, 192);//背景顏色
LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.ForeColor = Color.Red;//字體紅色
if(x >=16)
LblName.Font = new Font("標楷體", 7);
LblName.Name = "R" + Convert.ToInt16(r);
LblName.Text = "R" + Convert.ToInt16(r);
tableLayoutPanel1.Controls.Add(LblName, 0, r);
}
}
}
}
//產生第一列Label
for (int ro = 0; ro < y; ro++)
{
RowStyle rs = new RowStyle(SizeType.Percent, 100 / y);
tableLayoutPanel4.RowStyles.Add(rs);
if (ro == 0)
{
for (int c = 0; c < x;c++)
{
if (c != 0)
{
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.BackColor = Color.FromArgb(255, 255, 192);//背景顏色
//LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.ForeColor = Color.Red;//字體紅色
if (x >= 17)
LblName.Font = new Font("標楷體", 8);
if (x >= 20)
LblName.Font = new Font("標楷體", 7);
LblName.Name = "C" + Convert.ToInt16(c);
LblName.Text = "C" + Convert.ToInt16(c);
tableLayoutPanel1.Controls.Add(LblName, c, 0);
}
}
}
}
3.點選TableLayoutPanel時,動態產生Label:從TableLayoutPanel的MouseClick事件
int row = 0;
int verticalOffset = 0;
tableLayoutPanel1.Visible = false;//產生Label的過程,畫面會跑來跑去,所以先隱藏掉
foreach (int h in tableLayoutPanel1.GetRowHeights())
{
int column = 0;
int horizontalOffset = 0;
foreach (int w in tableLayoutPanel1.GetColumnWidths())
{
Rectangle rectangle = new Rectangle(horizontalOffset, verticalOffset, w, h);
if (rectangle.Contains(e.Location))
{
//產生Label
Label LblName;
LblName = new Label();
LblName.Dock = DockStyle.Fill;//填滿表格
LblName.ForeColor = Color.Red;//字體紅色
LblName.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;//文字置中
LblName.Font = new Font(LblName.Font, FontStyle.Bold);//粗體字
LblName.Font = new Font("標楷體", 10);
LblName.Name = "A1";
LblName.Text = "A1";
tableLayoutPanel1.Controls.Add(LblName, column, row);
LblName.Click += new System.EventHandler(LblName_Click);//動態事件
tableLayoutPanel4.Visible = true;
return;
}
horizontalOffset += w;
column++;
}
verticalOffset += h;
row++;
}
4.宣告點擊動態產生的Label
private void LblName_Click(object sender, System.EventArgs e)
{
dosomething;
}
沒有留言:
張貼留言