Results 1 to 6 of 6

Thread: CheckedListBox with RadioButton-style Behaviour

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    CheckedListBox with RadioButton-style Behaviour

    C# version here.
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. ''' <summary>
    4. ''' Provides RadioButton-style functionality in a CheckedListBox.
    5. ''' </summary>
    6. <ToolboxBitmap(GetType(CheckedListBox))> _
    7. Public Class RadioCheckedListBox
    8.     Inherits CheckedListBox
    9.  
    10.     ''' <summary>
    11.     ''' Indicates whether the check boxes behave like radio buttons.
    12.     ''' </summary>
    13.     Private _radioCheck As Boolean = True
    14.  
    15.  
    16.     ''' <summary>
    17.     ''' Gets or sets a value that indicates whether the check boxes behave like radio buttons.
    18.     ''' </summary>
    19.     ''' <value>
    20.     ''' <b>true</b> if only one item can be checked at a time; otherwise, <b>false</b>.
    21.     ''' </value>
    22.     <Category("Behavior")> _
    23.     <Description("Indicates whether only a single item can be checked or not.")> _
    24.     <DefaultValue(True)> _
    25.     Public Property RadioCheck() As Boolean
    26.         Get
    27.             Return Me._radioCheck
    28.         End Get
    29.         Set(ByVal value As Boolean)
    30.             If Me._radioCheck <> value Then
    31.                 Me._radioCheck = value
    32.                 Me.OnRadioCheckChanged(EventArgs.Empty)
    33.             End If
    34.         End Set
    35.     End Property
    36.  
    37.  
    38.     ''' <summary>
    39.     ''' Raised when the <see cref="RadioCheck" /> property value changes.
    40.     ''' </summary>
    41.     ''' <remarks></remarks>
    42.     Public Event RadioCheckChanged As EventHandler
    43.  
    44.  
    45.     ''' <summary>
    46.     ''' Raises the <see cref="RadioCheckChanged" /> event.
    47.     ''' </summary>
    48.     ''' <param name="e">
    49.     ''' The data for the event.
    50.     ''' </param>
    51.     Protected Overridable Sub OnRadioCheckChanged(ByVal e As EventArgs)
    52.         RaiseEvent RadioCheckChanged(Me, e)
    53.     End Sub
    54.  
    55.     ''' <summary>
    56.     ''' Raises the <see cref="ItemCheck" /> event.
    57.     ''' </summary>
    58.     ''' <param name="e">
    59.     ''' The data for the event.
    60.     ''' </param>
    61.     ''' <remarks>
    62.     ''' If an item is being checked, all other items are unchecked.
    63.     ''' </remarks>
    64.     Protected Overrides Sub OnItemCheck(ByVal e As System.Windows.Forms.ItemCheckEventArgs)
    65.         If Me._radioCheck AndAlso e.NewValue = CheckState.Checked Then
    66.             'An item is being checked so uncheck all others.
    67.             For index As Integer = 0 To Me.Items.Count - 1
    68.                 If index <> e.Index Then
    69.                     Me.SetItemChecked(index, False)
    70.                 End If
    71.             Next
    72.         End If
    73.  
    74.         MyBase.OnItemCheck(e)
    75.     End Sub
    76.  
    77. End Class
    Attached Files Attached Files
    Last edited by jmcilhinney; May 11th, 2010 at 12:41 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    2.         MyBase.OnDrawItem(e)
    3.  
    4.         If e.Index >= 0 AndAlso e.Index < Me.Items.Count Then
    5.  
    6.             Dim checkRect As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height)
    7.             Dim state As ButtonState
    8.             If Me.GetItemChecked(e.Index) Then
    9.                 state = ButtonState.Checked
    10.             Else
    11.                 state = ButtonState.Normal
    12.             End If
    13.  
    14.             ' Hide checkboxes
    15.             Using b As New SolidBrush(Me.BackColor)
    16.                 e.Graphics.FillRectangle(b, checkRect)
    17.             End Using
    18.  
    19.             ' Paint radio buttons
    20.             ControlPaint.DrawRadioButton(e.Graphics, checkRect, state)
    21.  
    22.         End If
    23.     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


  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: CheckedListBox with RadioButton-style Behaviour

    Nice idea Nick. The RadioButtonRenderer class might let you draw a themed radio button.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: CheckedListBox with RadioButton-style Behaviour

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. ''' <summary>
    2.     ''' Paints a RadioButton instead of a CheckBox on each item.
    3.     ''' </summary>
    4.     ''' <param name="e">The data for the event.</param>
    5.     Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    6.         MyBase.OnDrawItem(e)
    7.  
    8.         If Me.RadioCheck AndAlso e.Index >= 0 AndAlso e.Index < Me.Items.Count Then
    9.  
    10.             Dim checkRect As Rectangle = Me.GetCheckboxRectangle(e.Bounds)
    11.  
    12.             Dim state As VisualStyles.RadioButtonState
    13.             If Me.GetItemChecked(e.Index) Then
    14.                 state = VisualStyles.RadioButtonState.CheckedNormal
    15.             Else
    16.                 state = VisualStyles.RadioButtonState.UncheckedNormal
    17.             End If
    18.  
    19.             ' Hide original checkbox
    20.             Using b As New SolidBrush(Me.BackColor)
    21.                 e.Graphics.FillRectangle(b, checkRect)
    22.             End Using
    23.  
    24.             ' Paint radio button
    25.             RadioButtonRenderer.DrawRadioButton(e.Graphics, checkRect.Location, state)
    26.  
    27.         End If
    28.     End Sub




    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.

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: CheckedListBox with RadioButton-style Behaviour

    Someone wanna post the entire class' code?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: CheckedListBox with RadioButton-style Behaviour

    Quote Originally Posted by JuggaloBrotha View Post
    Someone wanna post the entire class' code?
    It's already in the first post..? Just paste my code snippet in if you need that too. Quote my post in order to copy the code without linenumbers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width