【问题标题】:winform change checkbox's color of the actual box [duplicate]winform更改实际框的复选框颜色[重复]
【发布时间】:2021-09-03 04:25:38
【问题描述】:

是否有任何方法可以更改包含 winform 复选框中复选标记的框的颜色?像这样。

【问题讨论】:

标签: c# winforms


【解决方案1】:

没有直接的方法。

如果您必须使用 winforms,最简单的方法是模仿按钮并使用图像。

为此,请将您的 CheckBoxes 属性编辑为以下内容。

  • 外观:Normal -> Button
  • 平面样式:Standard -> Flat
  • 平面外观,边框大小:1 -> 0
  • FlatAppearance,所有颜色:父容器的背景颜色,通常为Control
  • TextImageRelation: Overlay -> ImageBeforeText
  • 图片:加载未选中框的图片

然后在每个 CheckBox 的事件处理程序 CheckedChanged 中,我们将图像从未选中的框更改为已选中的框,反之亦然,如下所示:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox currentCheckBox = (sender as CheckBox);
    if (currentCheckBox.Checked)
    {
        currentCheckBox.Image = Properties.Resources._checked;
    }
    else
    {
        currentCheckBox.Image = Properties.Resources._unchecked;
    }
}

或者,更短:

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    (sender as CheckBox).Image = (sender as CheckBox).Checked ? Properties.Resources._checked : Properties.Resources._unchecked;
}

结果应该是这样的: Result

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多