简体   繁体   English

如何创建只有复选框的表?

[英]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#. 我正在寻找一个Windows窗体控件,看起来像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. 极力避开一个TableLayoutPanel则显得过于昂贵与这么多的复选框。

DataGridView is the appropriate choice, change the column types to DataGridViewCheckBoxColumn. DataGridView是合适的选择,将列类型更改为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. 如果需要可滚动,请从Control,Panel派生一个类。 ControlPaint.DrawCheckBox() can be useful. ControlPaint.DrawCheckBox()可能很有用。

Use a TableLayoutPanel to render the check boxes. 使用TableLayoutPanel呈现复选框。

This sample creates a TableLayoutPanel by code: 此示例通过代码创建TableLayoutPanel

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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