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;
53 SetStyle (ControlStyles.ResizeRedraw, true);
58 [EditorBrowsable (EditorBrowsableState.Never)]
59 public new event EventHandler Click;
62 [EditorBrowsable (EditorBrowsableState.Never)]
63 public new event EventHandler DataSourceChanged;
66 [EditorBrowsable (EditorBrowsableState.Never)]
67 public new event EventHandler DisplayMemberChanged;
70 [EditorBrowsable (EditorBrowsableState.Never)]
71 public new event DrawItemEventHandler DrawItem;
72 public event ItemCheckEventHandler ItemCheck;
75 [EditorBrowsable (EditorBrowsableState.Never)]
76 public new event MeasureItemEventHandler MeasureItem;
79 [EditorBrowsable (EditorBrowsableState.Never)]
80 public new event EventHandler ValueMemberChanged;
83 #region Public Properties
86 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87 public CheckedListBox.CheckedIndexCollection CheckedIndices {
88 get {return checked_indices; }
92 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93 public CheckedListBox.CheckedItemCollection CheckedItems {
94 get {return checked_items; }
97 [DefaultValue (false)]
98 public bool CheckOnClick {
99 get { return check_onclick; }
100 set { check_onclick = value; }
103 protected override CreateParams CreateParams {
104 get { return base.CreateParams;}
107 [EditorBrowsable (EditorBrowsableState.Never)]
109 public new object DataSource {
110 get { return base.DataSource; }
111 set { base.DataSource = value; }
114 [EditorBrowsable (EditorBrowsableState.Never)]
116 public new string DisplayMember {
117 get { return base.DisplayMember; }
118 set { base.DisplayMember = value; }
122 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
123 [EditorBrowsable (EditorBrowsableState.Never)]
124 public override DrawMode DrawMode {
125 get { return DrawMode.Normal; }
126 set { /* Not possible */ }
130 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
131 [EditorBrowsable (EditorBrowsableState.Never)]
132 public override int ItemHeight {
133 get { return listbox_info.item_height; }
134 set { /* Not possible */ }
137 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
139 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
140 public new CheckedListBox.ObjectCollection Items {
141 get { return (CheckedListBox.ObjectCollection) base.Items; }
144 public override SelectionMode SelectionMode {
145 get { return base.SelectionMode; }
147 if (value == SelectionMode.MultiSimple || value == SelectionMode.MultiExtended)
148 throw new InvalidEnumArgumentException ("Multi selection modes not supported");
150 base.SelectionMode = value;
154 [DefaultValue (false)]
155 public bool ThreeDCheckBoxes {
156 get { return three_dcheckboxes; }
158 if (three_dcheckboxes == value)
161 three_dcheckboxes = value;
167 [EditorBrowsable (EditorBrowsableState.Never)]
168 public new string ValueMember {
169 get { return base.ValueMember; }
170 set { base.ValueMember = value; }
173 #endregion Public Properties
175 #region Public Methods
177 protected override AccessibleObject CreateAccessibilityInstance ()
179 return base.CreateAccessibilityInstance ();
182 protected override ListBox.ObjectCollection CreateItemCollection ()
184 return new ObjectCollection (this);
187 public bool GetItemChecked (int index)
189 return (GetItemCheckState (index) != CheckState.Unchecked);
192 public CheckState GetItemCheckState (int index)
194 if (index < 0 || index >= Items.Count)
195 throw new ArgumentOutOfRangeException ("Index of out range");
197 return (Items.GetListBoxItem (index)).State;
200 protected override void OnBackColorChanged (EventArgs e)
202 base.OnBackColorChanged (e);
205 protected override void OnClick (EventArgs e)
210 Click (this, EventArgs.Empty);
213 protected override void OnDrawItem (DrawItemEventArgs e)
215 ThemeEngine.Current.DrawCheckedListBoxItem (this, e);
218 protected override void OnFontChanged (EventArgs e)
220 base.OnFontChanged (e);
223 protected override void OnHandleCreated (EventArgs e)
225 base.OnHandleCreated (e);
228 protected virtual void OnItemCheck (ItemCheckEventArgs ice)
230 if (ItemCheck != null)
231 ItemCheck (this, ice);
234 protected override void OnKeyPress (KeyPressEventArgs e)
238 if (e.KeyChar == ' ') { // Space should check focused item
239 if (focused_item != -1) {
240 SetItemChecked (focused_item, !GetItemChecked (focused_item));
245 protected override void OnMeasureItem (MeasureItemEventArgs e)
247 if (MeasureItem != null)
248 MeasureItem (this, e);
251 protected override void OnSelectedIndexChanged (EventArgs e)
253 base.OnSelectedIndexChanged (e);
256 public void SetItemChecked (int index, bool value)
258 SetItemCheckState (index, value ? CheckState.Checked : CheckState.Unchecked);
261 public void SetItemCheckState (int index, CheckState value)
263 if (index < 0 || index >= Items.Count)
264 throw new ArgumentOutOfRangeException ("Index of out range");
266 if (!Enum.IsDefined (typeof (CheckState), value))
267 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for CheckState", value));
269 CheckState old_value = (Items.GetListBoxItem (index)).State;
271 if (old_value == value)
274 (Items.GetListBoxItem (index)).State = value;
276 Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
279 case CheckState.Checked:
280 checked_indices.AddIndex (index);
281 checked_items.AddObject (Items[index]);
283 case CheckState.Unchecked:
284 checked_indices.RemoveIndex (index);
285 checked_items.RemoveObject (Items[index]);
287 case CheckState.Indeterminate:
292 OnItemCheck (new ItemCheckEventArgs (index, value, old_value));
294 if (ClientRectangle.Contains (invalidate))
295 Invalidate (invalidate);
298 protected override void WmReflectCommand (ref Message m)
300 base.WmReflectCommand (ref m);
303 protected override void WndProc (ref Message m)
305 base.WndProc (ref m);
308 #endregion Public Methods
310 #region Private Methods
312 internal override void OnMouseDownLB (object sender, MouseEventArgs e)
315 CheckState value = CheckState.Checked;
316 bool set_value = false;
317 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
320 if (e.Button == MouseButtons.Left) {
329 hit_rect = GetItemDisplayRectangle (index, LBoxInfo.top_item); // Full item rect
330 hit_rect.X += ThemeEngine.Current.CheckedListBoxCheckRectangle().X;
331 hit_rect.Y += ThemeEngine.Current.CheckedListBoxCheckRectangle().Y;
332 hit_rect.Width = ThemeEngine.Current.CheckedListBoxCheckRectangle().Width;
333 hit_rect.Height = ThemeEngine.Current.CheckedListBoxCheckRectangle().Height;
335 if ((Items.GetListBoxItem (index)).State == CheckState.Checked) { //From checked to unchecked
336 value = CheckState.Unchecked;
343 if ((Items.GetListBoxItem (index)).Selected == true)
349 SetItemCheckState (index, value);
351 SelectedItemFromNavigation (index);
352 SetFocusedItem (index);
355 internal override void UpdateItemInfo (UpdateOperation operation, int first, int last)
357 base.UpdateItemInfo (operation, first, last);
358 CheckedItems.ReCreate ();
359 CheckedIndices.ReCreate ();
362 #endregion Private Methods
364 public class ObjectCollection : ListBox.ObjectCollection
366 private CheckedListBox owner;
368 public ObjectCollection (CheckedListBox owner) : base (owner)
373 public int Add (object item, bool isChecked)
376 return Add (item, CheckState.Checked);
378 return Add (item, CheckState.Unchecked);
382 public int Add (object item, CheckState check)
384 int cnt = object_items.Count;
385 ListBox.ListBoxItem box_item = new ListBox.ListBoxItem (cnt);
386 box_item.State = check;
387 object_items.Add (item);
388 listbox_items.Add (box_item);
389 if (check == CheckState.Checked)
390 owner.OnItemCheck (new ItemCheckEventArgs (cnt, check, CheckState.Unchecked));
391 owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, cnt);
397 CheckedListBox.CheckedIndexCollection
399 public class CheckedIndexCollection : IList, ICollection, IEnumerable
401 private CheckedListBox owner;
402 private ArrayList indices = new ArrayList ();
404 internal CheckedIndexCollection (CheckedListBox owner)
409 #region Public Properties
410 public virtual int Count {
411 get { return indices.Count; }
414 public virtual bool IsReadOnly {
418 bool ICollection.IsSynchronized {
419 get { return false; }
422 bool IList.IsFixedSize{
426 object ICollection.SyncRoot {
431 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
432 public int this[int index] {
434 if (index < 0 || index >= Count)
435 throw new ArgumentOutOfRangeException ("Index of out range");
437 return (int) indices[index];
440 #endregion Public Properties
442 public bool Contains (int index)
444 return indices.Contains (index);
448 public virtual void CopyTo (Array dest, int index)
450 indices.CopyTo (dest, index);
453 public virtual IEnumerator GetEnumerator ()
455 return indices.GetEnumerator ();
458 int IList.Add (object value)
460 throw new NotSupportedException ();
465 throw new NotSupportedException ();
468 bool IList.Contains (object index)
470 return Contains ((int)index);
473 int IList.IndexOf (object index)
475 return IndexOf ((int) index);
478 void IList.Insert (int index, object value)
480 throw new NotSupportedException ();
483 void IList.Remove (object value)
485 throw new NotSupportedException ();
488 void IList.RemoveAt (int index)
490 throw new NotSupportedException ();
493 object IList.this[int index]{
494 get {return indices[index]; }
495 set {throw new NotImplementedException (); }
498 public int IndexOf (int index)
500 return indices.IndexOf (index);
503 #region Private Methods
505 internal void AddIndex (int index)
510 internal void ClearIndices ()
515 internal void RemoveIndex (int index)
517 indices.Remove (index);
520 internal void ReCreate ()
524 for (int i = 0; i < owner.Items.Count; i++) {
525 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
527 if (item.State == CheckState.Checked)
528 indices.Add (item.Index);
532 #endregion Private Methods
536 CheckedItemCollection
538 public class CheckedItemCollection : IList, ICollection, IEnumerable
540 private CheckedListBox owner;
541 private ArrayList object_items = new ArrayList ();
543 internal CheckedItemCollection (CheckedListBox owner)
548 #region Public Properties
549 public virtual int Count {
550 get { return object_items.Count; }
553 public virtual bool IsReadOnly {
558 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
559 public virtual object this [int index] {
561 if (index < 0 || index >= Count)
562 throw new ArgumentOutOfRangeException ("Index of out range");
564 return object_items[index];
566 set {throw new NotSupportedException ();}
569 bool ICollection.IsSynchronized {
573 object ICollection.SyncRoot {
577 bool IList.IsFixedSize {
581 object IList.this[int index] {
582 get { return object_items[index]; }
583 set { throw new NotSupportedException (); }
586 #endregion Public Properties
588 #region Public Methods
589 public virtual bool Contains (object selectedObject)
591 return object_items.Contains (selectedObject);
594 public virtual void CopyTo (Array dest, int index)
596 object_items.CopyTo (dest, index);
599 int IList.Add (object value)
601 throw new NotSupportedException ();
606 throw new NotSupportedException ();
609 bool IList.Contains (object selectedIndex)
611 throw new NotImplementedException ();
614 void IList.Insert (int index, object value)
616 throw new NotSupportedException ();
619 void IList.Remove (object value)
621 throw new NotSupportedException ();
624 void IList.RemoveAt (int index)
626 throw new NotSupportedException ();
629 public int IndexOf (object item)
631 return object_items.IndexOf (item);
634 public virtual IEnumerator GetEnumerator ()
636 return object_items.GetEnumerator ();
639 #endregion Public Methods
641 #region Private Methods
642 internal void AddObject (object obj)
644 object_items.Add (obj);
647 internal void ClearObjects ()
649 object_items.Clear ();
652 internal void ReCreate ()
654 object_items.Clear ();
656 for (int i = 0; i < owner.Items.Count; i++) {
657 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
659 if (item.State == CheckState.Checked)
660 object_items.Add (owner.Items[item.Index]);
664 internal void RemoveObject (object obj)
666 object_items.Remove (obj);
668 #endregion Private Methods