Here is the actual code (from .Net Reference Source) that paints the checkboxes, in the DrawItem
event:
Rectangle bounds = e.Bounds;
int border = 1;
int height = Font.Height + 2 * border;
// set up the appearance of the checkbox
// [Snip]
// If we are drawing themed CheckBox .. get the size from renderer..
// the Renderer might return a different size in different DPI modes..
if (Application.RenderWithVisualStyles) {
VisualStyles.CheckBoxState cbState = CheckBoxRenderer.ConvertFromButtonState(state, false, ((e.State & DrawItemState.HotLight) == DrawItemState.HotLight));
idealCheckSize = (int)(CheckBoxRenderer.GetGlyphSize(e.Graphics, cbState)).Width;
}
// Determine bounds for the checkbox
//
int centeringFactor = Math.Max((height - idealCheckSize) / 2, 0);
// Keep the checkbox within the item's upper and lower bounds
if (centeringFactor + idealCheckSize > bounds.Height) {
centeringFactor = bounds.Height - idealCheckSize;
}
Rectangle box = new Rectangle(bounds.X + border,
bounds.Y + centeringFactor,
idealCheckSize,
idealCheckSize);
if (RightToLeft == RightToLeft.Yes) {
// For a RightToLeft checked list box, we want the checkbox
// to be drawn at the right.
// So we override the X position.
box.X = bounds.X + bounds.Width - idealCheckSize - border;
}
// Draw the checkbox.
//
if (Application.RenderWithVisualStyles) {
VisualStyles.CheckBoxState cbState = CheckBoxRenderer.ConvertFromButtonState(state, false, ((e.State & DrawItemState.HotLight) == DrawItemState.HotLight));
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(box.X, box.Y), cbState);
}
else {
ControlPaint.DrawCheckBox(e.Graphics, box, state);
}
EDIT: Here is CheckBoxRenderer.ConvertFromButtonState
:
internal static CheckBoxState ConvertFromButtonState(ButtonState state, bool isMixed, bool isHot) {
if (isMixed) {
if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
return CheckBoxState.MixedPressed;
}
else if ((state & ButtonState.Inactive) == ButtonState.Inactive) {
return CheckBoxState.MixedDisabled;
}
else if (isHot) {
return CheckBoxState.MixedHot;
}
return CheckBoxState.MixedNormal;
}
else if ((state & ButtonState.Checked) == ButtonState.Checked) {
if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
return CheckBoxState.CheckedPressed;
}
else if ((state & ButtonState.Inactive) == ButtonState.Inactive) {
return CheckBoxState.CheckedDisabled;
}
else if (isHot) {
return CheckBoxState.CheckedHot;
}
return CheckBoxState.CheckedNormal;
}
else { //unchecked
if ((state & ButtonState.Pushed) == ButtonState.Pushed) {
return CheckBoxState.UncheckedPressed;
}
else if ((state & ButtonState.Inactive) == ButtonState.Inactive) {
return CheckBoxState.UncheckedDisabled;
}
else if (isHot) {
return CheckBoxState.UncheckedHot;
}
return CheckBoxState.UncheckedNormal;
}
}
CheckedListBox
control, I think you're better off using aListView
control with theCheckBoxes
property on, and you'll get the behavior you want. Then, just use the control's styling properties to make it look like a simpleCheckedListBox
. – Dogtired