using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TradeIdeas.TIProGUI { public partial class CustomCheckedListBox : CheckedListBox { public CustomCheckedListBox() { this.DoubleBuffered = true; } //courtesy of http://stackoverflow.com/questions/3666682/which-checkedlistbox-event-triggers-after-a-item-is-checked //This class will trigger *after* the item has been checked (after CheckedItems is updated). It simplifies things greatly for certain purposes. //Normally, when one clicks the checkbox, the ItemCheck event fires *before* the CheckedItems is actually updated. protected override void OnItemCheck(ItemCheckEventArgs e) { base.OnItemCheck(e); EventHandler handler = AfterItemCheck; if (handler != null) { Delegate[] invocationList = AfterItemCheck.GetInvocationList(); foreach (var receiver in invocationList) { AfterItemCheck -= (EventHandler)receiver; } SetItemCheckState(e.Index, e.NewValue); foreach (var receiver in invocationList) { AfterItemCheck += (EventHandler)receiver; } } OnAfterItemCheck(EventArgs.Empty); } public event EventHandler AfterItemCheck; public void OnAfterItemCheck(EventArgs e) { EventHandler handler = AfterItemCheck; if (handler != null) handler(this, e); } public override Font Font { get { return base.Font; } set { base.Font = value; CalculateItemHeight(); } } private const int CHECKBOX_HEIGHT_ADJUSTMENT = 6; private void CalculateItemHeight() { Size measuredItemSize = TextRenderer.MeasureText("XXX", Font); ItemHeight = measuredItemSize.Height + CHECKBOX_HEIGHT_ADJUSTMENT; } private int _itemHeight = DefaultItemHeight; public override int ItemHeight { get { return _itemHeight; } set { _itemHeight = value; } } } }