WinForm——TableLayoutPanel实例

╰+攻爆jí腚メ 2022-11-24 14:06 345阅读 0赞

实例1、用TableLayoutPanel 制作表格

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hoaGhoaGhoaGh3d3d3d3d3d3d3_size_16_color_FFFFFF_t_70

声明TableLayoutPanel对象

///

  1. /// TableLayoutPanel
  2. /// </summary>

TableLayoutPanel table = new TableLayoutPanel();

添加控件和事件

private void Form2_Load(object sender, EventArgs e)

  1. \{
  2. // 默认添加一行数据
  3. table.Dock = DockStyle.Top; //顶部填充
  4. panel1.Controls.Add(table);
  5. table.ColumnCount = 5; //5列
  6. table.Height = table.RowCount \* 40; //table的整体高度,每行40
  7. table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width \* 0.2f)); //利用百分比计算,0.2f表示占用本行长度的20%
  8. table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width \* 0.2f));
  9. table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width \* 0.2f));
  10. table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width \* 0.2f));
  11. table.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, table.Width \* 0.2f));
  12. for (int i = 1; i <= 10; i++)
  13. \{
  14. AddRow("键盘侠"\+i.ToString().PadLeft(2,'0'),"蜘蛛侠" \+ i.ToString().PadLeft(2, '0'), "钢铁侠" \+ i.ToString().PadLeft(2, '0'), "猪猪侠" \+ i.ToString().PadLeft(2, '0'), "死猪佩奇" \+ i.ToString().PadLeft(2, '0'));
  15. \}
  16. \}
  17. private void AddRow(string apple, string orange, string banana, string casaba, string sugarcane)
  18. \{
  19. try
  20. \{
  21. // 动态添加一行
  22. table.RowCount++;
  23. //设置高度,边框线也算高度,所以将40修改大一点
  24. table.Height = table.RowCount \* 44;
  25. // 行高
  26. table.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40));
  27. // 设置cell样式,增加线条
  28. table.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetPartial;
  29. int i = table.RowCount - 1;
  30. Label label1 = new Label();
  31. label1.Text = apple;
  32. label1.Dock = DockStyle.Fill;
  33. label1.BackColor = Color.Red;
  34. label1.Click += Label1\_Click;
  35. label1.Font = new Font("楷体", 13, FontStyle.Regular);
  36. label1.TextAlign = ContentAlignment.MiddleCenter;
  37. table.Controls.Add(label1, 0, i);
  38. Label label2= new Label();
  39. label2.Text = orange;
  40. label2.Width = 200;
  41. label2.Height = 40;
  42. label2.Click += Label1\_Click;
  43. label2.Font = new Font("楷体", 13, FontStyle.Regular);
  44. label2.TextAlign = ContentAlignment.MiddleCenter;
  45. table.Controls.Add(label2, 1, i);
  46. Label label3 = new Label();
  47. label3.Text = banana;
  48. label3.Width = 200;
  49. label3.Height = 40;
  50. label3.Click += Label1\_Click;
  51. label3.Font = new Font("楷体", 13, FontStyle.Regular);
  52. label3.TextAlign = ContentAlignment.MiddleCenter;
  53. table.Controls.Add(label3, 2, i);
  54. Label label4 = new Label();
  55. label4.Text = casaba;
  56. label4.Width = 200;
  57. label4.Height = 40;
  58. label4.Click += Label1\_Click;
  59. label4.Font = new Font("楷体", 13, FontStyle.Regular);
  60. label4.TextAlign = ContentAlignment.MiddleCenter;
  61. table.Controls.Add(label4, 3, i);
  62. Label label5 = new Label();
  63. label5.Text = sugarcane;
  64. label5.Width = 200;
  65. label5.Height = 40;
  66. label5.Click += Label1\_Click;
  67. label5.Font = new Font("楷体", 13, FontStyle.Regular);
  68. label5.TextAlign = ContentAlignment.MiddleCenter;
  69. table.Controls.Add(label5, 4, i);
  70. \}
  71. catch (Exception ex)
  72. \{
  73. MessageBox.Show(ex.Message.PadRight(30, ' '), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. \}
  75. \}
  76. private void Label1\_Click(object sender, EventArgs e)
  77. \{
  78. Label label = (Label)sender;
  79. MessageBox.Show(label.Text);
  80. \}

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hoaGhoaGhoaGh3d3d3d3d3d3d3_size_16_color_FFFFFF_t_70 1

实例2、实现表格的跨列

在TableLayoutPanel中加入panel对象,设置panel对象的ColumnSpan属性为2,将Marign设置为0,Dock设置为Fill。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hoaGhoaGhoaGh3d3d3d3d3d3d3_size_16_color_FFFFFF_t_70 2

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hoaGhoaGhoaGh3d3d3d3d3d3d3_size_16_color_FFFFFF_t_70 3

发表评论

表情:
评论列表 (有 0 条评论,345人围观)

还没有评论,来说两句吧...

相关阅读

    相关 实例

    题目要求:设计一个目录扫描程序,输入目录名,扫描该目录下的文件,依次输出扫描的文件名 include <unistd.h> include <stdio.h>