Results 1 to 6 of 6

Thread: CheckedListBox with RadioButton-style Behaviour

Threaded View

  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

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