CheckedListBox with RadioButton-style Behaviour-VBForums
Results 1 to 1 of 1

Thread: CheckedListBox with RadioButton-style Behaviour

  1. #1

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,643

    CheckedListBox with RadioButton-style Behaviour

    VB version here.
    csharp Code:
    1. using System;
    2. using System.ComponentModel;
    3. using System.Drawing;
    4. using System.Windows.Forms;
    5.  
    6. namespace Wunnell.Windows.Forms
    7. {
    8.     /// <summary>
    9.     /// Provides RadioButton-style functionality in a CheckedListBox.
    10.     /// </summary>
    11.     [ToolboxBitmap(typeof(CheckedListBox))]
    12.     public class RadioCheckedListBox : CheckedListBox
    13.     {
    14.         /// <summary>
    15.         /// Indicates whether the check boxes behave like radio buttons.
    16.         /// </summary>
    17.         private bool _radioCheck = true;
    18.  
    19.  
    20.         /// <summary>
    21.         /// Gets or sets a value that indicates whether the check boxes behave like radio buttons.
    22.         /// </summary>
    23.         /// <value>
    24.         /// <b>true</b> if only one item can be checked at a time; otherwise, <b>false</b>.
    25.         /// </value>
    26.         [Category("Behavior")]
    27.         [Description("Indicates whether only a single item can be checked or not.")]
    28.         [DefaultValue(true)]
    29.         public bool RadioCheck
    30.         {
    31.             get
    32.             {
    33.                 return this._radioCheck;
    34.             }
    35.             set
    36.             {
    37.                 if (this._radioCheck != value)
    38.                 {
    39.                     this._radioCheck = value;
    40.                     this.OnRadioCheckChanged(EventArgs.Empty);
    41.                 }
    42.             }
    43.         }
    44.  
    45.  
    46.         /// <summary>
    47.         /// Raised when the <see cref="RadioCheck" /> property value changes.
    48.         /// </summary>
    49.         /// <remarks></remarks>
    50.         public event EventHandler RadioCheckChanged;
    51.  
    52.  
    53.         /// <summary>
    54.         /// Raises the <see cref="RadioCheckChanged" /> event.
    55.         /// </summary>
    56.         /// <param name="e">
    57.         /// The data for the event.
    58.         /// </param>
    59.         protected virtual void OnRadioCheckChanged(EventArgs e)
    60.         {
    61.             if (this.RadioCheckChanged != null)
    62.             {
    63.                 this.RadioCheckChanged(this, e);
    64.             }
    65.         }
    66.  
    67.         /// <summary>
    68.         /// Raises the <see cref="CheckedListBox.ItemCheck" /> event.
    69.         /// </summary>
    70.         /// <param name="e">
    71.         /// The data for the event.
    72.         /// </param>
    73.         /// <remarks>
    74.         /// If an item is being checked, all other items are unchecked.
    75.         /// </remarks>
    76.         protected override void OnItemCheck(ItemCheckEventArgs e)
    77.         {
    78.             if(this._radioCheck && e.NewValue== CheckState.Checked)
    79.             {
    80.                 // An item is being checked so uncheck all others.
    81.                 for (int index = 0; index < this.Items.Count; index++)
    82.                 {
    83.                     if (index != e.Index)
    84.                     {
    85.                         this.SetItemChecked(index, false);
    86.                     }
    87.                 }
    88.             }
    89.  
    90.             base.OnItemCheck(e);
    91.         }
    92.     }
    93. }
    Attached Files Attached Files
    Last edited by jmcilhinney; May 11th, 2010 at 12:46 AM.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.