Catalin Cernat Catalin Cernat - 1 year ago 160 Question

Custom checkbox - disabled state color

I have a custom checkbox witch allows resizing of the main rectangle. But when I want to set it as disabled, it doesnt gray out like normal checkbox control does. It just stays white.

Here is class for custom checkbox to allow resizing:

Public Class NewCheckBox
Inherits CheckBox

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

'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

Here is how i want to use it:

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

I've tried to set the forecolor property, but to no avail.

How can i make the custom control to be greyed out in disabled mode?

Answer Source

I don't see anything in your code that specifies how to paint based on whether the control is disabled or not, so of course it doesn't change. Inactive is one of the ButtonState values so you need to specify that when the control is disabled.

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download