using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Wunnell.Windows.Forms
{
///
/// Provides RadioButton-style functionality in a CheckedListBox.
///
[ToolboxBitmap(typeof(CheckedListBox))]
public class RadioCheckedListBox : CheckedListBox
{
///
/// Indicates whether the check boxes behave like radio buttons.
///
private bool _radioCheck = true;
///
/// Gets or sets a value that indicates whether the check boxes behave like radio buttons.
///
///
/// true if only one item can be checked at a time; otherwise, false.
///
[Category("Behavior")]
[Description("Indicates whether only a single item can be checked or not.")]
[DefaultValue(true)]
public bool RadioCheck
{
get
{
return this._radioCheck;
}
set
{
if (this._radioCheck != value)
{
this._radioCheck = value;
this.OnRadioCheckChanged(EventArgs.Empty);
}
}
}
///
/// Raised when the property value changes.
///
///
public event EventHandler RadioCheckChanged;
///
/// Raises the event.
///
///
/// The data for the event.
///
protected virtual void OnRadioCheckChanged(EventArgs e)
{
if (this.RadioCheckChanged != null)
{
this.RadioCheckChanged(this, e);
}
}
///
/// Raises the event.
///
///
/// The data for the event.
///
///
/// If an item is being checked, all other items are unchecked.
///
protected override void OnItemCheck(ItemCheckEventArgs e)
{
if(this._radioCheck && e.NewValue== CheckState.Checked)
{
// An item is being checked so uncheck all others.
for (int index = 0; index < this.Items.Count; index++)
{
if (index != e.Index)
{
this.SetItemChecked(index, false);
}
}
}
base.OnItemCheck(e);
}
}
}