自定义复选框 - 禁用状态颜色

时间:2016-10-19 08:28:07

标签: vb.net checkbox controls

我有一个自定义复选框,可以调整主矩形的大小。但是,当我想将其设置为禁用时,它不像正常的复选框控制那样灰显。它只是保持白色。

以下是允许调整大小的自定义复选框:

Public Class NewCheckBox
Inherits CheckBox

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e)

    'Make the box you check 3/4 the height
    Dim boxsize As Integer = Me.Height * 0.75
    Dim rect As New Rectangle(
        New Point(0, Me.Height / 2 - boxsize / 2),
        New Size(boxsize, boxsize)
    )
    ControlPaint.DrawCheckBox(e.Graphics, rect, If(Me.Checked, ButtonState.Checked, ButtonState.Normal))
End Sub 
End Class

以下是我想要使用它的方法:

Private Sub txtFpo_TextChanged(sender As Object, e As EventArgs) Handles txtFpo.TextChanged

    If functii.verificaLaser(txtFpo.Text) = True Then
        NewCheckBox1.Enabled = False
        'NewCheckBox1.ForeColor = Color.DarkGray
    End If

我试图设置forecolor属性,但无济于事。

如何在禁用模式下使自定义控件变为灰色?

1 个答案:

答案 0 :(得分:1)

我的代码中没有看到任何内容根据控件是否被禁用来指定如何绘制,所以当然它不会改变。 InactiveButtonState值之一,因此您需要在禁用控件时指定。

Dim buttonState = ButtonState.Normal

If Me.Checked Then
    buttonState = buttonState Or ButtonState.Checked
End If

If Not Me.Enabled Then
    buttonState = buttonState Or ButtonState.Inactive
End If

ControlPaint.DrawCheckBox(e.Graphics, rect, buttonState)