如何使用Array生成Checkboxes网格?

时间:2017-06-11 13:28:15

标签: c# arrays checkbox datagridview

有人知道如何使用2D数组以编程方式生成复选框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);
    }
}

3 个答案:

答案 0 :(得分:0)

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

var app = angular.module('myApp',[]);
app.controller("StatController",function($scope,$http,$window, $filter,$RootScope)
 {
    $RootScope.FindStats = function() {
     $scope.Stat = {
         "idStat": 21,
         "nbrBoks": 7,
         "nbSection": 5,
         "total": 135,
         "resCon": 0.0518519,
         "resNotCon": 0.037037
     };

      Highcharts.chart('container', {
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: 'Browser market shares January, 2015 to May, 2015'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.2f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: 'Brands',
            colorByPoint: true,
            data: [{
                name: 'Result of books',
                y: Stat.resNotCon, 
                color: '#00c853',
            },{
                name: 'Result of section',
                y:Stat.resCon, 
                color: '#b71c1c',
            }]
        }]
    });
 }
 $scope.FindStats();

 });

答案 1 :(得分:0)

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

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;

答案 2 :(得分:0)

我刚发现答案问题是没有自动调整大小的自动调整大小=真的某些复选框将不可见

     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]);
            }
        }