首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在DatagridviewCheckboxCell中更改checkBox大小

如何在DatagridviewCheckboxCell中更改checkBox大小
EN

Stack Overflow用户
提问于 2016-03-23 14:32:22
回答 2查看 6.2K关注 0票数 3

我知道复选框大小可以像这样改变。

代码语言:javascript
运行
复制
checkBox1.Size = new Size(10, 10);

我想用DataGridViewCheckBoxColumn更改DataGridview中的复选框大小,并尝试继承DatagridviewCheckboxCell,但最终还是找到了同样的方法。

代码语言:javascript
运行
复制
class DGCBC : DataGridViewCheckBoxColumn
{
    public DGCBC()
    {
        this.CellTemplate = new DatagridviewCheckboxCustomCell();
    }

    class DatagridviewCheckboxCustomCell : DataGridViewCheckBoxCell
    {
        public int row_index { get; set; }
        /// <summary>   
        /// constructor   
        /// </summary>   
        /// 
        public DatagridviewCheckboxCustomCell()
        {
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState,
         object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
         DataGridViewPaintParts paintParts)
        {
            *//I tried many way in there,but it's not work*
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }

    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-03-23 16:10:47

要以计算机的当前样式绘制系统控件,您应该使用ControlPaint class的许多方便方法中的一个。

下面是一个在Panel上绘制三个Checkboxes的示例

代码语言:javascript
运行
复制
private void panel1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawCheckBox(e.Graphics, 11, 11, 22, 22, ButtonState.Checked);
    ControlPaint.DrawCheckBox(e.Graphics, 11, 44, 33, 33, ButtonState.Checked);
    ControlPaint.DrawCheckBox(e.Graphics, 11, 88, 44, 44, ButtonState.Checked);
}

当然,您需要在CellPainting事件中对其进行调整,以使用所需的大小和单元格的坐标!

下面是一个简单的示例,几乎所有的都用CheckBox填充单元格

代码语言:javascript
运行
复制
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 1 && e.RowIndex >= 0)
    {
        e.PaintBackground(e.CellBounds, true);
        ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1, e.CellBounds.Y + 1, 
            e.CellBounds.Width - 2, e.CellBounds.Height - 2,
            (bool) e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
        e.Handled = true;
    }

你会想要找到一个适合你需要的尺寸。

请注意,您可以组合一些ButtonState。因此,要实现扁平外观,这是DataGridView CheckBoxCells的默认外观,您可以编写ButtonState.Checked | ButtonState.Flat等:

代码语言:javascript
运行
复制
ControlPaint.DrawCheckBox(e.Graphics, 11, 11, 22, 22, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 44, 33, 33, ButtonState.Checked | ButtonState.Flat);
ControlPaint.DrawCheckBox(e.Graphics, 11, 88, 44, 44, ButtonState.Checked | ButtonState.Flat | ButtonState.Inactive);

票数 8
EN

Stack Overflow用户

发布于 2020-04-07 08:55:48

要添加到@TaW's answer上,此解决方案将在dataGridView1上绘制任何复选框列

代码语言:javascript
运行
复制
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        DataGridView dgv = (DataGridView)sender;
        if (dgv.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
        {
            e.PaintBackground(e.CellBounds, true);
            ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1, e.CellBounds.Y + 1, 
                e.CellBounds.Width - 2, e.CellBounds.Height - 2,
                (bool) e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
            e.Handled = true;
        }
    }
}

在VB.NET中,这是:

代码语言:javascript
运行
复制
Private Sub dataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dataGridView1.CellPainting
    If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
        Dim dgv As DataGridView = sender
        If TypeOf dgv.Columns(e.ColumnIndex) Is DataGridViewCheckBoxColumn Then
            e.PaintBackground(e.CellBounds, True)
            ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1,
                e.CellBounds.Y + 1, e.CellBounds.Width - 2, e.CellBounds.Height - 2,
                If(CBool(e.FormattedValue), ButtonState.Checked, ButtonState.Normal))
            e.Handled = True
        End If
    End If
End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36171250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档