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-2006 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Mike Kestner <mkestner@novell.com>
25 // Daniel Nauck (dna(at)mono-project(dot)de)
28 using System.Collections;
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.ComponentModel.Design.Serialization;
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.InteropServices;
36 using System.Diagnostics;
38 namespace System.Windows.Forms
40 [DefaultProperty("Items")]
41 [DefaultEvent("SelectedIndexChanged")]
42 [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [DefaultBindingProperty ("Text")]
44 [ClassInterface (ClassInterfaceType.AutoDispatch)]
46 public class ComboBox : ListControl
48 private DrawMode draw_mode = DrawMode.Normal;
49 private ComboBoxStyle dropdown_style;
50 private int dropdown_width = -1;
51 private int selected_index = -1;
52 private ObjectCollection items;
53 private bool suspend_ctrlupdate;
54 private int maxdrop_items = 8;
55 private bool integral_height = true;
57 private int max_length;
58 private ComboListBox listbox_ctrl;
59 private ComboTextBox textbox_ctrl;
60 private bool process_textchanged_event = true;
61 private bool process_texchanged_autoscroll = true;
62 private bool item_height_specified;
63 private int item_height;
64 private int requested_height = -1;
65 private Hashtable item_heights;
66 private bool show_dropdown_button;
67 private ButtonState button_state = ButtonState.Normal;
68 private bool dropped_down;
69 private Rectangle text_area;
70 private Rectangle button_area;
71 private Rectangle listbox_area;
72 private const int button_width = 16;
73 bool drop_down_button_entered;
74 private AutoCompleteStringCollection auto_complete_custom_source = null;
75 private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
76 private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
77 private FlatStyle flat_style;
78 private int drop_down_height;
79 const int default_drop_down_height = 106;
82 public class ChildAccessibleObject : AccessibleObject {
84 public ChildAccessibleObject (ComboBox owner, IntPtr handle)
89 public override string Name {
98 items = new ObjectCollection (this);
99 DropDownStyle = ComboBoxStyle.DropDown;
100 item_height = FontHeight + 2;
101 background_color = ThemeEngine.Current.ColorWindow;
102 border_style = BorderStyle.None;
104 drop_down_height = default_drop_down_height;
105 flat_style = FlatStyle.Standard;
108 MouseDown += new MouseEventHandler (OnMouseDownCB);
109 MouseUp += new MouseEventHandler (OnMouseUpCB);
110 MouseMove += new MouseEventHandler (OnMouseMoveCB);
111 MouseWheel += new MouseEventHandler (OnMouseWheelCB);
112 MouseEnter += new EventHandler (OnMouseEnter);
113 MouseLeave += new EventHandler (OnMouseLeave);
114 KeyDown +=new KeyEventHandler(OnKeyDownCB);
120 [EditorBrowsable (EditorBrowsableState.Never)]
121 public new event EventHandler BackgroundImageChanged {
122 add { base.BackgroundImageChanged += value; }
123 remove { base.BackgroundImageChanged -= value; }
127 [EditorBrowsable (EditorBrowsableState.Never)]
128 public new event EventHandler BackgroundImageLayoutChanged
130 add { base.BackgroundImageLayoutChanged += value; }
131 remove { base.BackgroundImageLayoutChanged -= value; }
135 [EditorBrowsable (EditorBrowsableState.Never)]
136 public new event EventHandler DoubleClick
138 add { base.DoubleClick += value; }
139 remove { base.DoubleClick -= value; }
142 static object DrawItemEvent = new object ();
143 static object DropDownEvent = new object ();
144 static object DropDownStyleChangedEvent = new object ();
145 static object MeasureItemEvent = new object ();
146 static object SelectedIndexChangedEvent = new object ();
147 static object SelectionChangeCommittedEvent = new object ();
148 static object DropDownClosedEvent = new object ();
149 static object TextUpdateEvent = new object ();
151 public event DrawItemEventHandler DrawItem {
152 add { Events.AddHandler (DrawItemEvent, value); }
153 remove { Events.RemoveHandler (DrawItemEvent, value); }
156 public event EventHandler DropDown {
157 add { Events.AddHandler (DropDownEvent, value); }
158 remove { Events.RemoveHandler (DropDownEvent, value); }
160 public event EventHandler DropDownClosed
162 add { Events.AddHandler (DropDownClosedEvent, value); }
163 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
166 public event EventHandler DropDownStyleChanged {
167 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
168 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
171 public event MeasureItemEventHandler MeasureItem {
172 add { Events.AddHandler (MeasureItemEvent, value); }
173 remove { Events.RemoveHandler (MeasureItemEvent, value); }
176 [EditorBrowsable (EditorBrowsableState.Never)]
177 public new event EventHandler PaddingChanged
179 add { base.PaddingChanged += value; }
180 remove { base.PaddingChanged -= value; }
184 [EditorBrowsable (EditorBrowsableState.Never)]
185 public new event PaintEventHandler Paint {
186 add { base.Paint += value; }
187 remove { base.Paint -= value; }
190 public event EventHandler SelectedIndexChanged {
191 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
192 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
195 public event EventHandler SelectionChangeCommitted {
196 add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
197 remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
199 public event EventHandler TextUpdate
201 add { Events.AddHandler (TextUpdateEvent, value); }
202 remove { Events.RemoveHandler (TextUpdateEvent, value); }
207 #region Public Properties
208 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
209 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
211 [EditorBrowsable (EditorBrowsableState.Always)]
213 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
214 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
215 public AutoCompleteStringCollection AutoCompleteCustomSource {
217 if(auto_complete_custom_source == null) {
218 auto_complete_custom_source = new AutoCompleteStringCollection ();
219 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
221 return auto_complete_custom_source;
224 if(auto_complete_custom_source == value)
227 if(auto_complete_custom_source != null) //remove eventhandler from old collection
228 auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
230 auto_complete_custom_source = value;
232 if(auto_complete_custom_source != null)
233 auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
235 SetTextBoxAutoCompleteData ();
239 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
241 [EditorBrowsable (EditorBrowsableState.Always)]
242 [DefaultValue (AutoCompleteMode.None)]
243 public AutoCompleteMode AutoCompleteMode {
244 get { return auto_complete_mode; }
246 if(auto_complete_mode == value)
249 if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
250 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
252 auto_complete_mode = value;
253 SetTextBoxAutoCompleteData ();
257 [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
259 [EditorBrowsable (EditorBrowsableState.Always)]
260 [DefaultValue (AutoCompleteSource.None)]
261 public AutoCompleteSource AutoCompleteSource {
262 get { return auto_complete_source; }
264 if(auto_complete_source == value)
267 if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
268 throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
270 auto_complete_source = value;
271 SetTextBoxAutoCompleteData ();
275 void SetTextBoxAutoCompleteData ()
277 if (textbox_ctrl == null)
280 textbox_ctrl.AutoCompleteMode = auto_complete_mode;
282 if (auto_complete_source == AutoCompleteSource.ListItems) {
283 textbox_ctrl.AutoCompleteSource = AutoCompleteSource.CustomSource;
284 textbox_ctrl.AutoCompleteCustomSource = null;
285 textbox_ctrl.AutoCompleteInternalSource = this;
287 textbox_ctrl.AutoCompleteSource = auto_complete_source;
288 textbox_ctrl.AutoCompleteCustomSource = auto_complete_custom_source;
289 textbox_ctrl.AutoCompleteInternalSource = null;
292 public override Color BackColor {
293 get { return base.BackColor; }
295 if (base.BackColor == value)
297 base.BackColor = value;
303 [EditorBrowsable (EditorBrowsableState.Never)]
304 public override Image BackgroundImage {
305 get { return base.BackgroundImage; }
307 if (base.BackgroundImage == value)
309 base.BackgroundImage = value;
315 [EditorBrowsable (EditorBrowsableState.Never)]
316 public override ImageLayout BackgroundImageLayout {
317 get { return base.BackgroundImageLayout; }
318 set { base.BackgroundImageLayout = value; }
321 protected override CreateParams CreateParams {
322 get { return base.CreateParams;}
325 [DefaultValue ((string)null)]
326 [AttributeProvider (typeof (IListSource))]
327 [RefreshProperties (RefreshProperties.Repaint)]
328 [MWFCategory("Data")]
329 public new object DataSource {
330 get { return base.DataSource; }
331 set { base.DataSource = value; }
334 protected override Size DefaultSize {
335 get { return new Size (121, 21); }
338 [RefreshProperties(RefreshProperties.Repaint)]
339 [DefaultValue (DrawMode.Normal)]
340 [MWFCategory("Behavior")]
341 public DrawMode DrawMode {
342 get { return draw_mode; }
344 if (!Enum.IsDefined (typeof (DrawMode), value))
345 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
347 if (draw_mode == value)
350 if (draw_mode == DrawMode.OwnerDrawVariable)
353 if (draw_mode == DrawMode.OwnerDrawVariable)
354 item_heights = new Hashtable ();
361 [EditorBrowsable (EditorBrowsableState.Always)]
362 [MWFCategory("Behavior")]
363 public int DropDownHeight {
365 return drop_down_height;
369 throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
371 if (value == drop_down_height)
374 drop_down_height = value;
375 IntegralHeight = false;
379 [DefaultValue (ComboBoxStyle.DropDown)]
380 [RefreshProperties(RefreshProperties.Repaint)]
381 [MWFCategory("Appearance")]
382 public ComboBoxStyle DropDownStyle {
383 get { return dropdown_style; }
385 if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
386 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
388 if (dropdown_style == value)
393 if (dropdown_style == ComboBoxStyle.Simple) {
394 if (listbox_ctrl != null) {
395 Controls.RemoveImplicit (listbox_ctrl);
396 listbox_ctrl.Dispose ();
401 dropdown_style = value;
403 if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
404 Controls.RemoveImplicit (textbox_ctrl);
405 textbox_ctrl.Dispose ();
409 if (dropdown_style == ComboBoxStyle.Simple) {
410 show_dropdown_button = false;
412 CreateComboListBox ();
413 Controls.AddImplicit (listbox_ctrl);
414 listbox_ctrl.Visible = true;
416 // This should give us a 150 default height
417 // for Simple mode if size hasn't been set
418 // (DefaultSize doesn't work for us in this case)
419 if (requested_height == -1)
420 requested_height = 150;
422 show_dropdown_button = true;
423 button_state = ButtonState.Normal;
426 if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
427 textbox_ctrl = new ComboTextBox (this);
428 object selected_item = SelectedItem;
429 if (selected_item != null)
430 textbox_ctrl.Text = GetItemText (selected_item);
431 textbox_ctrl.BorderStyle = BorderStyle.None;
432 textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
433 textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
434 textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
435 textbox_ctrl.ContextMenu = ContextMenu;
436 textbox_ctrl.TopMargin = 1; // since we don't have borders, adjust manually the top
438 if (IsHandleCreated == true)
439 Controls.AddImplicit (textbox_ctrl);
440 SetTextBoxAutoCompleteData ();
444 OnDropDownStyleChanged (EventArgs.Empty);
447 UpdateComboBoxBounds ();
452 [MWFCategory("Behavior")]
453 public int DropDownWidth {
455 if (dropdown_width == -1)
458 return dropdown_width;
461 if (dropdown_width == value)
465 throw new ArgumentOutOfRangeException ("DropDownWidth",
466 "The DropDownWidth value is less than one.");
468 dropdown_width = value;
473 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
474 public bool DroppedDown {
476 if (dropdown_style == ComboBoxStyle.Simple)
482 if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
488 listbox_ctrl.HideWindow ();
492 [DefaultValue (FlatStyle.Standard)]
494 [MWFCategory("Appearance")]
495 public FlatStyle FlatStyle {
496 get { return flat_style; }
498 if (!Enum.IsDefined (typeof (FlatStyle), value))
499 throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
507 public override bool Focused {
508 get { return base.Focused; }
511 public override Color ForeColor {
512 get { return base.ForeColor; }
514 if (base.ForeColor == value)
516 base.ForeColor = value;
521 [DefaultValue (true)]
523 [MWFCategory("Behavior")]
524 public bool IntegralHeight {
525 get { return integral_height; }
527 if (integral_height == value)
529 integral_height = value;
530 UpdateComboBoxBounds ();
536 [MWFCategory("Behavior")]
537 public int ItemHeight {
539 if (item_height == -1) {
540 SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
541 item_height = (int) sz.Height;
547 throw new ArgumentOutOfRangeException ("ItemHeight",
548 "The item height value is less than one.");
550 item_height_specified = true;
553 UpdateComboBoxBounds ();
559 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
561 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
562 [MergableProperty (false)]
563 [MWFCategory("Data")]
564 public ComboBox.ObjectCollection Items {
565 get { return items; }
570 [MWFCategory("Behavior")]
571 public int MaxDropDownItems {
572 get { return maxdrop_items; }
574 if (maxdrop_items == value)
576 maxdrop_items = value;
580 public override Size MaximumSize {
581 get { return base.MaximumSize; }
583 base.MaximumSize = new Size (value.Width, 0);
589 [MWFCategory("Behavior")]
590 public int MaxLength {
591 get { return max_length; }
593 if (max_length == value)
598 if (dropdown_style != ComboBoxStyle.DropDownList) {
602 textbox_ctrl.MaxLength = value;
607 public override Size MinimumSize {
608 get { return base.MinimumSize; }
610 base.MinimumSize = new Size (value.Width, 0);
614 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
615 [EditorBrowsable (EditorBrowsableState.Never)]
617 public new Padding Padding {
618 get { return base.Padding; }
619 set { base.Padding = value; }
622 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
624 public int PreferredHeight {
625 get { return Font.Height + 8; }
629 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
630 public override int SelectedIndex {
631 get { return selected_index; }
633 SetSelectedIndex (value, false);
638 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
640 public object SelectedItem {
641 get { return selected_index == -1 ? null : Items [selected_index]; }
643 object item = selected_index == -1 ? null : Items [selected_index];
650 SelectedIndex = Items.IndexOf (value);
655 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
656 public string SelectedText {
658 if (dropdown_style == ComboBoxStyle.DropDownList)
661 string retval = textbox_ctrl.SelectedText;
666 if (dropdown_style == ComboBoxStyle.DropDownList)
668 textbox_ctrl.SelectedText = value;
673 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
674 public int SelectionLength {
676 if (dropdown_style == ComboBoxStyle.DropDownList)
679 int result = textbox_ctrl.SelectionLength;
680 return result == -1 ? 0 : result;
683 if (dropdown_style == ComboBoxStyle.DropDownList)
685 if (textbox_ctrl.SelectionLength == value)
687 textbox_ctrl.SelectionLength = value;
692 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
693 public int SelectionStart {
695 if (dropdown_style == ComboBoxStyle.DropDownList)
697 return textbox_ctrl.SelectionStart;
700 if (dropdown_style == ComboBoxStyle.DropDownList)
702 if (textbox_ctrl.SelectionStart == value)
704 textbox_ctrl.SelectionStart = value;
708 [DefaultValue (false)]
709 [MWFCategory("Behavior")]
711 get { return sorted; }
726 public override string Text {
728 if (dropdown_style != ComboBoxStyle.DropDownList) {
729 if (textbox_ctrl != null) {
730 return textbox_ctrl.Text;
734 if (SelectedItem != null)
735 return GetItemText (SelectedItem);
741 if (SelectedIndex == -1) {
742 if (dropdown_style != ComboBoxStyle.DropDownList)
743 SetControlText (string.Empty, false);
750 // don't set the index if value exactly matches text of selected item
751 if (SelectedItem == null || string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) != 0)
753 // find exact match using case-sensitive comparison, and if does
754 // not result in any match then use case-insensitive comparison
755 int index = FindStringExact (value, -1, false);
757 index = FindStringExact (value, -1, true);
760 SelectedIndex = index;
765 // set directly the passed value
766 if (dropdown_style != ComboBoxStyle.DropDownList)
767 textbox_ctrl.Text = value;
771 #endregion Public Properties
773 #region Internal Properties
774 internal Rectangle ButtonArea {
775 get { return button_area; }
778 internal Rectangle TextArea {
779 get { return text_area; }
783 #region UIA Framework Properties
785 internal TextBox UIATextBox {
786 get { return textbox_ctrl; }
789 internal ComboListBox UIAComboListBox {
790 get { return listbox_ctrl; }
793 #endregion UIA Framework Properties
795 #region Public Methods
796 [Obsolete ("This method has been deprecated")]
797 protected virtual void AddItemsCore (object[] value)
802 public void BeginUpdate ()
804 suspend_ctrlupdate = true;
807 protected override AccessibleObject CreateAccessibilityInstance ()
809 return base.CreateAccessibilityInstance ();
812 protected override void CreateHandle ()
814 base.CreateHandle ();
817 protected override void Dispose (bool disposing)
820 if (listbox_ctrl != null) {
821 listbox_ctrl.Dispose ();
822 Controls.RemoveImplicit (listbox_ctrl);
826 if (textbox_ctrl != null) {
827 Controls.RemoveImplicit (textbox_ctrl);
828 textbox_ctrl.Dispose ();
833 base.Dispose (disposing);
836 public void EndUpdate ()
838 suspend_ctrlupdate = false;
843 public int FindString (string s)
845 return FindString (s, -1);
848 public int FindString (string s, int startIndex)
850 if (s == null || Items.Count == 0)
853 if (startIndex < -1 || startIndex >= Items.Count)
854 throw new ArgumentOutOfRangeException ("startIndex");
857 if (i == (Items.Count - 1))
861 if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
863 if (i == (Items.Count - 1))
865 } while (i != startIndex);
870 public int FindStringExact (string s)
872 return FindStringExact (s, -1);
875 public int FindStringExact (string s, int startIndex)
877 return FindStringExact (s, startIndex, true);
880 private int FindStringExact (string s, int startIndex, bool ignoreCase)
882 if (s == null || Items.Count == 0)
885 if (startIndex < -1 || startIndex >= Items.Count)
886 throw new ArgumentOutOfRangeException ("startIndex");
889 if (i == (Items.Count - 1))
893 if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
895 if (i == (Items.Count - 1))
897 } while (i != startIndex);
902 public int GetItemHeight (int index)
904 if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
906 if (index < 0 || index >= Items.Count )
907 throw new ArgumentOutOfRangeException ("The item height value is less than zero");
909 object item = Items [index];
910 if (item_heights.Contains (item))
911 return (int) item_heights [item];
913 MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
914 OnMeasureItem (args);
915 item_heights [item] = args.ItemHeight;
916 return args.ItemHeight;
922 protected override bool IsInputKey (Keys keyData)
924 switch (keyData & ~Keys.Modifiers) {
940 protected override void OnBackColorChanged (EventArgs e)
942 base.OnBackColorChanged (e);
944 if (textbox_ctrl != null)
945 textbox_ctrl.BackColor = BackColor;
948 protected override void OnDataSourceChanged (EventArgs e)
950 base.OnDataSourceChanged (e);
954 ** This 'Debugger.IsAttached' hack is here because of
955 ** Xamarin Bug #2234, which noted that when changing
956 ** the DataSource, in Windows exceptions are eaten
957 ** when SelectedIndexChanged is fired. However, when
958 ** the debugger is running (i.e. in MonoDevelop), we
959 ** want to be alerted of exceptions.
962 if (Debugger.IsAttached) {
968 //ignore exceptions here per
974 private void SetSelectedIndex ()
976 if (DataSource == null || DataManager == null) {
980 SelectedIndex = DataManager.Position;
984 protected override void OnDisplayMemberChanged (EventArgs e)
986 base.OnDisplayMemberChanged (e);
988 if (DataManager == null)
991 SelectedIndex = DataManager.Position;
993 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
994 SetControlText (GetItemText (Items [selected_index]), true);
996 if (!IsHandleCreated)
1002 protected virtual void OnDrawItem (DrawItemEventArgs e)
1004 DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
1009 internal void HandleDrawItem (DrawItemEventArgs e)
1011 // Only raise OnDrawItem if we are in an OwnerDraw mode
1013 case DrawMode.OwnerDrawFixed:
1014 case DrawMode.OwnerDrawVariable:
1018 ThemeEngine.Current.DrawComboBoxItem (this, e);
1023 protected virtual void OnDropDown (EventArgs e)
1025 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
1030 protected virtual void OnDropDownClosed (EventArgs e)
1032 EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
1037 protected virtual void OnDropDownStyleChanged (EventArgs e)
1039 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
1044 protected override void OnFontChanged (EventArgs e)
1046 base.OnFontChanged (e);
1048 if (textbox_ctrl != null)
1049 textbox_ctrl.Font = Font;
1051 if (!item_height_specified)
1052 item_height = Font.Height + 2;
1055 UpdateComboBoxBounds ();
1060 protected override void OnForeColorChanged (EventArgs e)
1062 base.OnForeColorChanged (e);
1063 if (textbox_ctrl != null)
1064 textbox_ctrl.ForeColor = ForeColor;
1067 [EditorBrowsable(EditorBrowsableState.Advanced)]
1068 protected override void OnGotFocus (EventArgs e)
1070 if (dropdown_style == ComboBoxStyle.DropDownList) {
1071 // We draw DDL styles manually, so they require a
1072 // refresh to have their selection drawn
1076 if (textbox_ctrl != null) {
1077 textbox_ctrl.SetSelectable (false);
1078 textbox_ctrl.ShowSelection = Enabled;
1079 textbox_ctrl.ActivateCaret (true);
1080 textbox_ctrl.SelectAll ();
1083 base.OnGotFocus (e);
1086 [EditorBrowsable(EditorBrowsableState.Advanced)]
1087 protected override void OnLostFocus (EventArgs e)
1089 if (dropdown_style == ComboBoxStyle.DropDownList) {
1090 // We draw DDL styles manually, so they require a
1091 // refresh to have their selection drawn
1095 if (listbox_ctrl != null && dropped_down) {
1096 listbox_ctrl.HideWindow ();
1099 if (textbox_ctrl != null) {
1100 textbox_ctrl.SetSelectable (true);
1101 textbox_ctrl.ActivateCaret (false);
1102 textbox_ctrl.ShowSelection = false;
1103 textbox_ctrl.SelectionLength = 0;
1104 textbox_ctrl.HideAutoCompleteList ();
1107 base.OnLostFocus (e);
1110 protected override void OnHandleCreated (EventArgs e)
1112 base.OnHandleCreated (e);
1114 SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
1116 if (textbox_ctrl != null)
1117 Controls.AddImplicit (textbox_ctrl);
1120 UpdateComboBoxBounds ();
1123 protected override void OnHandleDestroyed (EventArgs e)
1125 base.OnHandleDestroyed (e);
1128 protected override void OnKeyPress (KeyPressEventArgs e)
1130 if (dropdown_style == ComboBoxStyle.DropDownList) {
1131 int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
1133 SelectedIndex = index;
1134 if (DroppedDown) { //Scroll into view
1135 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1136 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1137 // Or, selecting an item earlier in the list.
1138 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1139 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1144 base.OnKeyPress (e);
1147 protected virtual void OnMeasureItem (MeasureItemEventArgs e)
1149 MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
1154 protected override void OnParentBackColorChanged (EventArgs e)
1156 base.OnParentBackColorChanged (e);
1159 protected override void OnResize (EventArgs e)
1162 if (listbox_ctrl != null)
1163 listbox_ctrl.CalcListBoxArea ();
1166 protected override void OnSelectedIndexChanged (EventArgs e)
1168 base.OnSelectedIndexChanged (e);
1170 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
1175 protected virtual void OnSelectedItemChanged (EventArgs e)
1179 protected override void OnSelectedValueChanged (EventArgs e)
1181 base.OnSelectedValueChanged (e);
1184 protected virtual void OnSelectionChangeCommitted (EventArgs e)
1186 EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
1191 protected override void RefreshItem (int index)
1193 if (index < 0 || index >= Items.Count)
1194 throw new ArgumentOutOfRangeException ("index");
1196 if (draw_mode == DrawMode.OwnerDrawVariable)
1197 item_heights.Remove (Items [index]);
1200 protected override void RefreshItems ()
1202 for (int i = 0; i < Items.Count; i++) {
1209 if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
1210 SetControlText (GetItemText (Items [selected_index]), false);
1213 public override void ResetText ()
1215 Text = String.Empty;
1218 protected override bool ProcessKeyEventArgs (ref Message m)
1220 return base.ProcessKeyEventArgs (ref m);
1223 [EditorBrowsable (EditorBrowsableState.Advanced)]
1224 protected override void OnKeyDown (KeyEventArgs e)
1229 [EditorBrowsable (EditorBrowsableState.Advanced)]
1230 protected override void OnValidating (CancelEventArgs e)
1232 base.OnValidating (e);
1235 [EditorBrowsable (EditorBrowsableState.Advanced)]
1236 protected override void OnTextChanged (EventArgs e)
1238 base.OnTextChanged (e);
1241 protected virtual void OnTextUpdate (EventArgs e)
1243 EventHandler eh = (EventHandler) Events [TextUpdateEvent];
1247 protected override void OnMouseLeave (EventArgs e)
1249 if (flat_style == FlatStyle.Popup)
1251 base.OnMouseLeave (e);
1254 protected override void OnMouseEnter (EventArgs e)
1256 if (flat_style == FlatStyle.Popup)
1258 base.OnMouseEnter (e);
1261 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
1263 base.ScaleControl (factor, specified);
1266 public void Select (int start, int length)
1269 throw new ArgumentException ("Start cannot be less than zero");
1272 throw new ArgumentException ("length cannot be less than zero");
1274 if (dropdown_style == ComboBoxStyle.DropDownList)
1277 textbox_ctrl.Select (start, length);
1280 public void SelectAll ()
1282 if (dropdown_style == ComboBoxStyle.DropDownList)
1285 if (textbox_ctrl != null) {
1286 textbox_ctrl.ShowSelection = true;
1287 textbox_ctrl.SelectAll ();
1291 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
1293 bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
1294 bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
1296 if ((specified & BoundsSpecified.Height) != 0 ||
1297 (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
1299 requested_height = height;
1300 height = SnapHeight (height);
1303 base.SetBoundsCore (x, y, width, height, specified);
1306 protected override void SetItemCore (int index, object value)
1308 if (index < 0 || index >= Items.Count)
1311 Items[index] = value;
1314 protected override void SetItemsCore (IList value)
1319 Items.AddRange (value);
1325 public override string ToString ()
1327 return base.ToString () + ", Items.Count:" + Items.Count;
1330 protected override void WndProc (ref Message m)
1332 switch ((Msg) m.Msg) {
1334 case Msg.WM_KEYDOWN:
1335 Keys keys = (Keys) m.WParam.ToInt32 ();
1336 // Don't pass the message to base if auto complete is being used and available.
1337 if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
1338 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1341 if (keys == Keys.Up || keys == Keys.Down)
1343 goto case Msg.WM_CHAR;
1345 // Call our own handler first and send the message to the TextBox if still needed
1346 if (!ProcessKeyMessage (ref m) && textbox_ctrl != null)
1347 XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
1349 case Msg.WM_MOUSELEAVE:
1350 Point location = PointToClient (Control.MousePosition);
1351 if (ClientRectangle.Contains (location))
1357 base.WndProc (ref m);
1360 #endregion Public Methods
1362 #region Private Methods
1363 void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
1364 if(auto_complete_source == AutoCompleteSource.CustomSource) {
1365 //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
1369 internal override bool InternalCapture {
1370 get { return Capture; }
1374 void LayoutComboBox ()
1376 int border = ThemeEngine.Current.Border3DSize.Width;
1378 text_area = ClientRectangle;
1379 text_area.Height = PreferredHeight;
1381 listbox_area = ClientRectangle;
1382 listbox_area.Y = text_area.Bottom + 3;
1383 listbox_area.Height -= (text_area.Height + 2);
1385 Rectangle prev_button_area = button_area;
1387 if (DropDownStyle == ComboBoxStyle.Simple)
1388 button_area = Rectangle.Empty;
1390 button_area = text_area;
1391 button_area.X = text_area.Right - button_width - border;
1392 button_area.Y = text_area.Y + border;
1393 button_area.Width = button_width;
1394 button_area.Height = text_area.Height - 2 * border;
1395 if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
1396 button_area.Inflate (1, 1);
1398 button_area.Width -= 2;
1402 if (button_area != prev_button_area) {
1403 prev_button_area.Y -= border;
1404 prev_button_area.Width += border;
1405 prev_button_area.Height += 2 * border;
1406 Invalidate (prev_button_area);
1407 Invalidate (button_area);
1410 if (textbox_ctrl != null) {
1411 int text_border = border + 1;
1412 textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
1413 textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
1414 textbox_ctrl.Height = text_area.Height - text_border * 2;
1417 if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
1418 listbox_ctrl.Location = listbox_area.Location;
1419 listbox_ctrl.CalcListBoxArea ();
1423 private void CreateComboListBox ()
1425 listbox_ctrl = new ComboListBox (this);
1426 listbox_ctrl.HighlightedIndex = SelectedIndex;
1429 internal void Draw (Rectangle clip, Graphics dc)
1431 Theme theme = ThemeEngine.Current;
1432 FlatStyle style = FlatStyle.Standard;
1433 bool is_flat = false;
1435 style = this.FlatStyle;
1436 is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
1438 theme.ComboBoxDrawBackground (this, dc, clip, style);
1440 int border = theme.Border3DSize.Width;
1442 // No edit control, we paint the edit ourselves
1443 if (dropdown_style == ComboBoxStyle.DropDownList) {
1444 DrawItemState state = DrawItemState.None;
1445 Color back_color = BackColor;
1446 Color fore_color = ForeColor;
1447 Rectangle item_rect = text_area;
1448 item_rect.X += border;
1449 item_rect.Y += border;
1450 item_rect.Width -= (button_area.Width + 2 * border);
1451 item_rect.Height -= 2 * border;
1454 state = DrawItemState.Selected;
1455 state |= DrawItemState.Focus;
1456 back_color = SystemColors.Highlight;
1457 fore_color = SystemColors.HighlightText;
1460 state |= DrawItemState.ComboBoxEdit;
1461 HandleDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, fore_color, back_color));
1464 if (show_dropdown_button) {
1465 ButtonState current_state;
1467 current_state = button_state;
1469 current_state = ButtonState.Inactive;
1471 if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
1472 dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
1475 theme.DrawFlatStyleComboButton (dc, button_area, current_state);
1477 theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
1482 internal bool DropDownButtonEntered {
1483 get { return drop_down_button_entered; }
1485 if (drop_down_button_entered == value)
1487 drop_down_button_entered = value;
1488 if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
1489 Invalidate (button_area);
1493 internal void DropDownListBox ()
1495 DropDownButtonEntered = false;
1497 if (DropDownStyle == ComboBoxStyle.Simple)
1500 if (listbox_ctrl == null)
1501 CreateComboListBox ();
1503 listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
1505 FindMatchOrSetIndex(SelectedIndex);
1507 if (textbox_ctrl != null)
1508 textbox_ctrl.HideAutoCompleteList ();
1510 if (listbox_ctrl.ShowWindow ())
1511 dropped_down = true;
1513 button_state = ButtonState.Pushed;
1514 if (dropdown_style == ComboBoxStyle.DropDownList)
1515 Invalidate (text_area);
1518 internal void DropDownListBoxFinished ()
1520 if (DropDownStyle == ComboBoxStyle.Simple)
1523 FindMatchOrSetIndex (SelectedIndex);
1524 button_state = ButtonState.Normal;
1525 Invalidate (button_area);
1526 dropped_down = false;
1527 OnDropDownClosed (EventArgs.Empty);
1529 * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
1530 * this causes the popup to appear under the main form. This is horrible but necessary
1533 // If the user opens a new form in an event, it will close our dropdown,
1534 // so we need a null check here
1535 if (listbox_ctrl != null) {
1536 listbox_ctrl.Dispose ();
1537 listbox_ctrl = null;
1539 // The auto complete list could have been shown after the listbox,
1540 // so make sure it's hidden.
1541 if (textbox_ctrl != null)
1542 textbox_ctrl.HideAutoCompleteList ();
1545 private int FindStringCaseInsensitive (string search)
1547 if (search.Length == 0) {
1551 for (int i = 0; i < Items.Count; i++)
1553 if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
1560 // Search in the list for the substring, starting the search at the list
1561 // position specified, the search wraps thus covering all the list.
1562 internal int FindStringCaseInsensitive (string search, int start_index)
1564 if (search.Length == 0) {
1567 // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
1568 if (start_index < 0 || start_index > Items.Count)
1569 throw new ArgumentOutOfRangeException("start_index");
1571 for (int i = 0; i < Items.Count; i++) {
1572 int index = (i + start_index) % Items.Count;
1573 if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
1580 internal override bool IsInputCharInternal (char charCode)
1585 internal override ContextMenu ContextMenuInternal {
1587 return base.ContextMenuInternal;
1590 base.ContextMenuInternal = value;
1591 if (textbox_ctrl != null) {
1592 textbox_ctrl.ContextMenu = value;
1597 internal void RestoreContextMenu ()
1599 textbox_ctrl.RestoreContextMenu ();
1602 private void OnKeyDownCB(object sender, KeyEventArgs e)
1604 if (Items.Count == 0)
1607 // for keyboard navigation, we have to do our own scroll, since
1608 // the default behaviour for the SelectedIndex property is a little different,
1609 // setting the selected index in the top always
1615 FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
1618 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1619 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1623 if ((e.Modifiers & Keys.Alt) == Keys.Alt)
1626 FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
1629 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1630 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1634 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1638 SetSelectedIndex (Math.Max (SelectedIndex - offset, 0), true);
1641 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1642 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1646 if (SelectedIndex == -1) {
1648 if (dropdown_style != ComboBoxStyle.Simple)
1652 offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
1656 SetSelectedIndex (Math.Min (SelectedIndex + offset, Items.Count - 1), true);
1659 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1660 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1665 if (listbox_ctrl != null && listbox_ctrl.Visible)
1666 DropDownListBoxFinished ();
1670 if (dropdown_style == ComboBoxStyle.DropDownList) {
1674 if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
1675 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
1680 if (dropdown_style == ComboBoxStyle.DropDownList) {
1681 SetSelectedIndex (Items.Count - 1, true);
1684 if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
1685 listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
1694 void SetSelectedIndex (int value, bool supressAutoScroll)
1696 if (selected_index == value)
1699 if (value <= -2 || value >= Items.Count)
1700 throw new ArgumentOutOfRangeException ("SelectedIndex");
1702 selected_index = value;
1704 if (dropdown_style != ComboBoxStyle.DropDownList) {
1706 SetControlText (string.Empty, false, supressAutoScroll);
1708 SetControlText (GetItemText (Items [value]), false, supressAutoScroll);
1711 if (DropDownStyle == ComboBoxStyle.DropDownList)
1714 if (listbox_ctrl != null)
1715 listbox_ctrl.HighlightedIndex = value;
1717 OnSelectedValueChanged (EventArgs.Empty);
1718 OnSelectedIndexChanged (EventArgs.Empty);
1719 OnSelectedItemChanged (EventArgs.Empty);
1722 // If no item is currently selected, and an item is found matching the text
1723 // in the textbox, then selected that item. Otherwise the item at the given
1724 // index is selected.
1725 private void FindMatchOrSetIndex(int index)
1728 if (SelectedIndex == -1 && Text.Length != 0)
1729 match = FindStringCaseInsensitive(Text);
1731 SetSelectedIndex (match, true);
1733 SetSelectedIndex (index, true);
1736 void OnMouseDownCB (object sender, MouseEventArgs e)
1739 if (DropDownStyle == ComboBoxStyle.DropDownList)
1740 area = ClientRectangle;
1744 if (area.Contains (e.X, e.Y)) {
1745 if (Items.Count > 0)
1748 button_state = ButtonState.Pushed;
1749 OnDropDown (EventArgs.Empty);
1752 Invalidate (button_area);
1758 void OnMouseEnter (object sender, EventArgs e)
1760 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
1764 void OnMouseLeave (object sender, EventArgs e)
1766 if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
1767 drop_down_button_entered = false;
1770 if (show_dropdown_button)
1771 DropDownButtonEntered = false;
1775 void OnMouseMoveCB (object sender, MouseEventArgs e)
1777 if (show_dropdown_button && !dropped_down)
1778 DropDownButtonEntered = button_area.Contains (e.Location);
1780 if (DropDownStyle == ComboBoxStyle.Simple)
1783 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1784 Point location = listbox_ctrl.PointToClient (Control.MousePosition);
1785 if (listbox_ctrl.ClientRectangle.Contains (location))
1786 listbox_ctrl.Capture = true;
1790 void OnMouseUpCB (object sender, MouseEventArgs e)
1794 button_state = ButtonState.Normal;
1795 Invalidate (button_area);
1797 OnClick (EventArgs.Empty);
1800 listbox_ctrl.Capture = true;
1803 private void OnMouseWheelCB (object sender, MouseEventArgs me)
1805 if (Items.Count == 0)
1808 if (listbox_ctrl != null && listbox_ctrl.Visible) {
1809 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
1810 listbox_ctrl.Scroll (-lines);
1812 int lines = me.Delta / 120;
1813 int index = SelectedIndex - lines;
1816 else if (index >= Items.Count)
1817 index = Items.Count - 1;
1818 SelectedIndex = index;
1822 MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
1824 Point loc = PointToClient (Control.MousePosition);
1825 return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
1828 internal override void OnPaintInternal (PaintEventArgs pevent)
1830 if (suspend_ctrlupdate)
1833 Draw (ClientRectangle, pevent.Graphics);
1836 private void OnTextBoxClick (object sender, EventArgs e)
1841 private void OnTextChangedEdit (object sender, EventArgs e)
1843 if (process_textchanged_event == false)
1846 int item = FindStringCaseInsensitive (textbox_ctrl.Text);
1849 // Setting base.Text below will raise this event
1850 // if we found something
1851 OnTextChanged (EventArgs.Empty);
1855 if (listbox_ctrl != null) {
1857 if (process_texchanged_autoscroll)
1858 listbox_ctrl.EnsureTop (item);
1861 base.Text = textbox_ctrl.Text;
1864 private void OnTextKeyPress (object sender, KeyPressEventArgs e)
1866 selected_index = -1;
1867 if (listbox_ctrl != null)
1868 listbox_ctrl.HighlightedIndex = -1;
1871 internal void SetControlText (string s, bool suppressTextChanged)
1873 SetControlText (s, suppressTextChanged, false);
1876 internal void SetControlText (string s, bool suppressTextChanged, bool supressAutoScroll)
1878 if (suppressTextChanged)
1879 process_textchanged_event = false;
1880 if (supressAutoScroll)
1881 process_texchanged_autoscroll = false;
1883 textbox_ctrl.Text = s;
1884 textbox_ctrl.SelectAll ();
1885 process_textchanged_event = true;
1886 process_texchanged_autoscroll = true;
1889 void UpdateComboBoxBounds ()
1891 if (requested_height == -1)
1894 // Save the requested height since set bounds can destroy it
1895 int save_height = requested_height;
1896 SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
1897 BoundsSpecified.Height);
1898 requested_height = save_height;
1901 int SnapHeight (int height)
1903 if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
1904 if (IntegralHeight) {
1905 int border = ThemeEngine.Current.Border3DSize.Height;
1906 int lb_height = (height - PreferredHeight - 2) - border * 2;
1907 if (lb_height > ItemHeight) {
1908 int partial = (lb_height) % ItemHeight;
1910 } else if (lb_height < ItemHeight)
1911 height = PreferredHeight;
1914 height = PreferredHeight;
1919 private void UpdatedItems ()
1921 if (listbox_ctrl != null) {
1922 listbox_ctrl.UpdateLastVisibleItem ();
1923 listbox_ctrl.CalcListBoxArea ();
1924 listbox_ctrl.Refresh ();
1928 #endregion Private Methods
1930 [ListBindableAttribute (false)]
1931 public class ObjectCollection : IList, ICollection, IEnumerable
1934 private ComboBox owner;
1935 internal ArrayList object_items = new ArrayList ();
1937 #region UIA Framework Events
1940 // We are using Reflection to add/remove internal events.
1941 // Class ListProvider uses the events.
1943 //Event used to generate UIA StructureChangedEvent
1944 static object UIACollectionChangedEvent = new object ();
1946 internal event CollectionChangeEventHandler UIACollectionChanged {
1947 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1948 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1951 internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
1953 CollectionChangeEventHandler eh
1954 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1959 #endregion UIA Framework Events
1961 public ObjectCollection (ComboBox owner)
1966 #region Public Properties
1968 get { return object_items.Count; }
1971 public bool IsReadOnly {
1972 get { return false; }
1976 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
1977 public virtual object this [int index] {
1979 if (index < 0 || index >= Count)
1980 throw new ArgumentOutOfRangeException ("index");
1982 return object_items[index];
1985 if (index < 0 || index >= Count)
1986 throw new ArgumentOutOfRangeException ("index");
1988 throw new ArgumentNullException ("value");
1990 //UIA Framework event: Item Removed
1991 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
1993 object_items[index] = value;
1995 //UIA Framework event: Item Added
1996 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
1998 if (owner.listbox_ctrl != null)
1999 owner.listbox_ctrl.InvalidateItem (index);
2000 if (index == owner.SelectedIndex) {
2001 if (owner.textbox_ctrl == null)
2004 owner.textbox_ctrl.SelectedText = value.ToString ();
2009 bool ICollection.IsSynchronized {
2010 get { return false; }
2013 object ICollection.SyncRoot {
2014 get { return this; }
2017 bool IList.IsFixedSize {
2018 get { return false; }
2021 #endregion Public Properties
2023 #region Public Methods
2024 public int Add (object item)
2028 idx = AddItem (item, false);
2029 owner.UpdatedItems ();
2033 public void AddRange (object[] items)
2036 throw new ArgumentNullException ("items");
2038 foreach (object mi in items)
2044 owner.UpdatedItems ();
2047 public void Clear ()
2049 owner.selected_index = -1;
2050 object_items.Clear ();
2051 owner.UpdatedItems ();
2054 //UIA Framework event: Items list cleared
2055 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
2058 public bool Contains (object value)
2061 throw new ArgumentNullException ("value");
2063 return object_items.Contains (value);
2066 public void CopyTo (object [] destination, int arrayIndex)
2068 object_items.CopyTo (destination, arrayIndex);
2071 void ICollection.CopyTo (Array destination, int index)
2073 object_items.CopyTo (destination, index);
2076 public IEnumerator GetEnumerator ()
2078 return object_items.GetEnumerator ();
2081 int IList.Add (object item)
2086 public int IndexOf (object value)
2089 throw new ArgumentNullException ("value");
2091 return object_items.IndexOf (value);
2094 public void Insert (int index, object item)
2096 if (index < 0 || index > Count)
2097 throw new ArgumentOutOfRangeException ("index");
2099 throw new ArgumentNullException ("item");
2101 owner.BeginUpdate ();
2104 AddItem (item, false);
2106 object_items.Insert (index, item);
2107 //UIA Framework event: Item added
2108 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2111 owner.EndUpdate (); // Calls UpdatedItems
2114 public void Remove (object value)
2118 int index = IndexOf (value);
2123 public void RemoveAt (int index)
2125 if (index < 0 || index >= Count)
2126 throw new ArgumentOutOfRangeException ("index");
2128 if (index < owner.SelectedIndex)
2129 --owner.SelectedIndex;
2130 else if (index == owner.SelectedIndex)
2131 owner.SelectedIndex = -1;
2133 object removed = object_items [index];
2135 object_items.RemoveAt (index);
2136 owner.UpdatedItems ();
2138 //UIA Framework event: Item removed
2139 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
2141 #endregion Public Methods
2143 #region Private Methods
2144 private int AddItem (object item, bool suspend)
2146 // suspend means do not sort as we put new items in, we will do a
2147 // big sort at the end
2149 throw new ArgumentNullException ("item");
2151 if (owner.Sorted && !suspend) {
2153 foreach (object o in object_items) {
2154 if (String.Compare (item.ToString (), o.ToString ()) < 0) {
2155 object_items.Insert (index, item);
2157 // If we added the new item before the selectedindex
2158 // bump the selectedindex by one, behavior differs if
2159 // Handle has not been created.
2160 if (index <= owner.selected_index && owner.IsHandleCreated)
2161 owner.selected_index++;
2163 //UIA Framework event: Item added
2164 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2171 object_items.Add (item);
2173 //UIA Framework event: Item added
2174 OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
2176 return object_items.Count - 1;
2179 internal void AddRange (IList items)
2181 foreach (object mi in items)
2182 AddItem (mi, false);
2187 owner.UpdatedItems ();
2190 internal void Sort ()
2192 // If the objects the user put here don't have their own comparer,
2193 // use one that compares based on the object's ToString
2194 if (object_items.Count > 0 && object_items[0] is IComparer)
2195 object_items.Sort ();
2197 object_items.Sort (new ObjectComparer (owner));
2200 private class ObjectComparer : IComparer
2202 private ListControl owner;
2204 public ObjectComparer (ListControl owner)
2209 #region IComparer Members
2210 public int Compare (object x, object y)
2212 return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
2216 #endregion Private Methods
2219 internal class ComboTextBox : TextBox {
2221 private ComboBox owner;
2223 public ComboTextBox (ComboBox owner)
2226 ShowSelection = false;
2227 owner.EnabledChanged += OwnerEnabledChangedHandler;
2228 owner.LostFocus += OwnerLostFocusHandler;
2231 void OwnerEnabledChangedHandler (object o, EventArgs args)
2233 ShowSelection = owner.Focused && owner.Enabled;
2236 void OwnerLostFocusHandler (object o, EventArgs args)
2238 if (IsAutoCompleteAvailable)
2242 protected override void OnKeyDown (KeyEventArgs args)
2244 if (args.KeyCode == Keys.Enter && IsAutoCompleteAvailable)
2247 base.OnKeyDown (args);
2250 internal override void OnAutoCompleteValueSelected (EventArgs args)
2252 base.OnAutoCompleteValueSelected (args);
2256 internal void SetSelectable (bool selectable)
2258 SetStyle (ControlStyles.Selectable, selectable);
2261 internal void ActivateCaret (bool active)
2264 document.CaretHasFocus ();
2266 document.CaretLostFocus ();
2269 internal override void OnTextUpdate ()
2271 base.OnTextUpdate ();
2272 owner.OnTextUpdate (EventArgs.Empty);
2275 protected override void OnGotFocus (EventArgs e)
2277 owner.Select (false, true);
2280 protected override void OnLostFocus (EventArgs e)
2282 owner.Select (false, true);
2285 // We have to pass these events to our owner - MouseMove is not, however.
2287 protected override void OnMouseDown (MouseEventArgs e)
2289 base.OnMouseDown (e);
2290 owner.OnMouseDown (owner.TranslateMouseEventArgs (e));
2293 protected override void OnMouseUp (MouseEventArgs e)
2296 owner.OnMouseUp (owner.TranslateMouseEventArgs (e));
2299 protected override void OnMouseClick (MouseEventArgs e)
2301 base.OnMouseClick (e);
2302 owner.OnMouseClick (owner.TranslateMouseEventArgs (e));
2305 protected override void OnMouseDoubleClick (MouseEventArgs e)
2307 base.OnMouseDoubleClick (e);
2308 owner.OnMouseDoubleClick (owner.TranslateMouseEventArgs (e));
2311 public override bool Focused {
2313 return owner.Focused;
2317 internal override bool ActivateOnShow { get { return false; } }
2320 internal class ComboListBox : Control
2322 private ComboBox owner;
2323 private VScrollBarLB vscrollbar_ctrl;
2324 private int top_item; /* First item that we show the in the current page */
2325 private int last_item; /* Last visible item */
2326 internal int page_size; /* Number of listbox items per page */
2327 private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
2329 internal enum ItemNavigation
2339 #region UIA Framework: Properties
2341 internal int UIATopItem {
2342 get { return top_item; }
2345 internal int UIALastItem {
2346 get { return last_item; }
2349 internal ScrollBar UIAVScrollBar {
2350 get { return vscrollbar_ctrl; }
2355 class VScrollBarLB : VScrollBar
2357 public VScrollBarLB ()
2361 internal override bool InternalCapture {
2362 get { return Capture; }
2366 public void FireMouseDown (MouseEventArgs e)
2371 e = TranslateEvent (e);
2375 public void FireMouseUp (MouseEventArgs e)
2380 e = TranslateEvent (e);
2384 public void FireMouseMove (MouseEventArgs e)
2389 e = TranslateEvent (e);
2393 MouseEventArgs TranslateEvent (MouseEventArgs e)
2395 Point loc = PointToClient (Control.MousePosition);
2396 return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
2400 public ComboListBox (ComboBox owner)
2407 MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
2409 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
2410 SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
2412 this.is_visible = false;
2414 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2415 InternalBorderStyle = BorderStyle.Fixed3D;
2417 InternalBorderStyle = BorderStyle.FixedSingle;
2420 protected override CreateParams CreateParams
2423 CreateParams cp = base.CreateParams;
2424 if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
2427 cp.Style ^= (int)WindowStyles.WS_CHILD;
2428 cp.Style ^= (int)WindowStyles.WS_VISIBLE;
2429 cp.Style |= (int)WindowStyles.WS_POPUP;
2430 cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
2435 internal override bool InternalCapture {
2444 internal override bool ActivateOnShow { get { return false; } }
2445 #region Private Methods
2447 // Calcs the listbox area
2448 internal void CalcListBoxArea ()
2451 bool show_scrollbar;
2453 if (owner.DropDownStyle == ComboBoxStyle.Simple) {
2454 Rectangle area = owner.listbox_area;
2456 height = area.Height;
2457 show_scrollbar = owner.Items.Count * owner.ItemHeight > height;
2459 // No calculation needed
2460 if (height <= 0 || width <= 0)
2464 else { // DropDown or DropDownList
2466 width = owner.DropDownWidth;
2467 int visible_items_count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
2469 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2471 for (int i = 0; i < visible_items_count; i++) {
2472 height += owner.GetItemHeight (i);
2475 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2478 if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
2479 height = owner.ItemHeight * visible_items_count;
2480 show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
2482 // ignore visible items count, and use manual height instead
2483 height = owner.DropDownHeight;
2484 show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
2489 page_size = Math.Max (height / owner.ItemHeight, 1);
2491 ComboBoxStyle dropdown_style = owner.DropDownStyle;
2492 if (!show_scrollbar) {
2494 if (vscrollbar_ctrl != null)
2495 vscrollbar_ctrl.Visible = false;
2496 if (dropdown_style != ComboBoxStyle.Simple)
2497 height = owner.ItemHeight * owner.items.Count;
2499 /* Need vertical scrollbar */
2500 if (vscrollbar_ctrl == null) {
2501 vscrollbar_ctrl = new VScrollBarLB ();
2502 vscrollbar_ctrl.Minimum = 0;
2503 vscrollbar_ctrl.SmallChange = 1;
2504 vscrollbar_ctrl.LargeChange = 1;
2505 vscrollbar_ctrl.Maximum = 0;
2506 vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
2507 Controls.AddImplicit (vscrollbar_ctrl);
2510 vscrollbar_ctrl.Dock = DockStyle.Right;
2512 vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
2513 int large = page_size;
2516 vscrollbar_ctrl.LargeChange = large;
2517 vscrollbar_ctrl.Visible = true;
2519 int hli = HighlightedIndex;
2521 hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
2522 vscrollbar_ctrl.Value = hli;
2526 Size = new Size (width, height);
2527 textarea_drawable = ClientRectangle;
2528 textarea_drawable.Width = width;
2529 textarea_drawable.Height = height;
2531 if (vscrollbar_ctrl != null && show_scrollbar)
2532 textarea_drawable.Width -= vscrollbar_ctrl.Width;
2534 last_item = LastVisibleItem ();
2537 private void Draw (Rectangle clip, Graphics dc)
2539 dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
2541 if (owner.Items.Count > 0) {
2543 for (int i = top_item; i <= last_item; i++) {
2544 Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
2546 if (!clip.IntersectsWith (item_rect))
2549 DrawItemState state = DrawItemState.None;
2550 Color back_color = owner.BackColor;
2551 Color fore_color = owner.ForeColor;
2553 if (i == HighlightedIndex) {
2554 state |= DrawItemState.Selected;
2555 back_color = SystemColors.Highlight;
2556 fore_color = SystemColors.HighlightText;
2558 if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
2559 state |= DrawItemState.Focus;
2563 owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
2564 i, state, fore_color, back_color));
2569 int highlighted_index = -1;
2571 public int HighlightedIndex {
2572 get { return highlighted_index; }
2574 if (highlighted_index == value)
2577 if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
2578 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2579 highlighted_index = value;
2580 if (highlighted_index != -1)
2581 Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
2585 private Rectangle GetItemDisplayRectangle (int index, int top_index)
2587 if (index < 0 || index >= owner.Items.Count)
2588 throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
2590 Rectangle item_rect = new Rectangle ();
2591 int height = owner.GetItemHeight (index);
2594 item_rect.Width = textarea_drawable.Width;
2595 if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
2597 for (int i = top_index; i < index; i++)
2598 item_rect.Y += owner.GetItemHeight (i);
2600 item_rect.Y = height * (index - top_index);
2602 item_rect.Height = height;
2606 public void HideWindow ()
2608 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2613 owner.DropDownListBoxFinished ();
2616 private int IndexFromPointDisplayRectangle (int x, int y)
2618 for (int i = top_item; i <= last_item; i++) {
2619 if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
2626 public void InvalidateItem (int index)
2629 Invalidate (GetItemDisplayRectangle (index, top_item));
2632 public int LastVisibleItem ()
2634 Rectangle item_rect;
2635 int top_y = textarea_drawable.Y + textarea_drawable.Height;
2638 for (i = top_item; i < owner.Items.Count; i++) {
2639 item_rect = GetItemDisplayRectangle (i, top_item);
2640 if (item_rect.Y + item_rect.Height > top_y) {
2647 public void SetTopItem (int item)
2649 if (top_item == item)
2652 UpdateLastVisibleItem ();
2656 public int FirstVisibleItem ()
2661 public void EnsureTop (int item)
2663 if (owner.Items.Count == 0)
2665 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2668 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2671 else if (item < vscrollbar_ctrl.Minimum)
2672 item = vscrollbar_ctrl.Minimum;
2674 vscrollbar_ctrl.Value = item;
2677 bool scrollbar_grabbed = false;
2681 if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
2684 return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
2688 protected override void OnMouseDown (MouseEventArgs e)
2691 vscrollbar_ctrl.FireMouseDown (e);
2692 scrollbar_grabbed = true;
2696 protected override void OnMouseMove (MouseEventArgs e)
2698 if (owner.DropDownStyle == ComboBoxStyle.Simple)
2701 if (scrollbar_grabbed || (!Capture && InScrollBar)) {
2702 vscrollbar_ctrl.FireMouseMove (e);
2706 Point pt = PointToClient (Control.MousePosition);
2707 int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
2710 HighlightedIndex = index;
2713 protected override void OnMouseUp (MouseEventArgs e)
2715 int index = IndexFromPointDisplayRectangle (e.X, e.Y);
2717 if (scrollbar_grabbed) {
2718 vscrollbar_ctrl.FireMouseUp (e);
2719 scrollbar_grabbed = false;
2721 HighlightedIndex = index;
2730 bool is_change = owner.SelectedIndex != index;
2732 owner.SetSelectedIndex (index, true);
2733 owner.OnSelectionChangeCommitted (new EventArgs ());
2735 // If the user selected the already selected item, SelectedIndex
2736 // won't fire these events, but .Net does, so we do it here
2738 owner.OnSelectedValueChanged (EventArgs.Empty);
2739 owner.OnSelectedIndexChanged (EventArgs.Empty);
2745 internal override void OnPaintInternal (PaintEventArgs pevent)
2747 Draw (pevent.ClipRectangle,pevent.Graphics);
2750 public bool ShowWindow ()
2752 if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
2755 HighlightedIndex = owner.SelectedIndex;
2761 owner.OnDropDown (EventArgs.Empty);
2765 public void UpdateLastVisibleItem ()
2767 last_item = LastVisibleItem ();
2770 public void Scroll (int delta)
2772 if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
2775 int max = vscrollbar_ctrl.Maximum - page_size + 1;
2777 int val = vscrollbar_ctrl.Value + delta;
2780 else if (val < vscrollbar_ctrl.Minimum)
2781 val = vscrollbar_ctrl.Minimum;
2782 vscrollbar_ctrl.Value = val;
2785 private void OnMouseWheelCLB (object sender, MouseEventArgs me)
2787 if (owner.Items.Count == 0)
2790 int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
2795 private void VerticalScrollEvent (object sender, EventArgs e)
2797 if (top_item == vscrollbar_ctrl.Value)
2800 top_item = vscrollbar_ctrl.Value;
2801 UpdateLastVisibleItem ();
2805 protected override void WndProc(ref Message m) {
2806 if (m.Msg == (int)Msg.WM_SETFOCUS) {
2807 owner.Select (false, true);
2809 base.WndProc (ref m);
2812 #endregion Private Methods