1 Attachment(s)
CheckedListBox with RadioButton-style Behaviour
C# version here.
vb.net Code:
Imports System.ComponentModel
''' <summary>
''' Provides RadioButton-style functionality in a CheckedListBox.
''' </summary>
<ToolboxBitmap(GetType(CheckedListBox))> _
Public Class RadioCheckedListBox
Inherits CheckedListBox
''' <summary>
''' Indicates whether the check boxes behave like radio buttons.
''' </summary>
Private _radioCheck As Boolean = True
''' <summary>
''' Gets or sets a value that indicates whether the check boxes behave like radio buttons.
''' </summary>
''' <value>
''' <b>true</b> if only one item can be checked at a time; otherwise, <b>false</b>.
''' </value>
<Category("Behavior")> _
<Description("Indicates whether only a single item can be checked or not.")> _
<DefaultValue(True)> _
Public Property RadioCheck() As Boolean
Get
Return Me._radioCheck
End Get
Set(ByVal value As Boolean)
If Me._radioCheck <> value Then
Me._radioCheck = value
Me.OnRadioCheckChanged(EventArgs.Empty)
End If
End Set
End Property
''' <summary>
''' Raised when the <see cref="RadioCheck" /> property value changes.
''' </summary>
''' <remarks></remarks>
Public Event RadioCheckChanged As EventHandler
''' <summary>
''' Raises the <see cref="RadioCheckChanged" /> event.
''' </summary>
''' <param name="e">
''' The data for the event.
''' </param>
Protected Overridable Sub OnRadioCheckChanged(ByVal e As EventArgs)
RaiseEvent RadioCheckChanged(Me, e)
End Sub
''' <summary>
''' Raises the <see cref="ItemCheck" /> event.
''' </summary>
''' <param name="e">
''' The data for the event.
''' </param>
''' <remarks>
''' If an item is being checked, all other items are unchecked.
''' </remarks>
Protected Overrides Sub OnItemCheck(ByVal e As System.Windows.Forms.ItemCheckEventArgs)
If Me._radioCheck AndAlso e.NewValue = CheckState.Checked Then
'An item is being checked so uncheck all others.
For index As Integer = 0 To Me.Items.Count - 1
If index <> e.Index Then
Me.SetItemChecked(index, False)
End If
Next
End If
MyBase.OnItemCheck(e)
End Sub
End Class
Re: CheckedListBox with RadioButton-style Behaviour
Cool.
You could also add this code to let the control draw radiobuttons instead of checkboxes:
vb.net Code:
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
MyBase.OnDrawItem(e)
If e.Index >= 0 AndAlso e.Index < Me.Items.Count Then
Dim checkRect As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height)
Dim state As ButtonState
If Me.GetItemChecked(e.Index) Then
state = ButtonState.Checked
Else
state = ButtonState.Normal
End If
' Hide checkboxes
Using b As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(b, checkRect)
End Using
' Paint radio buttons
ControlPaint.DrawRadioButton(e.Graphics, checkRect, state)
End If
End Sub
Unfortunately, they don't look like the actual OS radio buttons (they're close, but not exactly), but I'm not sure how else to draw a radio button without actually putting a radiobutton there... It does the job though :)
http://i42.tinypic.com/j9aczo.jpg
Re: CheckedListBox with RadioButton-style Behaviour
Nice idea Nick. The RadioButtonRenderer class might let you draw a themed radio button.
Re: CheckedListBox with RadioButton-style Behaviour
Quote:
Originally Posted by
jmcilhinney
Nice idea Nick. The RadioButtonRenderer class might let you draw a themed radio button.
Ah, didn't know that. That looks much better :)
vb.net Code:
''' <summary>
''' Paints a RadioButton instead of a CheckBox on each item.
''' </summary>
''' <param name="e">The data for the event.</param>
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
MyBase.OnDrawItem(e)
If Me.RadioCheck AndAlso e.Index >= 0 AndAlso e.Index < Me.Items.Count Then
Dim checkRect As Rectangle = Me.GetCheckboxRectangle(e.Bounds)
Dim state As VisualStyles.RadioButtonState
If Me.GetItemChecked(e.Index) Then
state = VisualStyles.RadioButtonState.CheckedNormal
Else
state = VisualStyles.RadioButtonState.UncheckedNormal
End If
' Hide original checkbox
Using b As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(b, checkRect)
End Using
' Paint radio button
RadioButtonRenderer.DrawRadioButton(e.Graphics, checkRect.Location, state)
End If
End Sub
http://i42.tinypic.com/w80vtu.png
I tried to implement hot-tracking (and different style when pressed), but it resulted in loads of flickering that I couldn't solve without having to draw the entire item manually. While that's not a lot of work I decided to let it go for now, it looks great already.
EDIT
I added a check for RadioCheck, so that it still draws normal checkboxes when it is false.
Re: CheckedListBox with RadioButton-style Behaviour
Someone wanna post the entire class' code?
Re: CheckedListBox with RadioButton-style Behaviour
Quote:
Originally Posted by
JuggaloBrotha
Someone wanna post the entire class' code?
It's already in the first post..? :p Just paste my code snippet in if you need that too. Quote my post in order to copy the code without linenumbers.