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
30 using System.Collections;
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.ComponentModel.Design.Serialization;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
37 namespace System.Windows.Forms
39 [DefaultProperty("Items")]
40 [DefaultEvent("SelectedIndexChanged")]
41 [Designer ("System.Windows.Forms.Design.ListBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42 public class ListBox : ListControl
44 public const int DefaultItemHeight = 13;
45 public const int NoMatches = -1;
47 internal class ListBoxInfo
49 internal int item_height; /* Item's height */
50 internal int top_item; /* First item that we show the in the current page */
51 internal int last_item; /* Last visible item */
52 internal int page_size; /* Number of listbox items per page. In MultiColumn listbox indicates items per column */
53 internal Rectangle textdrawing_rect; /* Displayable Client Rectangle minus the scrollbars and with IntegralHeight calculated*/
54 internal bool show_verticalsb; /* Is Vertical scrollbar show it? */
55 internal bool show_horizontalsb; /* Is Horizontal scrollbar show it? */
56 internal Rectangle client_rect; /* Client Rectangle. Usually = ClientRectangle except when IntegralHeight has been applied*/
57 internal int max_itemwidth; /* Maxium item width within the listbox */
66 show_verticalsb = false;
67 show_horizontalsb = false;
71 internal class ListBoxItem
74 internal bool Selected;
75 internal int ItemHeight; /* Only used for OwnerDrawVariable */
76 internal CheckState State;
78 public ListBoxItem (int index)
83 State = CheckState.Unchecked;
86 public void CopyState (ListBoxItem src)
88 Selected = src.Selected;
89 ItemHeight = src.ItemHeight;
94 internal enum ItemNavigation
106 internal enum UpdateOperation
113 private int column_width;
114 private DrawMode draw_mode;
115 private int horizontal_extent;
116 private bool horizontal_scrollbar;
117 private bool integral_height;
118 private bool multicolumn;
119 private bool scroll_always_visible;
120 private int selected_index;
121 private SelectedIndexCollection selected_indices;
122 private SelectedObjectCollection selected_items;
123 private SelectionMode selection_mode;
125 private bool use_tabstops;
126 private int column_width_internal;
127 private VScrollBar vscrollbar_ctrl;
128 private HScrollBar hscrollbar_ctrl;
129 private bool suspend_ctrlupdate;
130 private bool ctrl_pressed;
131 private bool shift_pressed;
132 private bool has_focus;
133 private bool use_item_height;
135 internal int focused_item;
136 internal ListBoxInfo listbox_info;
137 internal ObjectCollection items;
141 border_style = BorderStyle.Fixed3D;
142 draw_mode = DrawMode.Normal;
143 horizontal_extent = 0;
144 horizontal_scrollbar = false;
145 integral_height = true;
147 scroll_always_visible = false;
150 selection_mode = SelectionMode.One;
153 BackColor = ThemeEngine.Current.ColorWindow;
155 suspend_ctrlupdate = false;
156 ctrl_pressed = false;
157 shift_pressed = false;
159 use_item_height = false;
161 items = new ObjectCollection (this);
162 selected_indices = new SelectedIndexCollection (this);
163 selected_items = new SelectedObjectCollection (this);
164 listbox_info = new ListBoxInfo ();
165 listbox_info.item_height = FontHeight;
167 /* Vertical scrollbar */
168 vscrollbar_ctrl = new VScrollBar ();
169 vscrollbar_ctrl.Minimum = 0;
170 vscrollbar_ctrl.SmallChange = 1;
171 vscrollbar_ctrl.LargeChange = 1;
172 vscrollbar_ctrl.Maximum = 0;
173 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
174 vscrollbar_ctrl.Visible = false;
176 /* Horizontal scrollbar */
177 hscrollbar_ctrl = new HScrollBar ();
178 hscrollbar_ctrl.Minimum = 0;
179 hscrollbar_ctrl.SmallChange = 1;
180 hscrollbar_ctrl.LargeChange = 1;
181 hscrollbar_ctrl.Maximum = 0;
182 hscrollbar_ctrl.Visible = false;
183 hscrollbar_ctrl.ValueChanged += new EventHandler (HorizontalScrollEvent);
186 MouseDown += new MouseEventHandler (OnMouseDownLB);
187 KeyDown += new KeyEventHandler (OnKeyDownLB);
188 KeyUp += new KeyEventHandler (OnKeyUpLB);
189 GotFocus += new EventHandler (OnGotFocus);
190 LostFocus += new EventHandler (OnLostFocus);
192 SetStyle (ControlStyles.UserPaint, false);
197 [EditorBrowsable (EditorBrowsableState.Never)]
198 public new event EventHandler BackgroundImageChanged;
201 [EditorBrowsable (EditorBrowsableState.Advanced)]
202 public new event EventHandler Click;
204 public event DrawItemEventHandler DrawItem;
205 public event MeasureItemEventHandler MeasureItem;
208 [EditorBrowsable (EditorBrowsableState.Never)]
209 public new event PaintEventHandler Paint;
211 public event EventHandler SelectedIndexChanged;
214 [EditorBrowsable (EditorBrowsableState.Advanced)]
215 public new event EventHandler TextChanged;
218 #region Public Properties
219 public override Color BackColor {
220 get { return base.BackColor; }
222 if (base.BackColor == value)
225 base.BackColor = value;
226 base.Refresh (); // Careful. Calling the base method is not the same that calling
227 } // the overriden one that refresh also all the items
231 [EditorBrowsable (EditorBrowsableState.Never)]
232 public override Image BackgroundImage {
233 get { return base.BackgroundImage; }
235 if (base.BackgroundImage == value)
238 base.BackgroundImage = value;
240 if (BackgroundImageChanged != null)
241 BackgroundImageChanged (this, EventArgs.Empty);
247 [DefaultValue (BorderStyle.Fixed3D)]
249 public BorderStyle BorderStyle {
250 get { return InternalBorderStyle; }
251 set { InternalBorderStyle = value; }
256 public int ColumnWidth {
257 get { return column_width; }
260 throw new ArgumentException ("A value less than zero is assigned to the property.");
262 column_width = value;
265 ColumnWidthInternal = 120;
267 ColumnWidthInternal = value;
273 protected override CreateParams CreateParams {
274 get { return base.CreateParams;}
277 protected override Size DefaultSize {
278 get { return new Size (120, 96); }
281 [RefreshProperties(RefreshProperties.Repaint)]
282 [DefaultValue (DrawMode.Normal)]
283 public virtual DrawMode DrawMode {
284 get { return draw_mode; }
287 if (!Enum.IsDefined (typeof (DrawMode), value))
288 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
290 if (value == DrawMode.OwnerDrawVariable && multicolumn == true)
291 throw new ArgumentException ("Cannot have variable height and multicolumn");
293 if (draw_mode == value)
301 public override Color ForeColor {
302 get { return base.ForeColor; }
305 if (base.ForeColor == value)
308 base.ForeColor = value;
315 public int HorizontalExtent {
316 get { return horizontal_extent; }
318 if (horizontal_extent == value)
321 horizontal_extent = value;
326 [DefaultValue (false)]
328 public bool HorizontalScrollbar {
329 get { return horizontal_scrollbar; }
331 if (horizontal_scrollbar == value)
334 horizontal_scrollbar = value;
335 UpdateShowHorizontalScrollBar ();
340 [DefaultValue (true)]
342 [RefreshProperties(RefreshProperties.Repaint)]
343 public bool IntegralHeight {
344 get { return integral_height; }
346 if (integral_height == value)
349 integral_height = value;
356 [RefreshProperties(RefreshProperties.Repaint)]
357 public virtual int ItemHeight {
359 if (draw_mode == DrawMode.Normal)
361 return listbox_info.item_height;
365 throw new ArgumentOutOfRangeException ("The ItemHeight property was set beyond 255 pixels");
367 if (listbox_info.item_height == value)
370 listbox_info.item_height = value;
371 use_item_height = true;
376 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
378 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
379 public ObjectCollection Items {
380 get { return items; }
383 [DefaultValue (false)]
384 public bool MultiColumn {
385 get { return multicolumn; }
387 if (multicolumn == value)
390 if (value == true && DrawMode == DrawMode.OwnerDrawVariable)
391 throw new ArgumentException ("A multicolumn ListBox cannot have a variable-sized height.");
395 if (IsHandleCreated) {
396 RellocateScrollBars ();
398 UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
404 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
405 [EditorBrowsable (EditorBrowsableState.Advanced)]
406 public int PreferredHeight {
409 if (draw_mode == DrawMode.Normal)
410 itemsHeight = FontHeight * items.Count;
411 else if (draw_mode == DrawMode.OwnerDrawFixed)
412 itemsHeight = ItemHeight * items.Count;
413 else if (draw_mode == DrawMode.OwnerDrawVariable) {
414 for (int i = 0; i < items.Count; i++)
415 itemsHeight += items.GetListBoxItem (i).ItemHeight;
422 public override RightToLeft RightToLeft {
423 get { return base.RightToLeft; }
425 base.RightToLeft = value;
430 // Only affects the Vertical ScrollBar
431 [DefaultValue (false)]
433 public bool ScrollAlwaysVisible {
434 get { return scroll_always_visible; }
436 if (scroll_always_visible == value)
439 scroll_always_visible = value;
440 UpdateShowVerticalScrollBar ();
441 UpdateShowHorizontalScrollBar ();
447 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
448 public override int SelectedIndex {
449 get { return selected_index;}
451 if (value < -1 || value >= Items.Count)
452 throw new ArgumentOutOfRangeException ("Index of out range");
454 if (SelectionMode == SelectionMode.None)
455 throw new ArgumentException ("cannot call this method if SelectionMode is SelectionMode.None");
457 if (selected_index == value)
460 if (SelectionMode == SelectionMode.One)
461 UnSelectItem (selected_index, true);
464 selected_index = value;
465 focused_item = value;
466 OnSelectedIndexChanged (new EventArgs ());
467 OnSelectedValueChanged (new EventArgs ());
472 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
473 public SelectedIndexCollection SelectedIndices {
474 get { return selected_indices; }
479 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
480 public object SelectedItem {
482 if (SelectedItems.Count > 0)
483 return SelectedItems[0];
489 int index = Items.IndexOf (value);
494 if (index != SelectedIndex) {
495 SelectedIndex = index;
501 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
502 public SelectedObjectCollection SelectedItems {
503 get {return selected_items;}
506 [DefaultValue (SelectionMode.One)]
507 public virtual SelectionMode SelectionMode {
508 get { return selection_mode; }
510 if (!Enum.IsDefined (typeof (SelectionMode), value))
511 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for SelectionMode", value));
513 if (selection_mode == value)
516 selection_mode = value;
518 if (SelectedItems.Count > 0) {
519 switch (selection_mode) {
520 case SelectionMode.None:
523 case SelectionMode.One: {
524 if (SelectedItems.Count > 1) { // All except one
525 int cnt = selected_indices.Count - 1;
526 for (int i = 0; i < cnt; i++) {
527 UnSelectItem (i, true);
539 [DefaultValue (false)]
541 get { return sorted; }
554 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
555 [EditorBrowsable (EditorBrowsableState.Advanced)]
556 public override string Text {
558 if (SelectionMode != SelectionMode.None && SelectedIndex != -1)
559 return GetItemText (SelectedItem);
567 if (SelectionMode == SelectionMode.None)
572 index = FindStringExact (value);
577 SelectedIndex = index;
582 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
583 public int TopIndex {
584 get { return LBoxInfo.top_item; }
586 if (value == LBoxInfo.top_item)
589 if (value < 0 || value >= Items.Count)
592 LBoxInfo.top_item = value;
598 [DefaultValue (true)]
599 public bool UseTabStops {
600 get { return use_tabstops; }
603 if (use_tabstops == value)
606 use_tabstops = value;
611 #endregion Public Properties
613 #region Private Properties
615 internal ListBoxInfo LBoxInfo {
616 get { return listbox_info; }
619 private int ColumnWidthInternal {
620 get { return column_width_internal; }
621 set { column_width_internal = value; }
624 #endregion Private Properties
626 #region Public Methods
627 protected virtual void AddItemsCore (object[] value)
629 Items.AddRange (value);
632 public void BeginUpdate ()
634 suspend_ctrlupdate = true;
637 public void ClearSelected ()
639 foreach (int i in selected_indices) {
640 UnSelectItem (i, false);
643 selected_indices.ClearIndices ();
644 selected_items.ClearObjects ();
647 protected virtual ObjectCollection CreateItemCollection ()
649 return new ObjectCollection (this);
652 public void EndUpdate ()
654 suspend_ctrlupdate = false;
655 UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
659 public int FindString (String s)
661 return FindString (s, -1);
664 public int FindString (string s, int startIndex)
666 if (Items.Count == 0)
667 return -1; // No exception throwing if empty
669 if (startIndex < -1 || startIndex >= Items.Count - 1)
670 throw new ArgumentOutOfRangeException ("Index of out range");
673 for (int i = startIndex; i < Items.Count; i++) {
674 if ((GetItemText (Items[i])).StartsWith (s))
681 public int FindStringExact (string s)
683 return FindStringExact (s, -1);
686 public int FindStringExact (string s, int startIndex)
688 if (Items.Count == 0)
689 return -1; // No exception throwing if empty
691 if (startIndex < -1 || startIndex >= Items.Count - 1)
692 throw new ArgumentOutOfRangeException ("Index of out range");
695 for (int i = startIndex; i < Items.Count; i++) {
696 if ((GetItemText (Items[i])).Equals (s))
703 public int GetItemHeight (int index)
705 if (index < 0 || index >= Items.Count)
706 throw new ArgumentOutOfRangeException ("Index of out range");
708 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated == true) {
710 if ((Items.GetListBoxItem (index)).ItemHeight != -1) {
711 return (Items.GetListBoxItem (index)).ItemHeight;
714 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
715 OnMeasureItem (args);
716 (Items.GetListBoxItem (index)).ItemHeight = args.ItemHeight;
717 return args.ItemHeight;
723 public Rectangle GetItemRectangle (int index)
725 if (index < 0 || index >= Items.Count)
726 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
728 Rectangle rect = new Rectangle ();
730 if (MultiColumn == false) {
733 rect.Height = GetItemHeight (index);
734 rect.Width = listbox_info.textdrawing_rect.Width;
736 if (DrawMode == DrawMode.OwnerDrawVariable) {
738 if (index >= listbox_info.top_item) {
739 for (int i = listbox_info.top_item; i < index; i++) {
740 rect.Y += GetItemHeight (i);
743 for (int i = index; i < listbox_info.top_item; i++) {
744 rect.Y -= GetItemHeight (i);
748 rect.Y = ItemHeight * (index - listbox_info.top_item);
754 which_page = index / listbox_info.page_size;
755 rect.Y = ((index - listbox_info.top_item) % listbox_info.page_size) * ItemHeight;
756 rect.X = which_page * ColumnWidthInternal;
757 rect.Height = ItemHeight;
758 rect.Width = ColumnWidthInternal;
764 public bool GetSelected (int index)
766 if (index < 0 || index >= Items.Count)
767 throw new ArgumentOutOfRangeException ("Index of out range");
769 return (Items.GetListBoxItem (index)).Selected;
772 public int IndexFromPoint (Point p)
774 return IndexFromPoint (p.X, p.Y);
777 // Only returns visible points
778 public int IndexFromPoint (int x, int y)
780 for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
781 if (GetItemRectangle (i).Contains (x,y) == true)
788 protected override void OnChangeUICues (UICuesEventArgs e)
790 base.OnChangeUICues (e);
793 protected override void OnDataSourceChanged (EventArgs e)
795 base.OnDataSourceChanged (e);
796 BindDataItems (items);
798 if (DataSource == null || DataManager == null) {
802 SelectedIndex = DataManager.Position;
806 protected override void OnDisplayMemberChanged (EventArgs e)
808 base.OnDisplayMemberChanged (e);
810 if (DataManager == null || !IsHandleCreated)
813 BindDataItems (items);
817 protected virtual void OnDrawItem (DrawItemEventArgs e)
820 if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
825 ThemeEngine.Current.DrawListBoxItem (this, e);
828 protected override void OnFontChanged (EventArgs e)
830 base.OnFontChanged (e);
831 if (!use_item_height) {
832 listbox_info.item_height = FontHeight;
833 RellocateScrollBars ();
835 UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
841 protected override void OnHandleCreated (EventArgs e)
843 base.OnHandleCreated (e);
845 UpdateInternalClientRect (ClientRectangle);
847 Controls.AddImplicit (vscrollbar_ctrl);
848 Controls.AddImplicit (hscrollbar_ctrl);
850 UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
853 protected override void OnHandleDestroyed (EventArgs e)
855 base.OnHandleDestroyed (e);
858 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
860 if (draw_mode != DrawMode.OwnerDrawVariable)
863 if (MeasureItem != null)
864 MeasureItem (this, e);
867 protected override void OnParentChanged (EventArgs e)
869 base.OnParentChanged (e);
872 protected override void OnResize (EventArgs e)
875 UpdateInternalClientRect (ClientRectangle);
878 protected override void OnSelectedIndexChanged (EventArgs e)
880 base.OnSelectedIndexChanged (e);
882 if (SelectedIndexChanged != null)
883 SelectedIndexChanged (this, e);
886 protected override void OnSelectedValueChanged (EventArgs e)
888 base.OnSelectedValueChanged (e);
891 public override void Refresh ()
893 if (draw_mode == DrawMode.OwnerDrawVariable) {
894 for (int i = 0; i < Items.Count; i++) {
895 (Items.GetListBoxItem (i)).ItemHeight = -1;
902 protected override void RefreshItem (int index)
904 if (index < 0 || index >= Items.Count)
905 throw new ArgumentOutOfRangeException ("Index of out range");
907 if (draw_mode == DrawMode.OwnerDrawVariable) {
908 (Items.GetListBoxItem (index)).ItemHeight = -1;
912 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
914 base.SetBoundsCore (x, y, width, height, specified);
917 protected override void SetItemCore (int index, object value)
919 if (index < 0 || index >= Items.Count)
922 Items[index] = value;
925 protected override void SetItemsCore (IList value)
930 Items.AddRange (value);
936 public void SetSelected (int index, bool value)
938 if (index < 0 || index >= Items.Count)
939 throw new ArgumentOutOfRangeException ("Index of out range");
941 if (SelectionMode == SelectionMode.None)
942 throw new InvalidOperationException ();
947 UnSelectItem (index, true);
950 protected virtual void Sort ()
952 if (Items.Count == 0)
959 public override string ToString ()
961 return base.ToString () + ", Items Count: " + Items.Count;
964 protected virtual void WmReflectCommand (ref Message m)
969 protected override void WndProc (ref Message m)
971 switch ((Msg) m.Msg) {
974 PaintEventArgs paint_event;
975 paint_event = XplatUI.PaintEventStart (Handle, true);
976 OnPaintLB (paint_event);
977 XplatUI.PaintEventEnd (Handle, true);
981 case Msg.WM_ERASEBKGND:
982 m.Result = (IntPtr) 1;
989 base.WndProc (ref m);
992 #endregion Public Methods
994 #region Private Methods
996 internal void CalcClientArea ()
998 listbox_info.textdrawing_rect = listbox_info.client_rect;
1000 if (listbox_info.show_verticalsb)
1001 listbox_info.textdrawing_rect.Width -= vscrollbar_ctrl.Width;
1003 if (listbox_info.show_horizontalsb)
1004 listbox_info.textdrawing_rect.Height -= hscrollbar_ctrl.Height;
1006 if (DrawMode == DrawMode.OwnerDrawVariable) {
1009 listbox_info.page_size = 0;
1010 for (int i = 0; i < Items.Count; i++) {
1011 height += GetItemHeight (i);
1012 if (height > listbox_info.textdrawing_rect.Height)
1015 listbox_info.page_size++;
1019 listbox_info.page_size = listbox_info.textdrawing_rect.Height / ItemHeight;
1022 if (listbox_info.page_size == 0) {
1023 listbox_info.page_size = 1;
1026 /* Adjust size to visible the maximum number of displayable items */
1027 if (IntegralHeight == true) {
1029 // From MS Docs: The integral height is based on the height of the ListBox, rather than
1030 // the client area height. As a result, when the IntegralHeight property is set true,
1031 // items can still be partially shown if scroll bars are displayed.
1033 int remaining = listbox_info.client_rect.Height % listbox_info.item_height;
1035 if (remaining > 0) {
1036 listbox_info.client_rect.Height -= remaining;
1038 RellocateScrollBars ();
1044 internal void Draw (Rectangle clip, Graphics dc)
1046 // IntegralHeight has effect, we also have to paint the unused area
1047 if (IntegralHeight) {
1048 if (ClientRectangle.Height > listbox_info.client_rect.Height) {
1049 Region area = new Region (ClientRectangle);
1050 area.Exclude (LBoxInfo.textdrawing_rect);
1052 if (listbox_info.show_horizontalsb) {
1053 area.Exclude (new Rectangle (hscrollbar_ctrl.Location.X, hscrollbar_ctrl.Location.Y,
1054 hscrollbar_ctrl.Width, hscrollbar_ctrl.Height));
1057 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
1058 area.GetBounds (dc));
1064 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
1065 LBoxInfo.textdrawing_rect);
1067 if (Items.Count > 0) {
1068 Rectangle item_rect;
1069 DrawItemState state = DrawItemState.None;
1071 for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
1072 item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
1074 if (clip.IntersectsWith (item_rect) == false)
1078 state = DrawItemState.None;
1080 if ((Items.GetListBoxItem (i)).Selected) {
1081 state |= DrawItemState.Selected;
1084 if (has_focus == true && focused_item == i)
1085 state |= DrawItemState.Focus;
1087 if (MultiColumn == false && hscrollbar_ctrl != null && hscrollbar_ctrl.Visible) {
1088 item_rect.X -= hscrollbar_ctrl.Value;
1089 item_rect.Width += hscrollbar_ctrl.Value;
1092 OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect,
1093 i, state, ForeColor, BackColor));
1099 // Converts a GetItemRectangle to a one that we can display
1100 internal Rectangle GetItemDisplayRectangle (int index, int first_displayble)
1102 Rectangle item_rect;
1103 Rectangle first_item_rect = GetItemRectangle (first_displayble);
1104 item_rect = GetItemRectangle (index);
1105 item_rect.X -= first_item_rect.X;
1106 item_rect.Y -= first_item_rect.Y;
1112 private void HorizontalScrollEvent (object sender, EventArgs e)
1119 int top_item = LBoxInfo.top_item;
1120 int last_item = LBoxInfo.last_item;
1122 LBoxInfo.top_item = listbox_info.page_size * hscrollbar_ctrl.Value;
1123 LBoxInfo.last_item = LastVisibleItem ();
1125 if (top_item != LBoxInfo.top_item || last_item != LBoxInfo.last_item)
1129 // Only returns visible points. The diference of with IndexFromPoint is that the rectangle
1130 // has screen coordinates
1131 internal int IndexFromPointDisplayRectangle (int x, int y)
1133 if (Items.Count == 0)
1136 for (int i = LBoxInfo.top_item; i <= LBoxInfo.last_item; i++) {
1137 if (GetItemDisplayRectangle (i, LBoxInfo.top_item).Contains (x, y) == true)
1144 private int LastVisibleItem ()
1146 Rectangle item_rect;
1147 int top_y = LBoxInfo.textdrawing_rect.Y + LBoxInfo.textdrawing_rect.Height;
1150 if (LBoxInfo.top_item >= Items.Count)
1151 return LBoxInfo.top_item;
1153 for (i = LBoxInfo.top_item; i < Items.Count; i++) {
1155 item_rect = GetItemDisplayRectangle (i, LBoxInfo.top_item);
1158 if (item_rect.X > LBoxInfo.textdrawing_rect.Width)
1162 if (item_rect.Y + item_rect.Height > top_y) {
1170 private void UpdatedTopItem ()
1173 int col = LBoxInfo.top_item / LBoxInfo.page_size;
1175 if (col > hscrollbar_ctrl.Maximum)
1176 hscrollbar_ctrl.Value = hscrollbar_ctrl.Maximum;
1178 hscrollbar_ctrl.Value = col;
1181 if (LBoxInfo.top_item > vscrollbar_ctrl.Maximum)
1182 vscrollbar_ctrl.Value = vscrollbar_ctrl.Maximum;
1184 vscrollbar_ctrl.Value = LBoxInfo.top_item;
1188 // Navigates to the indicated item and returns the new item
1189 private int NavigateItemVisually (ItemNavigation navigation)
1191 int page_size, columns, selected_index = -1;
1194 columns = LBoxInfo.textdrawing_rect.Width / ColumnWidthInternal;
1195 page_size = columns * LBoxInfo.page_size;
1196 if (page_size == 0) {
1197 page_size = LBoxInfo.page_size;
1200 page_size = LBoxInfo.page_size;
1203 switch (navigation) {
1205 case ItemNavigation.PreviousColumn: {
1206 if (focused_item - LBoxInfo.page_size < 0) {
1210 if (focused_item - LBoxInfo.page_size < LBoxInfo.top_item) {
1211 LBoxInfo.top_item = focused_item - LBoxInfo.page_size;
1215 selected_index = focused_item - LBoxInfo.page_size;
1219 case ItemNavigation.NextColumn: {
1220 if (focused_item + LBoxInfo.page_size >= Items.Count) {
1224 if (focused_item + LBoxInfo.page_size > LBoxInfo.last_item) {
1225 LBoxInfo.top_item = focused_item;
1229 selected_index = focused_item + LBoxInfo.page_size;
1233 case ItemNavigation.First: {
1234 LBoxInfo.top_item = 0;
1240 case ItemNavigation.Last: {
1242 if (Items.Count < LBoxInfo.page_size) {
1243 LBoxInfo.top_item = 0;
1244 selected_index = Items.Count - 1;
1247 LBoxInfo.top_item = Items.Count - LBoxInfo.page_size;
1248 selected_index = Items.Count - 1;
1254 case ItemNavigation.Next: {
1255 if (focused_item + 1 < Items.Count) {
1256 int actualHeight = 0;
1257 if (draw_mode == DrawMode.OwnerDrawVariable) {
1258 for (int i = LBoxInfo.top_item; i <= focused_item + 1; i++)
1259 actualHeight += GetItemHeight (i);
1261 actualHeight = ((focused_item + 1) - LBoxInfo.top_item + 1) * ItemHeight;
1263 if (actualHeight >= LBoxInfo.textdrawing_rect.Height) {
1264 int bal = IntegralHeight ? 0 : (listbox_info.textdrawing_rect.Height == actualHeight ? 0 : 1);
1265 if (focused_item + bal >= LBoxInfo.last_item) {
1266 LBoxInfo.top_item++;
1270 selected_index = focused_item + 1;
1275 case ItemNavigation.Previous: {
1276 if (focused_item > 0) {
1277 if (focused_item - 1 < LBoxInfo.top_item) {
1278 LBoxInfo.top_item--;
1281 selected_index = focused_item - 1;
1286 case ItemNavigation.NextPage: {
1287 if (Items.Count < page_size) {
1288 NavigateItemVisually (ItemNavigation.Last);
1292 if (focused_item + page_size - 1 >= Items.Count) {
1293 LBoxInfo.top_item = Items.Count - page_size;
1295 selected_index = Items.Count - 1;
1298 if (focused_item + page_size - 1 > LBoxInfo.last_item) {
1299 LBoxInfo.top_item = focused_item;
1303 selected_index = focused_item + page_size - 1;
1309 case ItemNavigation.PreviousPage: {
1311 if (focused_item - (LBoxInfo.page_size - 1) <= 0) {
1313 LBoxInfo.top_item = 0;
1318 if (focused_item - (LBoxInfo.page_size - 1) < LBoxInfo.top_item) {
1319 LBoxInfo.top_item = focused_item - (LBoxInfo.page_size - 1);
1323 selected_index = focused_item - (LBoxInfo.page_size - 1);
1332 return selected_index;
1336 private void OnGotFocus (object sender, EventArgs e)
1340 if (focused_item != -1) {
1341 Rectangle invalidate = GetItemDisplayRectangle (focused_item, LBoxInfo.top_item);
1342 Invalidate (invalidate);
1346 private void OnLostFocus (object sender, EventArgs e)
1350 if (focused_item != -1) {
1351 Rectangle invalidate = GetItemDisplayRectangle (focused_item, LBoxInfo.top_item);
1352 Invalidate (invalidate);
1356 private void OnKeyDownLB (object sender, KeyEventArgs e)
1360 if (Items.Count == 0)
1363 switch (e.KeyCode) {
1365 case Keys.ControlKey:
1366 ctrl_pressed = true;
1370 shift_pressed = true;
1374 new_item = NavigateItemVisually (ItemNavigation.First);
1378 new_item = NavigateItemVisually (ItemNavigation.Last);
1382 new_item = NavigateItemVisually (ItemNavigation.Previous);
1386 new_item = NavigateItemVisually (ItemNavigation.Next);
1390 new_item = NavigateItemVisually (ItemNavigation.PreviousPage);
1394 new_item = NavigateItemVisually (ItemNavigation.NextPage);
1398 if (multicolumn == true) {
1399 new_item = NavigateItemVisually (ItemNavigation.NextColumn);
1404 if (multicolumn == true) {
1405 new_item = NavigateItemVisually (ItemNavigation.PreviousColumn);
1410 if (selection_mode == SelectionMode.MultiSimple) {
1411 SelectedItemFromNavigation (focused_item);
1420 if (new_item != -1) {
1421 SetFocusedItem (new_item);
1424 if (new_item != -1) {
1425 if (selection_mode != SelectionMode.MultiSimple && selection_mode != SelectionMode.None) {
1426 SelectedItemFromNavigation (new_item);
1431 private void OnKeyUpLB (object sender, KeyEventArgs e)
1433 switch (e.KeyCode) {
1434 case Keys.ControlKey:
1435 ctrl_pressed = false;
1438 shift_pressed = false;
1445 internal virtual void OnMouseDownLB (object sender, MouseEventArgs e)
1447 if (Click != null) {
1448 if (e.Button == MouseButtons.Left) {
1453 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
1456 SelectedItemFromNavigation (index);
1457 SetFocusedItem (index);
1461 private void OnPaintLB (PaintEventArgs pevent)
1464 Paint (this, pevent);
1466 if (suspend_ctrlupdate == true)
1469 Draw (pevent.ClipRectangle, pevent.Graphics);
1472 internal void RellocateScrollBars ()
1474 if (listbox_info.show_verticalsb) {
1475 vscrollbar_ctrl.Size = new Size (vscrollbar_ctrl.Width, ClientRectangle.Height);
1476 vscrollbar_ctrl.Location = new Point (ClientRectangle.Width - vscrollbar_ctrl.Width, 0);
1479 if (listbox_info.show_horizontalsb) {
1480 int width = listbox_info.client_rect.Width;
1482 if (listbox_info.show_verticalsb)
1483 width -= vscrollbar_ctrl.Width;
1485 hscrollbar_ctrl.Size = new Size (width, hscrollbar_ctrl.Height);
1486 hscrollbar_ctrl.Location = new Point (0, ClientRectangle.Height - hscrollbar_ctrl.Height);
1492 // Add an item in the Selection array and marks it visually as selected
1493 private void SelectItem (int index)
1498 Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
1499 (Items.GetListBoxItem (index)).Selected = true;
1500 selected_indices.AddIndex (index);
1501 selected_items.AddObject (Items[index]);
1503 if (ClientRectangle.Contains (invalidate))
1504 Invalidate (invalidate);
1508 // An item navigation operation (mouse or keyboard) has caused to select a new item
1509 internal void SelectedItemFromNavigation (int index)
1511 switch (SelectionMode) {
1512 case SelectionMode.None: // Do nothing
1514 case SelectionMode.One: {
1515 SelectedIndex = index;
1518 case SelectionMode.MultiSimple: {
1519 if (selected_index == -1) {
1520 SelectedIndex = index;
1523 if ((Items.GetListBoxItem (index)).Selected) // BUG: index or selected_index?
1524 UnSelectItem (index, true);
1527 OnSelectedIndexChanged (new EventArgs ());
1528 OnSelectedValueChanged (new EventArgs ());
1534 case SelectionMode.MultiExtended: {
1535 if (selected_index == -1) {
1536 SelectedIndex = index;
1539 if (ctrl_pressed == false && shift_pressed == false) {
1543 if (shift_pressed == true) {
1544 ShiftSelection (index);
1545 } else { // ctrl_pressed or single item
1549 OnSelectedIndexChanged (new EventArgs ());
1550 OnSelectedValueChanged (new EventArgs ());
1560 private void ShiftSelection (int index)
1562 int shorter_item = -1, dist = Items.Count + 1, cur_dist;
1564 foreach (int idx in selected_indices) {
1566 cur_dist = idx - index;
1569 cur_dist = index - idx;
1572 if (cur_dist < dist) {
1578 if (shorter_item != -1) {
1581 if (shorter_item > index) {
1585 start = shorter_item;
1590 for (int idx = start; idx <= end; idx++) {
1596 internal void SetFocusedItem (int index)
1598 Rectangle invalidate;
1599 int prev = focused_item;
1601 focused_item = index;
1603 if (has_focus == false)
1606 if (prev != -1) { // Invalidates previous item
1607 invalidate = GetItemDisplayRectangle (prev, LBoxInfo.top_item);
1608 Invalidate (invalidate);
1612 invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
1613 Invalidate (invalidate);
1617 // Removes an item in the Selection array and marks it visually as unselected
1618 private void UnSelectItem (int index, bool remove)
1623 Rectangle invalidate = GetItemDisplayRectangle (index, LBoxInfo.top_item);
1624 (Items.GetListBoxItem (index)).Selected = false;
1627 selected_indices.RemoveIndex (index);
1628 selected_items.RemoveObject (Items[index]);
1631 if (ClientRectangle.Contains (invalidate))
1632 Invalidate (invalidate);
1635 internal StringFormat GetFormatString ()
1637 StringFormat string_format = new StringFormat ();
1639 if (RightToLeft == RightToLeft.Yes)
1640 string_format.Alignment = StringAlignment.Far;
1642 string_format.Alignment = StringAlignment.Near;
1645 string_format.SetTabStops (0, new float [] {(float)(Font.Height * 3.7)});
1647 return string_format;
1650 // Updates the scrollbar's position with the new items and inside area
1651 internal virtual void UpdateItemInfo (UpdateOperation operation, int first, int last)
1653 if (!IsHandleCreated || suspend_ctrlupdate == true)
1656 UpdateShowVerticalScrollBar ();
1658 if (listbox_info.show_verticalsb && Items.Count > listbox_info.page_size)
1659 if (vscrollbar_ctrl.Enabled)
1660 vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
1662 if (listbox_info.show_horizontalsb) {
1664 int fullpage = (listbox_info.page_size * (listbox_info.client_rect.Width / ColumnWidthInternal));
1666 if (hscrollbar_ctrl.Enabled && listbox_info.page_size > 0)
1667 hscrollbar_ctrl.Maximum = Math.Max (0, 1 + ((Items.Count - fullpage) / listbox_info.page_size));
1671 if (MultiColumn == false) {
1672 /* Calc the longest items for non multicolumn listboxes */
1673 if (operation == UpdateOperation.AllItems || operation == UpdateOperation.DeleteItems) {
1676 for (int i = 0; i < Items.Count; i++) {
1677 size = DeviceContext.MeasureString (GetItemText (Items[i]), Font);
1679 if ((int) size.Width > listbox_info.max_itemwidth)
1680 listbox_info.max_itemwidth = (int) size.Width;
1684 if (operation == UpdateOperation.AddItems) {
1687 for (int i = first; i < last + 1; i++) {
1688 size = DeviceContext.MeasureString (GetItemText (Items[i]), Font);
1690 if ((int) size.Width > listbox_info.max_itemwidth)
1691 listbox_info.max_itemwidth = (int) size.Width;
1700 if (Items.Count == 0) {
1701 selected_index = -1;
1705 SelectedItems.ReCreate ();
1706 SelectedIndices.ReCreate ();
1707 UpdateShowHorizontalScrollBar ();
1708 LBoxInfo.last_item = LastVisibleItem ();
1712 private void UpdateInternalClientRect (Rectangle client_rectangle)
1714 listbox_info.client_rect = client_rectangle;
1715 UpdateShowHorizontalScrollBar ();
1716 UpdateShowVerticalScrollBar ();
1717 RellocateScrollBars ();
1718 UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
1721 /* Determines if the horizontal scrollbar has to be displyed */
1722 private void UpdateShowHorizontalScrollBar ()
1725 bool enabled = true;
1727 if (MultiColumn) { /* Horizontal scrollbar is always shown in Multicolum mode */
1729 /* Is it really need it */
1730 int page_size = listbox_info.client_rect.Height / listbox_info.item_height;
1731 int fullpage = (page_size * (listbox_info.textdrawing_rect.Height / ColumnWidthInternal));
1733 if (Items.Count > fullpage) {
1736 else { /* Acording to MS Documentation ScrollAlwaysVisible only affects Horizontal scrollbars but
1737 this is not true for MultiColumn listboxes */
1738 if (ScrollAlwaysVisible == true) {
1744 } else { /* If large item*/
1746 if (listbox_info.max_itemwidth > listbox_info.client_rect.Width && HorizontalScrollbar) {
1748 hscrollbar_ctrl.Maximum = listbox_info.max_itemwidth;
1749 hscrollbar_ctrl.LargeChange = listbox_info.textdrawing_rect.Width;
1753 if (hscrollbar_ctrl.Enabled != enabled)
1754 hscrollbar_ctrl.Enabled = enabled;
1756 if (listbox_info.show_horizontalsb == show)
1759 listbox_info.show_horizontalsb = show;
1760 hscrollbar_ctrl.Visible = show;
1763 RellocateScrollBars ();
1769 /* Determines if the vertical scrollbar has to be displyed */
1770 private void UpdateShowVerticalScrollBar ()
1773 bool enabled = true;
1775 if (!MultiColumn) { /* Vertical scrollbar is never shown in Multicolum mode */
1776 if (Items.Count > listbox_info.page_size) {
1780 if (ScrollAlwaysVisible) {
1786 if (vscrollbar_ctrl.Enabled != enabled)
1787 vscrollbar_ctrl.Enabled = enabled;
1789 if (listbox_info.show_verticalsb == show)
1792 listbox_info.show_verticalsb = show;
1793 vscrollbar_ctrl.Visible = show;
1796 if (vscrollbar_ctrl.Enabled)
1797 vscrollbar_ctrl.Maximum = Items.Count - listbox_info.page_size;
1799 RellocateScrollBars ();
1800 } else if (vscrollbar_ctrl.Maximum > 0) {
1801 vscrollbar_ctrl.Maximum = 0;
1808 private void VerticalScrollEvent (object sender, EventArgs e)
1810 int top_item = LBoxInfo.top_item;
1811 int last_item = LBoxInfo.last_item;
1813 LBoxInfo.top_item = /*listbox_info.page_size + */ vscrollbar_ctrl.Value;
1814 LBoxInfo.last_item = LastVisibleItem ();
1816 if (top_item != LBoxInfo.top_item || last_item != LBoxInfo.last_item)
1820 #endregion Private Methods
1823 ListBox.ObjectCollection
1825 [ListBindable (false)]
1826 public class ObjectCollection : IList, ICollection, IEnumerable
1829 internal class ListObjectComparer : IComparer
1831 private ListBox owner;
1833 public ListObjectComparer (ListBox owner)
1838 public int Compare (object a, object b)
1840 string str1 = a.ToString ();
1841 string str2 = b.ToString ();
1842 return str1.CompareTo (str2);
1847 internal class ListItemComparer : IComparer
1849 private ListBox owner;
1851 public ListItemComparer (ListBox owner)
1856 public int Compare (object a, object b)
1858 int index1 = ((ListBox.ListBoxItem) (a)).Index;
1859 int index2 = ((ListBox.ListBoxItem) (b)).Index;
1860 string str1 = owner.GetItemText (owner.Items[index1]);
1861 string str2 = owner.GetItemText (owner.Items[index2]);
1862 return str1.CompareTo (str2);
1866 private ListBox owner;
1867 internal ArrayList object_items = new ArrayList ();
1868 internal ArrayList listbox_items = new ArrayList ();
1870 public ObjectCollection (ListBox owner)
1875 public ObjectCollection (ListBox owner, object[] obj)
1881 public ObjectCollection (ListBox owner, ObjectCollection obj)
1887 #region Public Properties
1888 public virtual int Count {
1889 get { return object_items.Count; }
1892 public virtual bool IsReadOnly {
1893 get { return false; }
1897 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1898 public virtual object this [int index] {
1900 if (index < 0 || index >= Count)
1901 throw new ArgumentOutOfRangeException ("Index of out range");
1903 return object_items[index];
1906 if (index < 0 || index >= Count)
1907 throw new ArgumentOutOfRangeException ("Index of out range");
1909 object_items[index] = value;
1913 bool ICollection.IsSynchronized {
1914 get { return false; }
1917 object ICollection.SyncRoot {
1918 get { return this; }
1921 bool IList.IsFixedSize {
1922 get { return false; }
1925 #endregion Public Properties
1927 #region Private Properties
1928 internal ArrayList ObjectItems {
1929 get { return object_items;}
1931 object_items = value;
1935 internal ArrayList ListBoxItems {
1936 get { return listbox_items;}
1938 listbox_items = value;
1941 #endregion Private Properties
1943 #region Public Methods
1944 public int Add (object item)
1948 idx = AddItem (item);
1949 owner.UpdateItemInfo (UpdateOperation.AddItems, idx, idx);
1953 public void AddRange (object[] items)
1957 foreach (object mi in items)
1960 owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
1963 public void AddRange (ObjectCollection col)
1967 foreach (object mi in col)
1970 owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
1973 internal void AddRange (IList list)
1977 foreach (object mi in list)
1980 owner.UpdateItemInfo (UpdateOperation.AddItems, cnt, Count - 1);
1983 public virtual void Clear ()
1985 owner.selected_index = -1;
1986 owner.focused_item = -1;
1987 object_items.Clear ();
1988 listbox_items.Clear ();
1989 owner.UpdateItemInfo (UpdateOperation.AllItems, 0, 0);
1991 public virtual bool Contains (object obj)
1993 return object_items.Contains (obj);
1996 public void CopyTo (object[] dest, int arrayIndex)
1998 object_items.CopyTo (dest, arrayIndex);
2001 void ICollection.CopyTo (Array dest, int index)
2003 object_items.CopyTo (dest, index);
2006 public virtual IEnumerator GetEnumerator ()
2008 return object_items.GetEnumerator ();
2011 int IList.Add (object item)
2016 public virtual int IndexOf (object value)
2018 return object_items.IndexOf (value);
2021 public virtual void Insert (int index, object item)
2023 if (index < 0 || index > Count)
2024 throw new ArgumentOutOfRangeException ("Index of out range");
2027 ObjectCollection new_items = new ObjectCollection (owner);
2029 owner.BeginUpdate ();
2031 for (int i = 0; i < index; i++) {
2032 idx = new_items.AddItem (ObjectItems[i]);
2033 (new_items.GetListBoxItem (idx)).CopyState (GetListBoxItem (i));
2036 new_items.AddItem (item);
2038 for (int i = index; i < Count; i++){
2039 idx = new_items.AddItem (ObjectItems[i]);
2040 (new_items.GetListBoxItem (idx)).CopyState (GetListBoxItem (i));
2043 ObjectItems = new_items.ObjectItems;
2044 ListBoxItems = new_items.ListBoxItems;
2046 owner.EndUpdate (); // Calls UpdateItemInfo
2049 public virtual void Remove (object value)
2051 RemoveAt (IndexOf (value));
2054 public virtual void RemoveAt (int index)
2056 if (index < 0 || index >= Count)
2057 throw new ArgumentOutOfRangeException ("Index of out range");
2059 object_items.RemoveAt (index);
2060 listbox_items.RemoveAt (index);
2061 owner.UpdateItemInfo (UpdateOperation.DeleteItems, index, index);
2063 #endregion Public Methods
2065 #region Private Methods
2066 internal int AddItem (object item)
2068 int cnt = object_items.Count;
2069 object_items.Add (item);
2070 listbox_items.Add (new ListBox.ListBoxItem (cnt));
2074 internal ListBox.ListBoxItem GetListBoxItem (int index)
2076 if (index < 0 || index >= Count)
2077 throw new ArgumentOutOfRangeException ("Index of out range");
2079 return (ListBox.ListBoxItem) listbox_items[index];
2082 internal void SetListBoxItem (ListBox.ListBoxItem item, int index)
2084 if (index < 0 || index >= Count)
2085 throw new ArgumentOutOfRangeException ("Index of out range");
2087 listbox_items[index] = item;
2090 internal void Sort ()
2092 /* Keep this order */
2093 listbox_items.Sort (new ListItemComparer (owner));
2094 object_items.Sort (new ListObjectComparer (owner));
2096 for (int i = 0; i < listbox_items.Count; i++) {
2097 ListBox.ListBoxItem item = GetListBoxItem (i);
2102 #endregion Private Methods
2106 ListBox.SelectedIndexCollection
2108 public class SelectedIndexCollection : IList, ICollection, IEnumerable
2110 private ListBox owner;
2111 private ArrayList indices = new ArrayList ();
2113 public SelectedIndexCollection (ListBox owner)
2118 #region Public Properties
2120 public virtual int Count {
2121 get { return indices.Count; }
2124 public virtual bool IsReadOnly {
2125 get { return true; }
2128 public int this [int index] {
2130 if (index < 0 || index >= Count)
2131 throw new ArgumentOutOfRangeException ("Index of out range");
2133 return (int) indices[index];
2137 bool ICollection.IsSynchronized {
2138 get { return true; }
2141 bool IList.IsFixedSize{
2142 get { return true; }
2145 object ICollection.SyncRoot {
2146 get { return this; }
2149 #endregion Public Properties
2151 #region Public Methods
2152 public bool Contains (int selectedIndex)
2154 return indices.Contains (selectedIndex);
2157 public virtual void CopyTo (Array dest, int index)
2159 indices.CopyTo (dest, index);
2162 public virtual IEnumerator GetEnumerator ()
2164 return indices.GetEnumerator ();
2167 int IList.Add (object obj)
2169 throw new NotSupportedException ();
2174 throw new NotSupportedException ();
2177 bool IList.Contains (object selectedIndex)
2179 return Contains ((int)selectedIndex);
2182 int IList.IndexOf (object selectedIndex)
2184 return IndexOf ((int) selectedIndex);
2187 void IList.Insert (int index, object value)
2189 throw new NotSupportedException ();
2192 void IList.Remove (object value)
2194 throw new NotSupportedException ();
2197 void IList.RemoveAt (int index)
2199 throw new NotSupportedException ();
2202 object IList.this[int index]{
2203 get {return indices[index]; }
2204 set {throw new NotImplementedException (); }
2207 public int IndexOf (int selectedIndex)
2209 return indices.IndexOf (selectedIndex);
2211 #endregion Public Methods
2213 #region Private Methods
2215 internal void AddIndex (int index)
2217 indices.Add (index);
2220 internal void ClearIndices ()
2225 internal void RemoveIndex (int index)
2227 indices.Remove (index);
2230 internal void ReCreate ()
2234 for (int i = 0; i < owner.Items.Count; i++) {
2235 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
2238 indices.Add (item.Index);
2242 #endregion Private Methods
2246 SelectedObjectCollection
2248 public class SelectedObjectCollection : IList, ICollection, IEnumerable
2250 private ListBox owner;
2251 private ArrayList object_items = new ArrayList ();
2253 public SelectedObjectCollection (ListBox owner)
2258 #region Public Properties
2259 public virtual int Count {
2260 get { return object_items.Count; }
2263 public virtual bool IsReadOnly {
2264 get { return true; }
2268 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
2269 public virtual object this [int index] {
2271 if (index < 0 || index >= Count)
2272 throw new ArgumentOutOfRangeException ("Index of out range");
2274 return object_items[index];
2276 set {throw new NotSupportedException ();}
2279 bool ICollection.IsSynchronized {
2280 get { return true; }
2283 object ICollection.SyncRoot {
2284 get { return this; }
2287 bool IList.IsFixedSize {
2288 get { return true; }
2291 object IList.this[int index] {
2292 get { return object_items[index]; }
2293 set { throw new NotSupportedException (); }
2296 #endregion Public Properties
2298 #region Public Methods
2299 public virtual bool Contains (object selectedObject)
2301 return object_items.Contains (selectedObject);
2304 public virtual void CopyTo (Array dest, int index)
2306 object_items.CopyTo (dest, index);
2309 int IList.Add (object value)
2311 throw new NotSupportedException ();
2316 throw new NotSupportedException ();
2319 bool IList.Contains (object selectedIndex)
2321 throw new NotImplementedException ();
2324 void IList.Insert (int index, object value)
2326 throw new NotSupportedException ();
2329 void IList.Remove (object value)
2331 throw new NotSupportedException ();
2334 void IList.RemoveAt (int index)
2336 throw new NotSupportedException ();
2339 public int IndexOf (object item)
2341 return object_items.IndexOf (item);
2344 public virtual IEnumerator GetEnumerator ()
2346 return object_items.GetEnumerator ();
2349 #endregion Public Methods
2351 #region Private Methods
2352 internal void AddObject (object obj)
2354 object_items.Add (obj);
2357 internal void ClearObjects ()
2359 object_items.Clear ();
2362 internal void ReCreate ()
2364 object_items.Clear ();
2366 for (int i = 0; i < owner.Items.Count; i++) {
2367 ListBox.ListBoxItem item = owner.Items.GetListBoxItem (i);
2370 object_items.Add (owner.Items[item.Index]);
2374 internal void RemoveObject (object obj)
2376 object_items.Remove (obj);
2379 #endregion Private Methods