【问题标题】:How to change checkBox Size in DatagridviewCheckboxCell如何更改 DatagridviewCheckboxCell 中的复选框大小
【发布时间】:2020-06-03 17:04:28
【问题描述】:

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

checkBox1.Size = new Size(10, 10);

我想用 DataGridViewCheckBoxColumn 更改 DataGridview 中的复选框大小,我试图继承 DatagridviewCheckboxCell,但找到了任何方法来做同样的事情。

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

    }
}

【问题讨论】:

标签: c# .net


【解决方案1】:

要以您机器的当前样式绘制系统控件,您应该使用ControlPaint class 的众多便捷方法之一。

这是一个将三个Checkboxes 绘制到Panel 上的示例:

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 填充单元格:

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 等:

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

【讨论】:

  • 感谢您的帮助,成功了!
  • 如果您对答案感到满意,请考虑accepting it..! - 我看到你从来没有这样做过:去左上角的(不可见的)复选标记,在答案的投票下方,然后单击它!它变成绿色并为我们俩赢得了一点声誉。..
【解决方案2】:

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

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 中是:

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

【讨论】:

  • 真的很有帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-05-09
  • 2016-08-07
  • 1970-01-01
  • 2017-05-12
  • 2011-09-09
  • 2012-04-24
  • 2012-04-26
  • 2012-02-05
相关资源
最近更新 更多