【问题标题】:How to generate a Checkboxes grid with Array?如何使用数组生成复选框网格?
【发布时间】:2017-11-13 00:58:30
【问题描述】:

有人知道如何使用二维数组以编程方式生成复选框like this 的网格吗?

for (int x = 0; x < numberOfRows; x++)
{
    for (int y = 0; y < numberOfColumns; y++)
    {
        int index = x * numberOfColumns + y;
        var checkbox = new CheckBox();

        checkbox.Location = new Point(20 * x, 20*y);
        this.Controls.Add(checkbox);
    }
}

【问题讨论】:

  • CheckBox[,] c = new CheckBox[10,10]; for (int i = 0; i &lt; c.GetLength(0); i++) { for (int j = 1; j &lt; c.GetLength(1); j++) { c[i,j] = new CheckBox(); c[i, j].Location = new Point(5, i * 20); this.Controls.Add(c[i, j]); } }
  • 你在使用 WPF 吗?
  • 不只是 Windows 窗体
  • 您是否考虑过DataGridView 充满了复选框列?

标签: c# arrays checkbox datagridview


【解决方案1】:

试一试。您需要初始化一个字典,如顶部所示,以允许 GetCheckBoxAtPosition 函数工作。

    private Dictionary<Point, CheckBox> checkBoxes;

    private Dictionary<Point,CheckBox> GenerateCheckBoxes(Form form, int width, int height, int padding) {

        Dictionary<Point, CheckBox> checkboxes = new Dictionary<Point, CheckBox>();

        for(int x = 0; x < width; x++) {
            for(int y = 0; y < height; y++) {
                CheckBox checkBox = new CheckBox();
                Point location = new Point(x*(15+padding), y*(15+padding));

                //Formatting
                checkBox.Location = location;
                checkBox.Text = string.Empty;
                checkBox.Size = new Size(15,15);

                //Custom behaviour
                checkBox.Click += CheckBox_Click;

                form.Controls.Add(checkBox);
                checkboxes.Add(new Point(x, y), checkBox);                                 
            }
        }

        return checkboxes;
    }

    private void CheckBox_Click(object sender, EventArgs e)
    {
        CheckBox clicked = (CheckBox)sender;
    }

    private CheckBox GetCheckBoxAtPosition(int x, int y) {
        return checkBoxes[new Point(x, y)];
    }

【讨论】:

    【解决方案2】:

    您可以使用列表列表,如下所示:

    private List<List<CheckBox>> CheckBoxes = new List<List<CheckBox>>();
    
    private void button1_Click(object sender, EventArgs e)
    {
        int numberOfRows = 5;
        int numberOfColumns = 10;
    
        CheckBoxes.Clear();
        for (int y = 0; y < numberOfRows; y++)
        {
            List<CheckBox> row = new List<CheckBox>();
            CheckBoxes.Add(row);
            for (int x = 0; x < numberOfColumns; x++)
            {
                var checkbox = new CheckBox();
                checkbox.Text = "";
                checkbox.AutoSize = true;
                checkbox.Location = new Point(20 * x, 20 * y);
                row.Add(checkbox);
                this.Controls.Add(checkbox);
            }
        }
    }
    

    然后你可以访问一个特定的:

    CheckBoxes[0][3].Checked = true;
    

    【讨论】:

      【解决方案3】:

      我刚刚发现问题的答案是 Autosize without Autosize=true 一些复选框将不可见

           CheckBox[,] c = new CheckBox[2, 2];
      
              for (int i = 0; i < c.GetLength(0); i++)
              {
                  for (int j = 0; j < c.GetLength(1); j++)
                  {
                      c[i, j] = new CheckBox();
                      c[i, j].Location = new Point(i*20, j* 20);
      
                         c[i, j].AutoSize = true;
      
                      //c[i, j].Height = 10;
                      //c[i, j].Width = 10;
                      this.Controls.Add(c[i, j]);
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2014-01-15
        • 1970-01-01
        • 1970-01-01
        • 2014-11-29
        • 2021-09-30
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 2022-11-18
        相关资源
        最近更新 更多