简体   繁体   中英

How to create table with only check boxes?

I am looking for a Windows Forms Control that looks like a grid of a check boxes in C#. Something like this I guess, hope it makes sense.

在此输入图像描述

So how would I go about making this happen, is it even possible?

你可以使用TableLayoutPanel ,这个链接可以帮助你了解它的一些属性,这个链接是使用它的例子

Strongly avoid a TableLayoutPanel it is much too expensive with this many check boxes.

DataGridView is the appropriate choice, change the column types to DataGridViewCheckBoxColumn. Editing only requires a single real checkbox control, taken care of automatically. No problems with focus either.

Making your own is a very reasonable approach as well, you can make it look any way you want. Derive a class from Control, Panel if you need it to be scrollable. ControlPaint.DrawCheckBox() can be useful.

Use a TableLayoutPanel to render the check boxes.

This sample creates a TableLayoutPanel by code:

TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();

int rowCount = 2;
int columnCount = 2;

for (int row = 0; row < rowCount; row++)
{
    tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));

    for (int column = 0; column < columnCount; column++)
    {
        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

        CheckBox checkBox = new CheckBox();

        tableLayoutPanel.Controls.Add(checkBox, column, row);
    }
}

this.Controls.Add(tableLayoutPanel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM