1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Reflection;
35 namespace System.Windows.Forms
38 public class CheckedListBox : ListBox
40 private CheckedIndexCollection checked_indices;
41 private CheckedItemCollection checked_items;
42 private bool check_onclick;
43 private bool three_dcheckboxes;
45 public CheckedListBox ()
47 items = new CheckedListBox.ObjectCollection (this);
48 checked_indices = new CheckedIndexCollection (this);
49 checked_items = new CheckedItemCollection (this);
50 check_onclick = false;
51 three_dcheckboxes = false;
52 listbox_info.item_height = FontHeight + 2;
57 [EditorBrowsable (EditorBrowsableState.Never)]
58 public new event EventHandler Click;
61 [EditorBrowsable (EditorBrowsableState.Never)]
62 public new event EventHandler DataSourceChanged;
65 [EditorBrowsable (EditorBrowsableState.Never)]
66 public new event EventHandler DisplayMemberChanged;
69 [EditorBrowsable (EditorBrowsableState.Never)]
70 public new event DrawItemEventHandler DrawItem;
71 public event ItemCheckEventHandler ItemCheck;
74 [EditorBrowsable (EditorBrowsableState.Never)]
75 public new event MeasureItemEventHandler MeasureItem;
78 [EditorBrowsable (EditorBrowsableState.Never)]
79 public new event EventHandler ValueMemberChanged;
82 #region Public Properties
85 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
86 public CheckedListBox.CheckedIndexCollection CheckedIndices {
87 get {return checked_indices; }
91 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
\r
92 public CheckedListBox.CheckedItemCollection CheckedItems {
93 get {return checked_items; }
96 [DefaultValue (false)]
97 public bool CheckOnClick {
98 get { return check_onclick; }
99 set { check_onclick = value; }
102 protected override CreateParams CreateParams {
103 get { return base.CreateParams;}
106 [EditorBrowsable (EditorBrowsableState.Never)]
107 [Browsable (false)]
\r
108 public new object DataSource {
109 get { return base.DataSource; }
110 set { base.DataSource = value; }
113 [EditorBrowsable (EditorBrowsableState.Never)]
115 public new string DisplayMember {
116 get { return base.DisplayMember; }
117 set { base.DisplayMember = value; }
121 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
122 [EditorBrowsable (EditorBrowsableState.Never)]
123 public override DrawMode DrawMode {
124 get { return DrawMode.Normal; }
125 set { /* Not possible */ }
129 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
130 [EditorBrowsable (EditorBrowsableState.Never)]
131 public override int ItemHeight {
132 get { return listbox_info.item_height; }
133 set { /* Not possible */ }
136 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
138 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
139 public new CheckedListBox.ObjectCollection Items {
140 get { return (CheckedListBox.ObjectCollection) base.Items; }
143 public override SelectionMode SelectionMode {
144 get { return base.SelectionMode; }
146 if (value == SelectionMode.MultiSimple || value == SelectionMode.MultiExtended)
147 throw new InvalidEnumArgumentException ("Multi selection modes not supported");
149 base.SelectionMode = value;
153 [DefaultValue (false)]
154 public bool ThreeDCheckBoxes {
155 get { return three_dcheckboxes; }
157 if (three_dcheckboxes == value)
160 three_dcheckboxes = value;
166 [EditorBrowsable (EditorBrowsableState.Never)]
167 public new string ValueMember {
168 get { return base.ValueMember; }
169 set { base.ValueMember = value; }
172 #endregion Public Properties
174 #region Public Methods
176 protected override AccessibleObject CreateAccessibilityInstance ()
178 return base.CreateAccessibilityInstance ();
181 protected override ListBox.ObjectCollection CreateItemCollection ()
183 return new ObjectCollection (this);
186 public bool GetItemChecked (int index)
188 return (GetItemCheckState (index) == CheckState.Checked);
191 public CheckState GetItemCheckState (int index)
193 if (index < 0 || index >= Items.Count)
194 throw new ArgumentOutOfRangeException ("Index of out range");
196 return (Items.GetListBoxItem (index)).State;
199 protected override void OnBackColorChanged (EventArgs e)
201 base.OnBackColorChanged (e);
204 protected override void OnClick (EventArgs e)
209 if (focused_item != -1) {
210 SetItemChecked (focused_item, !GetItemChecked (focused_item));
215 Click (this, EventArgs.Empty);
218 protected override void OnDrawItem (DrawItemEventArgs e)
220 ThemeEngine.Current.DrawCheckedListBoxItem (this, e);
223 protected override void OnFontChanged (EventArgs e)
225 base.OnFontChanged (e);
228 protected override void OnHandleCreated (EventArgs e)
230 base.OnHandleCreated (e);
233 protected virtual void OnItemCheck (ItemCheckEventArgs ice)
235 if (ItemCheck != null)
236 ItemCheck (this, ice);
239 protected override void OnKeyPress (KeyPressEventArgs e)
243 if (e.KeyChar == ' ') { // Space should check focused item
244 if (focused_item != -1) {
245 SetItemChecked (focused_item, !GetItemChecked (focused_item));
250 protected override void OnMeasureItem (MeasureItemEventArgs e)
252 if (MeasureItem != null)
253 MeasureItem (this, e);
256 protected override void OnSelectedIndexChanged (EventArgs e)
258 base.OnSelectedIndexChanged (e);
261 public void SetItemChecked (int index, bool value)
263 SetItemCheckState (index, value ? CheckState.Checked : CheckState.Unchecked);
266 public void SetItemCheckState (int index, CheckState value)
268 if (index < 0 || index >= Items.Count)
269 throw new ArgumentOutOfRangeException ("Index of out range");
271 if (!Enum.IsDefined (typeof (CheckState), value))
272 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for CheckState", value));
274 CheckState old_value = (Items.GetListBoxItem (index)).State;
276 if (old_value == value)
279 (Items.GetListBoxItem (index)).State = value;
281 Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
284 case CheckState.Checked:
285 checked_indices.AddIndex (index);
286 checked_items.AddObject (Items[index]);
288 case CheckState.Unchecked:
289 checked_indices.RemoveIndex (index);
290 checked_items.RemoveObject (Items[index]);
292 case CheckState.Indeterminate:
297 OnItemCheck (new ItemCheckEventArgs (index, value, old_value));
299 if (ClientRectangle.Contains (invalidate))
300 Invalidate (invalidate);
303 protected override void WmReflectCommand (ref Message m)
305 base.WmReflectCommand (ref m);
308 protected override void WndProc (ref Message m)
310 base.WndProc (ref m);
313 #endregion Public Methods
315 #region Private Methods
317 internal override void OnMouseDownLB (object sender, MouseEventArgs e)
319 base.OnMouseDownLB (sender, e);
321 Rectangle hit_rect, item_rect;
322 CheckState value = CheckState.Checked;
323 bool set_value = false;
324 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
330 hit_rect = item_rect = GetItemDisplayRectangle (index, LBoxInfo.top_item); // Full item rect
331 hit_rect.X += ThemeEngine.Current.CheckedListBoxCheckRectangle().X;
332 hit_rect.Y += ThemeEngine.Current.CheckedListBoxCheckRectangle().Y;
333 hit_rect.Width = ThemeEngine.Current.CheckedListBoxCheckRectangle().Width;
334 hit_rect.Height = ThemeEngine.Current.CheckedListBoxCheckRectangle().Height;
336 if ((Items.GetListBoxItem (index)).State == CheckState.Checked)
337 value = CheckState.Unchecked;
339 if (hit_rect.Contains (e.X, e.Y) == true) {
343 if (item_rect.Contains (e.X, e.Y) == true) {
344 if (!check_onclick) {
345 if ((Items.GetListBoxItem (index)).Selected == true)
352 SetItemCheckState (index, value);
356 internal override void UpdateItemInfo (UpdateOperation operation, int first, int last)
358 base.UpdateItemInfo (operation, first, last);
359 CheckedItems.ReCreate ();
360 CheckedIndices.ReCreate ();
363 #endregion Private Methods
365 public class ObjectCollection : ListBox.ObjectCollection
367 public ObjectCollection (CheckedListBox owner) : base (owner)
372 public int Add (object item, bool isChecked)
375 return Add (item, CheckState.Checked);
377 return Add (item, CheckState.Unchecked);
381 public int Add (object item, CheckState check)
383 int cnt = object_items.Count;
384 ListBox.ListBoxItem box_item = new ListBox.ListBoxItem (cnt);
385 box_item.State = check;
386 object_items.Add (item);
387 listbox_items.Add (box_item);
393 CheckedListBox.CheckedIndexCollection
395 public class CheckedIndexCollection : IList, ICollection, IEnumerable
397 private CheckedListBox owner;
398 private ArrayList indices = new ArrayList ();
400 internal CheckedIndexCollection (CheckedListBox owner)
405 #region Public Properties
406 public virtual int Count {
407 get { return indices.Count; }
410 public virtual bool IsReadOnly {
414 bool ICollection.IsSynchronized {
415 get { return false; }
418 bool IList.IsFixedSize{
422 object ICollection.SyncRoot {
427 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
428 public int this[int index] {
430 if (index < 0 || index >= Count)
431 throw new ArgumentOutOfRangeException ("Index of out range");
433 return (int) indices[index];
436 #endregion Public Properties
438 public bool Contains (int index)
440 return indices.Contains (index);
444 public virtual void CopyTo (Array dest, int index)
446 indices.CopyTo (dest, index);
449 public virtual IEnumerator GetEnumerator ()
451 return indices.GetEnumerator ();
454 int IList.Add (object value)
456 throw new NotSupportedException ();
461 throw new NotSupportedException ();
464 bool IList.Contains (object index)
466 return Contains ((int)index);
469 int IList.IndexOf (object index)
471 return IndexOf ((int) index);
474 void IList.Insert (int index, object value)
476 throw new NotSupportedException ();
479 void IList.Remove (object value)
481 throw new NotSupportedException ();
484 void IList.RemoveAt (int index)
486 throw new NotSupportedException ();
489 object IList.this[int index]{
490 get {return indices[index]; }
491 set {throw new NotImplementedException (); }
494 public int IndexOf (int index)
496 return indices.IndexOf (index);
499 #region Private Methods
501 internal void AddIndex (int index)
506 internal void ClearIndices ()
511 internal void RemoveIndex (int index)
513 indices.Remove (index);
516 internal void ReCreate ()
520 for (int i = 0; i < owner.Items.Count; i++) {
521 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
523 if (item.State == CheckState.Checked)
524 indices.Add (item.Index);
528 #endregion Private Methods
532 CheckedItemCollection
534 public class CheckedItemCollection : IList, ICollection, IEnumerable
536 private CheckedListBox owner;
537 private ArrayList object_items = new ArrayList ();
539 internal CheckedItemCollection (CheckedListBox owner)
544 #region Public Properties
545 public virtual int Count {
546 get { return object_items.Count; }
549 public virtual bool IsReadOnly {
554 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
555 public virtual object this [int index] {
557 if (index < 0 || index >= Count)
558 throw new ArgumentOutOfRangeException ("Index of out range");
560 return object_items[index];
562 set {throw new NotSupportedException ();}
565 bool ICollection.IsSynchronized {
569 object ICollection.SyncRoot {
573 bool IList.IsFixedSize {
577 object IList.this[int index] {
578 get { return object_items[index]; }
579 set { throw new NotSupportedException (); }
582 #endregion Public Properties
584 #region Public Methods
585 public virtual bool Contains (object selectedObject)
587 return object_items.Contains (selectedObject);
590 public virtual void CopyTo (Array dest, int index)
592 object_items.CopyTo (dest, index);
595 int IList.Add (object value)
597 throw new NotSupportedException ();
602 throw new NotSupportedException ();
605 bool IList.Contains (object selectedIndex)
607 throw new NotImplementedException ();
610 void IList.Insert (int index, object value)
612 throw new NotSupportedException ();
615 void IList.Remove (object value)
617 throw new NotSupportedException ();
620 void IList.RemoveAt (int index)
622 throw new NotSupportedException ();
625 public int IndexOf (object item)
627 return object_items.IndexOf (item);
630 public virtual IEnumerator GetEnumerator ()
632 return object_items.GetEnumerator ();
635 #endregion Public Methods
637 #region Private Methods
638 internal void AddObject (object obj)
640 object_items.Add (obj);
643 internal void ClearObjects ()
645 object_items.Clear ();
648 internal void ReCreate ()
650 object_items.Clear ();
652 for (int i = 0; i < owner.Items.Count; i++) {
653 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
655 if (item.State == CheckState.Checked)
656 object_items.Add (owner.Items[item.Index]);
660 internal void RemoveObject (object obj)
662 object_items.Remove (obj);
664 #endregion Private Methods