private DrawMode draw_mode = DrawMode.Normal;
private ComboBoxStyle dropdown_style = (ComboBoxStyle)(-1);
private int dropdown_width = -1;
- private object selected_item = null;
+ private int selected_index = -1;
internal ObjectCollection items = null;
private bool suspend_ctrlupdate;
private int maxdrop_items = 8;
if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
textbox_ctrl = new ComboTextBox (this);
+ object selected_item = SelectedItem;
if (selected_item != null)
textbox_ctrl.Text = GetItemText (selected_item);
textbox_ctrl.BorderStyle = BorderStyle.None;
textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
+ textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
if (IsHandleCreated == true) {
Controls.AddImplicit (textbox_ctrl);
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public override int SelectedIndex {
- get { return Items.IndexOf (selected_item); }
+ get { return selected_index; }
set {
if (value <= -2 || value >= Items.Count)
throw new ArgumentOutOfRangeException ("Index of out range");
+ selected_index = value;
- object item = null;
- if (value != -1)
- item = Items [value];
-
- SelectedItem = item;
- }
- }
-
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [Bindable(true)]
- public object SelectedItem {
- get { return selected_item; }
- set {
- if (selected_item == value)
- return;
-
- selected_item = value;
-
if (dropdown_style != ComboBoxStyle.DropDownList) {
- if (selected_item == null)
+ if (value == -1)
SetControlText("");
else
- SetControlText (GetItemText (selected_item));
+ SetControlText (GetItemText (Items [value]));
}
OnSelectedValueChanged (new EventArgs ());
Invalidate ();
if (listbox_ctrl != null)
- listbox_ctrl.HighlightedItem = value;
+ listbox_ctrl.HighlightedIndex = value;
+ }
+ }
+
+ [Browsable (false)]
+ [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+ [Bindable(true)]
+ public object SelectedItem {
+ get { return selected_index == -1 ? null : Items [selected_index]; }
+ set {
+ object item = selected_index == -1 ? null : Items [selected_index];
+ if (item == value)
+ return;
+
+ SelectedIndex = Items.IndexOf (value);
}
}
return;
sorted = value;
+ SelectedIndex = -1;
+ if (sorted) {
+ Items.Sort ();
+ Layout ();
+ }
}
}
private void CreateComboListBox ()
{
listbox_ctrl = new ComboListBox (this);
- if (selected_item != null)
- listbox_ctrl.HighlightedItem = selected_item;
+ listbox_ctrl.HighlightedIndex = SelectedIndex;
}
internal void Draw (Rectangle clip, Graphics dc)
Draw (ClientRectangle, pevent.Graphics);
}
+ private void OnTextBoxClick (object sender, EventArgs e)
+ {
+ OnClick (e);
+ }
+
private void OnTextChangedEdit (object sender, EventArgs e)
{
if (process_textchanged_event == false)
return;
+ OnTextChanged (EventArgs.Empty);
+
int item = FindStringCaseInsensitive (textbox_ctrl.Text);
if (item == -1)
if (listbox_ctrl != null) {
listbox_ctrl.SetTopItem (item);
- listbox_ctrl.HighlightedItem = Items [item];
+ listbox_ctrl.HighlightedIndex = item;
}
base.Text = textbox_ctrl.Text;
void UpdateBounds ()
{
if (requested_height != -1)
- SetBoundsCore (0, 0, 0, requested_height, BoundsSpecified.Height);
+ SetBounds (0, 0, 0, requested_height, BoundsSpecified.Height);
}
private void UpdatedItems ()
set {
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException ("Index of out range");
+ if (value == null)
+ throw new ArgumentNullException ("value");
object_items[index] = value;
+ 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 ();
+ }
}
}
#endregion Public Properties
- #region Private Properties
- internal ArrayList ObjectItems {
- get { return object_items;}
- set {
- object_items = value;
- }
- }
-
- #endregion Private Properties
-
#region Public Methods
public int Add (object item)
{
public void AddRange (object[] items)
{
+ if (items == null)
+ throw new ArgumentNullException ("items");
+
foreach (object mi in items)
AddItem (mi);
public void Clear ()
{
- owner.selected_item = null;
+ owner.selected_index = -1;
object_items.Clear ();
owner.UpdatedItems ();
owner.Refresh ();
public bool Contains (object obj)
{
+ if (obj == null)
+ throw new ArgumentNullException ("obj");
+
return object_items.Contains (obj);
}
public int IndexOf (object value)
{
+ if (value == null)
+ throw new ArgumentNullException ("value");
+
return object_items.IndexOf (value);
}
{
if (index < 0 || index > Count)
throw new ArgumentOutOfRangeException ("Index of out range");
+ if (item == null)
+ throw new ArgumentNullException ("item");
owner.BeginUpdate ();
- object_items.Insert (index, item);
+ if (owner.Sorted)
+ AddItem (item);
+ else
+ object_items.Insert (index, item);
owner.EndUpdate (); // Calls UpdatedItems
}
public void Remove (object value)
{
+ if (value == null)
+ return;
+
if (IndexOf (value) == owner.SelectedIndex)
- owner.SelectedItem = null;
+ owner.SelectedIndex = -1;
RemoveAt (IndexOf (value));
-
}
public void RemoveAt (int index)
throw new ArgumentOutOfRangeException ("Index of out range");
if (index == owner.SelectedIndex)
- owner.SelectedItem = null;
+ owner.SelectedIndex = -1;
object_items.RemoveAt (index);
owner.UpdatedItems ();
#region Private Methods
private int AddItem (object item)
{
- int cnt = object_items.Count;
+ if (item == null)
+ throw new ArgumentNullException ("item");
+
+ if (owner.Sorted) {
+ int index = 0;
+ foreach (string s in object_items) {
+ if (String.Compare (item as String, s) < 0) {
+ object_items.Insert (index, item);
+ return index;
+ }
+ index++;
+ }
+ }
object_items.Add (item);
- return cnt;
+ return object_items.Count - 1;
}
internal void AddRange (IList items)
owner.UpdatedItems ();
}
+ internal void Sort ()
+ {
+ object_items.Sort ();
+ }
+
#endregion Private Methods
}
}
}
+ int highlighted_index = -1;
+
public int HighlightedIndex {
- get { return owner.Items.IndexOf (highlighted_item); }
+ get { return highlighted_index; }
set {
- object item = null;
- if (value != -1)
- item = owner.Items [value];
- HighlightedItem = item;
- }
- }
-
- object highlighted_item = null;
-
- public object HighlightedItem {
- get { return highlighted_item; }
- set {
- if (highlighted_item == value)
+ if (highlighted_index == value)
return;
-
- int index = owner.Items.IndexOf (highlighted_item);
- if (index != -1)
- Invalidate (GetItemDisplayRectangle (index, top_item));
- highlighted_item = value;
- index = owner.Items.IndexOf (highlighted_item);
- if (index != -1)
- Invalidate (GetItemDisplayRectangle (index, top_item));
+
+ if (highlighted_index != -1)
+ Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
+ highlighted_index = value;
+ if (highlighted_index != -1)
+ Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
}
}
-
+
private Rectangle GetItemDisplayRectangle (int index, int top_index)
{
if (index < 0 || index >= owner.Items.Count)
return -1;
}
+ public void InvalidateItem (int index)
+ {
+ if (Visible)
+ Invalidate (GetItemDisplayRectangle (index, top_item));
+ }
+
private int LastVisibleItem ()
{
Rectangle item_rect;
return;
top_item = item;
UpdateLastVisibleItem ();
- Refresh ();
+ Invalidate ();
}
protected override void OnMouseDown (MouseEventArgs e)
} else {
owner.OnSelectionChangeCommitted (new EventArgs ());
owner.SelectedIndex = index;
- HighlightedIndex = index;
HideWindow ();
}
top_item = vscrollbar_ctrl.Value;
UpdateLastVisibleItem ();
- Refresh ();
+ Invalidate ();
}
protected override void WndProc(ref Message m) {