【问题标题】:How to (create and) add components to a Table Layout?如何(创建和)将组件添加到表格布局?
【发布时间】:2016-08-09 06:39:43
【问题描述】:

我正在C# 中创建一个国际象棋游戏。来自Java-Swing 环境,我创建了一个标准函数,它创建一个8x8 字段并为其赋予基本属性。

     Board = new Label[8, 8];
            for (int i = 0; i < 8; i++)
            {

                for (int j = 0; j < 8; j++)
                {
                    Board[i, j] = new System.Windows.Forms.Label();
                    Board[i, j].Location = new Point(i * 50, j * 50);
                    Board[i, j].Size = new System.Drawing.Size(50, 50);
                    Board[i, j].Visible = true;


                    if ((i + j) % 2 == 0) // Color decision
                    {
                        Board[i, j].BackColor = Color.Black;
                    }

                    else {
                        Board[i, j].BackColor = Color.White;
                    }

                    this.Controls.Add(Board[i, j]);
                }
            }

现在有两个额外的数组来保存棋盘外边缘的 abc 和 123(这样您就可以输入像 - “Knight to E3” 这样的移动)。

我已设法将所有组件添加到屏幕上,但它们目前相互重叠。我正在考虑创建一个 9x9“grid-layout”并向其中添加所有组件。

来自Java 我习惯了简单的命令,比如:

    GridLayout gl = new Gridlayout(3,3);
    this.setLayout(gl);

然后所有添加的元素都会自动放入网格中。 经过数小时的研究,我在C# 中找不到类似的东西。玩TableLayout 只会导致比解决方案更多的问题。

我的问题是如何实现 (grid) layout 并将我的所有标签添加到其中? 对于没有发布我的任何布局代码,我提前道歉,但就像我说的那样,这只是一团糟,没有做任何应该做的事情。

谢谢你:)

【问题讨论】:

    标签: c# winforms layout tablelayout


    【解决方案1】:

    我猜在 Win Forms 中它的工作方式有点不同。 你需要创建一个TableLayoutPanel,然后访问TableLayoutPanel.Controls,通过调用Control.ControlCollection.Add方法一个一个添加新的控件。

    var panel = new TableLayoutPanel();
    panel.ColumnCount = 3;
    panel.RowCount = 4;
    
    for(int i = 0; i< panel.ColumnCount; ++i)
        panel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
    
    for (int i = 0; i < panel.RowCount; ++i)
        panel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
    
    panel.Dock = DockStyle.Fill;
    this.Controls.Add(panel);
    
    for (int c = 0; c < 3; ++c)
    {
        for (int r = 0; r < 4; ++r)
        {
            var btn = new Button();
            btn.Text = (c+r).ToString();
            btn.Dock = DockStyle.Fill;
            panel.Controls.Add(btn, c, r);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我喜欢创建自己的类,该类继承如下代码所示的表单控件。您可以添加自己的属性,如行和列,或使用棋盘添加图像,即棋子的图片。请参阅下面的代码。您可以将下面的代码添加到布局面板,而不是像我一样添加到表单中。您可以在板上创建一个继承矩形的空间,然后将图像添加到您的自定义矩形中,该矩形将替换下面的按钮类。

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
      
              public Form1()
              {
                  InitializeComponent();
      
                  MyButton myButton = new MyButton(this);
              }
      
          }
          public class MyButton : Button
          {
              public static List<List<MyButton>> board { get; set; }
              public static List<MyButton> buttons { get; set; }
              const int WIDTH = 10;
              const int HEIGTH = 10;
              const int SPACE = 5;
              const int ROWS = 10;
              const int COLS = 20;
              public int row { get; set; }
              public int col { get; set; }
      
              public MyButton()
              {
              }
              public MyButton(Form1 form1)
              {
                  board = new List<List<MyButton>>();
                  buttons = new List<MyButton>();
      
                  for (int _row = 0; _row < ROWS; _row++)
                  {
                      List<MyButton> newRow = new List<MyButton>();
                      board.Add(newRow);
                      for (int _col = 0; _col < COLS; _col++)
                      {
                          MyButton newButton = new MyButton();
                          newButton.row = _row;
                          newButton.col = _col;
                          newButton.Width = WIDTH;
                          newButton.Height = HEIGTH;
                          newButton.Top = _row * (HEIGTH + SPACE);
                          newButton.Left = _col * (WIDTH + SPACE);
                          form1.Controls.Add(newButton);
                          newRow.Add(newButton);
                          buttons.Add(newButton);
                      }
                  }
              }
      
          }
      }
      

      【讨论】:

      • 非常感谢您,先生,使用了与您类似的系统,并且运行良好 =)
      猜你喜欢
      • 2021-08-02
      • 2012-06-03
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多