using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
+using System.Diagnostics;
namespace System.Windows.Forms
{
[DefaultProperty("Items")]
[DefaultEvent("SelectedIndexChanged")]
[Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
-#if NET_2_0
[DefaultBindingProperty ("Text")]
[ClassInterface (ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
-#endif
public class ComboBox : ListControl
{
private DrawMode draw_mode = DrawMode.Normal;
private Rectangle listbox_area;
private const int button_width = 16;
bool drop_down_button_entered;
-#if NET_2_0
private AutoCompleteStringCollection auto_complete_custom_source = null;
private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
private FlatStyle flat_style;
private int drop_down_height;
const int default_drop_down_height = 106;
-#endif
[ComVisible(true)]
public class ChildAccessibleObject : AccessibleObject {
background_color = ThemeEngine.Current.ColorWindow;
border_style = BorderStyle.None;
-#if NET_2_0
drop_down_height = default_drop_down_height;
flat_style = FlatStyle.Standard;
-#endif
/* Events */
MouseDown += new MouseEventHandler (OnMouseDownCB);
remove { base.BackgroundImageChanged -= value; }
}
-#if NET_2_0
-
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public new event EventHandler BackgroundImageLayoutChanged
add { base.DoubleClick += value; }
remove { base.DoubleClick -= value; }
}
-#endif
static object DrawItemEvent = new object ();
static object DropDownEvent = new object ();
static object MeasureItemEvent = new object ();
static object SelectedIndexChangedEvent = new object ();
static object SelectionChangeCommittedEvent = new object ();
-#if NET_2_0
static object DropDownClosedEvent = new object ();
static object TextUpdateEvent = new object ();
-#endif
public event DrawItemEventHandler DrawItem {
add { Events.AddHandler (DrawItemEvent, value); }
add { Events.AddHandler (DropDownEvent, value); }
remove { Events.RemoveHandler (DropDownEvent, value); }
}
-#if NET_2_0
public event EventHandler DropDownClosed
{
add { Events.AddHandler (DropDownClosedEvent, value); }
remove { Events.RemoveHandler (DropDownClosedEvent, value); }
}
-#endif
public event EventHandler DropDownStyleChanged {
add { Events.AddHandler (DropDownStyleChangedEvent, value); }
add { Events.AddHandler (MeasureItemEvent, value); }
remove { Events.RemoveHandler (MeasureItemEvent, value); }
}
-#if NET_2_0
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public new event EventHandler PaddingChanged
add { base.PaddingChanged += value; }
remove { base.PaddingChanged -= value; }
}
-#endif
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
}
-#if NET_2_0
public event EventHandler TextUpdate
{
add { Events.AddHandler (TextUpdateEvent, value); }
remove { Events.RemoveHandler (TextUpdateEvent, value); }
}
-#endif
#endregion Events
#region Public Properties
-#if NET_2_0
[MonoTODO("AutoCompletion algorithm is currently not implemented.")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[Browsable (true)]
textbox_ctrl.AutoCompleteInternalSource = null;
}
}
-#endif
public override Color BackColor {
get { return base.BackColor; }
set {
}
}
-#if NET_2_0
[Browsable (false)]
[EditorBrowsable (EditorBrowsableState.Never)]
public override ImageLayout BackgroundImageLayout {
get { return base.BackgroundImageLayout; }
set { base.BackgroundImageLayout = value; }
}
-#endif
protected override CreateParams CreateParams {
get { return base.CreateParams;}
}
-#if NET_2_0
[DefaultValue ((string)null)]
[AttributeProvider (typeof (IListSource))]
[RefreshProperties (RefreshProperties.Repaint)]
get { return base.DataSource; }
set { base.DataSource = value; }
}
-#endif
protected override Size DefaultSize {
get { return new Size (121, 21); }
}
}
-#if NET_2_0
[Browsable (true)]
[DefaultValue (106)]
[EditorBrowsable (EditorBrowsableState.Always)]
IntegralHeight = false;
}
}
-#endif
[DefaultValue (ComboBoxStyle.DropDown)]
[RefreshProperties(RefreshProperties.Repaint)]
if (IsHandleCreated == true)
Controls.AddImplicit (textbox_ctrl);
-#if NET_2_0
SetTextBoxAutoCompleteData ();
-#endif
}
ResumeLayout ();
return;
if (value < 1)
-#if NET_2_0
throw new ArgumentOutOfRangeException ("DropDownWidth",
"The DropDownWidth value is less than one.");
-#else
- throw new ArgumentException ("The DropDownWidth value is less than one.");
-#endif
dropdown_width = value;
}
}
}
-#if NET_2_0
[DefaultValue (FlatStyle.Standard)]
[Localizable (true)]
[MWFCategory("Appearance")]
Invalidate ();
}
}
-#endif
public override bool Focused {
get { return base.Focused; }
}
set {
if (value < 1)
-#if NET_2_0
throw new ArgumentOutOfRangeException ("ItemHeight",
"The item height value is less than one.");
-#else
- throw new ArgumentException ("The item height value is less than one.");
-#endif
item_height_specified = true;
item_height = value;
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[Localizable (true)]
[Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
-#if NET_2_0
[MergableProperty (false)]
-#endif
[MWFCategory("Data")]
public ComboBox.ObjectCollection Items {
get { return items; }
}
}
-#if NET_2_0
public override Size MaximumSize {
get { return base.MaximumSize; }
set {
base.MaximumSize = new Size (value.Width, 0);
}
}
-#endif
[DefaultValue (0)]
[Localizable (true)]
}
}
-#if NET_2_0
public override Size MinimumSize {
get { return base.MinimumSize; }
set {
get { return base.Padding; }
set { base.Padding = value; }
}
-#endif
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Browsable (false)]
string retval = textbox_ctrl.SelectedText;
-#if ONLY_1_1
- // On 1.1, the textbox will return null, combobox returns ""
- if (retval == null && !textbox_ctrl.IsHandleCreated)
- return string.Empty;
-#endif
return retval;
}
set {
return;
}
- // do nothing if value exactly matches text of selected item
- if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
- return;
-
- // find exact match using case-sensitive comparison, and if does
- // not result in any match then use case-insensitive comparison
- int index = FindStringExact (value, -1, false);
- if (index == -1) {
- index = FindStringExact (value, -1, true);
- }
- if (index != -1) {
- SelectedIndex = index;
- return;
+ // don't set the index if value exactly matches text of selected item
+ if (SelectedItem == null || string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) != 0)
+ {
+ // find exact match using case-sensitive comparison, and if does
+ // not result in any match then use case-insensitive comparison
+ int index = FindStringExact (value, -1, false);
+ if (index == -1) {
+ index = FindStringExact (value, -1, true);
+ }
+ if (index != -1) {
+ SelectedIndex = index;
+ return;
+ }
}
- // set directly the passed value, since we already know it's not matching any item
+ // set directly the passed value
if (dropdown_style != ComboBoxStyle.DropDownList)
textbox_ctrl.Text = value;
}
#endregion UIA Framework Properties
#region Public Methods
-#if NET_2_0
[Obsolete ("This method has been deprecated")]
-#endif
protected virtual void AddItemsCore (object[] value)
{
suspend_ctrlupdate = true;
}
-#if NET_2_0
protected override AccessibleObject CreateAccessibilityInstance ()
{
return base.CreateAccessibilityInstance ();
{
base.CreateHandle ();
}
-#endif
protected override void Dispose (bool disposing)
{
if (s == null || Items.Count == 0)
return -1;
-#if NET_2_0
if (startIndex < -1 || startIndex >= Items.Count)
-#else
- if (startIndex < -1 || startIndex >= Items.Count - 1)
-#endif
throw new ArgumentOutOfRangeException ("startIndex");
int i = startIndex;
-#if NET_2_0
if (i == (Items.Count - 1))
i = -1;
-#endif
do {
i++;
if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
if (s == null || Items.Count == 0)
return -1;
-#if NET_2_0
if (startIndex < -1 || startIndex >= Items.Count)
-#else
- if (startIndex < -1 || startIndex >= Items.Count - 1)
-#endif
throw new ArgumentOutOfRangeException ("startIndex");
int i = startIndex;
-#if NET_2_0
if (i == (Items.Count - 1))
i = -1;
-#endif
do {
i++;
if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
base.OnDataSourceChanged (e);
BindDataItems ();
+ /**
+ ** This 'Debugger.IsAttached' hack is here because of
+ ** Xamarin Bug #2234, which noted that when changing
+ ** the DataSource, in Windows exceptions are eaten
+ ** when SelectedIndexChanged is fired. However, when
+ ** the debugger is running (i.e. in MonoDevelop), we
+ ** want to be alerted of exceptions.
+ **/
+
+ if (Debugger.IsAttached) {
+ SetSelectedIndex ();
+ } else {
+ try {
+ SetSelectedIndex ();
+ } catch {
+ //ignore exceptions here per
+ //bug 2234
+ }
+ }
+ }
+
+ private void SetSelectedIndex ()
+ {
if (DataSource == null || DataManager == null) {
SelectedIndex = -1;
}
eh (this, e);
}
-#if NET_2_0
protected virtual void OnDropDownClosed (EventArgs e)
{
EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
if (eh != null)
eh (this, e);
}
-#endif
protected virtual void OnDropDownStyleChanged (EventArgs e)
{
textbox_ctrl.ActivateCaret (false);
textbox_ctrl.ShowSelection = false;
textbox_ctrl.SelectionLength = 0;
-#if NET_2_0
textbox_ctrl.HideAutoCompleteList ();
-#endif
}
base.OnLostFocus (e);
item_heights.Remove (Items [index]);
}
-#if NET_2_0
protected override void RefreshItems ()
{
for (int i = 0; i < Items.Count; i++) {
base.OnTextChanged (e);
}
-#if NET_2_0
protected virtual void OnTextUpdate (EventArgs e)
{
EventHandler eh = (EventHandler) Events [TextUpdateEvent];
if (eh != null)
eh (this, e);
}
-#endif
protected override void OnMouseLeave (EventArgs e)
{
-#if NET_2_0
if (flat_style == FlatStyle.Popup)
Invalidate ();
-#endif
base.OnMouseLeave (e);
}
protected override void OnMouseEnter (EventArgs e)
{
-#if NET_2_0
if (flat_style == FlatStyle.Popup)
Invalidate ();
-#endif
base.OnMouseEnter (e);
}
-#endif
-#if NET_2_0
protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
{
base.ScaleControl (factor, specified);
}
-#endif
public void Select (int start, int length)
{
case Msg.WM_KEYUP:
case Msg.WM_KEYDOWN:
Keys keys = (Keys) m.WParam.ToInt32 ();
-#if NET_2_0
// Don't pass the message to base if auto complete is being used and available.
if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
return;
}
-#endif
if (keys == Keys.Up || keys == Keys.Down)
break;
goto case Msg.WM_CHAR;
#endregion Public Methods
#region Private Methods
-#if NET_2_0
void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
if(auto_complete_source == AutoCompleteSource.CustomSource) {
//FIXME: handle add, remove and refresh events in AutoComplete algorithm.
}
}
-#endif
internal override bool InternalCapture {
get { return Capture; }
button_area.Y = text_area.Y + border;
button_area.Width = button_width;
button_area.Height = text_area.Height - 2 * border;
-#if NET_2_0
if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
button_area.Inflate (1, 1);
button_area.X += 2;
button_area.Width -= 2;
}
-#endif
}
if (button_area != prev_button_area) {
FlatStyle style = FlatStyle.Standard;
bool is_flat = false;
-#if NET_2_0
style = this.FlatStyle;
is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
-#endif
theme.ComboBoxDrawBackground (this, dc, clip, style);
FindMatchOrSetIndex(SelectedIndex);
-#if NET_2_0
if (textbox_ctrl != null)
textbox_ctrl.HideAutoCompleteList ();
-#endif
if (listbox_ctrl.ShowWindow ())
dropped_down = true;
button_state = ButtonState.Normal;
Invalidate (button_area);
dropped_down = false;
-#if NET_2_0
OnDropDownClosed (EventArgs.Empty);
-#endif
/*
* Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
* this causes the popup to appear under the main form. This is horrible but necessary
listbox_ctrl.Dispose ();
listbox_ctrl = null;
}
-#if NET_2_0
// The auto complete list could have been shown after the listbox,
// so make sure it's hidden.
if (textbox_ctrl != null)
textbox_ctrl.HideAutoCompleteList ();
-#endif
}
private int FindStringCaseInsensitive (string search)
#region UIA Framework Events
-#if NET_2_0
//NOTE:
// We are using Reflection to add/remove internal events.
// Class ListProvider uses the events.
if (eh != null)
eh (owner, args);
}
-#endif
#endregion UIA Framework Events
if (value == null)
throw new ArgumentNullException ("value");
-#if NET_2_0
//UIA Framework event: Item Removed
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
-#endif
object_items[index] = value;
-#if NET_2_0
//UIA Framework event: Item Added
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
-#endif
if (owner.listbox_ctrl != null)
owner.listbox_ctrl.InvalidateItem (index);
if (index == owner.SelectedIndex) {
if (owner.textbox_ctrl == null)
owner.Refresh ();
- else
- owner.textbox_ctrl.SelectedText = value.ToString ();
+ else {
+ owner.textbox_ctrl.Text = value.ToString ();
+ owner.textbox_ctrl.SelectAll ();
+ }
}
}
}
owner.UpdatedItems ();
owner.Refresh ();
-#if NET_2_0
//UIA Framework event: Items list cleared
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
-#endif
}
public bool Contains (object value)
return object_items.Contains (value);
}
-#if NET_2_0
public void CopyTo (object [] destination, int arrayIndex)
{
object_items.CopyTo (destination, arrayIndex);
{
object_items.CopyTo (destination, index);
}
-#else
- public void CopyTo (object [] dest, int arrayIndex)
- {
- object_items.CopyTo (dest, arrayIndex);
- }
-
- void ICollection.CopyTo (Array dest, int index)
- {
- object_items.CopyTo (dest, index);
- }
-#endif
public IEnumerator GetEnumerator ()
{
AddItem (item, false);
else {
object_items.Insert (index, item);
-#if NET_2_0
//UIA Framework event: Item added
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
-#endif
}
owner.EndUpdate (); // Calls UpdatedItems
{
if (value == null)
return;
-
- if (IndexOf (value) == owner.SelectedIndex)
- owner.SelectedIndex = -1;
-
- RemoveAt (IndexOf (value));
+ int index = IndexOf (value);
+ if (index >= 0)
+ RemoveAt (index);
}
public void RemoveAt (int index)
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException ("index");
- if (index == owner.SelectedIndex)
+ if (index < owner.SelectedIndex)
+ --owner.SelectedIndex;
+ else if (index == owner.SelectedIndex)
owner.SelectedIndex = -1;
-#if NET_2_0
object removed = object_items [index];
-#endif
-
object_items.RemoveAt (index);
owner.UpdatedItems ();
-#if NET_2_0
//UIA Framework event: Item removed
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
-#endif
}
#endregion Public Methods
if (index <= owner.selected_index && owner.IsHandleCreated)
owner.selected_index++;
-#if NET_2_0
//UIA Framework event: Item added
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
-#endif
return index;
}
}
object_items.Add (item);
-#if NET_2_0
//UIA Framework event: Item added
OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
-#endif
return object_items.Count - 1;
}
this.owner = owner;
ShowSelection = false;
owner.EnabledChanged += OwnerEnabledChangedHandler;
-#if NET_2_0
owner.LostFocus += OwnerLostFocusHandler;
-#endif
}
void OwnerEnabledChangedHandler (object o, EventArgs args)
ShowSelection = owner.Focused && owner.Enabled;
}
-#if NET_2_0
void OwnerLostFocusHandler (object o, EventArgs args)
{
if (IsAutoCompleteAvailable)
base.OnAutoCompleteValueSelected (args);
owner.Text = Text;
}
-#endif
internal void SetSelectable (bool selectable)
{
document.CaretLostFocus ();
}
-#if NET_2_0
internal override void OnTextUpdate ()
{
base.OnTextUpdate ();
owner.OnTextUpdate (EventArgs.Empty);
}
-#endif
protected override void OnGotFocus (EventArgs e)
{
return owner.Focused;
}
}
-
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing ) {
+ // Prevents corruption of combobox text by disposed object
+ owner.EnabledChanged -= OwnerEnabledChangedHandler;
+ owner.LostFocus -= OwnerLostFocusHandler;
+ }
+ base.Dispose(disposing);
+ }
+
internal override bool ActivateOnShow { get { return false; } }
}
show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
} else {
-#if NET_2_0
if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
height = owner.ItemHeight * visible_items_count;
show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
height = owner.DropDownHeight;
show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
}
-#else
- height = owner.ItemHeight * visible_items_count;
- show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
-#endif
}
}
vscrollbar_ctrl.Dock = DockStyle.Right;
vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
-#if NET_2_0
int large = page_size;
-#else
- int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
-#endif
if (large < 1)
large = 1;
vscrollbar_ctrl.LargeChange = large;
vscrollbar_ctrl.Value = hli;
}
}
-
- Size = new Size (width, height);
- textarea_drawable = ClientRectangle;
- textarea_drawable.Width = width;
- textarea_drawable.Height = height;
-
+
+ var borderWidth = Hwnd.GetBorderWidth (CreateParams);
+ var borderAdjustment = dropdown_style == ComboBoxStyle.Simple ? new Size (0, 0) :
+ new Size (borderWidth.top + borderWidth.bottom, borderWidth.left + borderWidth.right);
+ Size = new Size (width, height + borderAdjustment.Height);
+ textarea_drawable = new Rectangle (ClientRectangle.Location,
+ new Size (width - borderAdjustment.Width, height));
+
if (vscrollbar_ctrl != null && show_scrollbar)
textarea_drawable.Width -= vscrollbar_ctrl.Width;
HighlightedIndex = owner.SelectedIndex;
CalcListBoxArea ();
+ // If the listbox would extend below the screen, move it above the textbox.
+ Rectangle scrn_rect = Screen.FromControl (owner).Bounds;
+ if (this.Location.Y + this.Height >= scrn_rect.Bottom)
+ this.Location = new Point (this.Location.X, this.Location.Y - (this.Height + owner.TextArea.Height));
Show ();
Refresh ();