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 Novell, Inc.
23 // Miguel de Icaza (miguel@novell.com).
28 using System.ComponentModel;
29 using System.Collections;
31 namespace System.Windows.Forms {
33 [DefaultEvent ("SelectedItemChanged")]
34 [DefaultProperty ("Items")]
35 public class DomainUpDown : UpDownBase
40 public class DomainUpDownItemCollection : ArrayList {
43 internal DomainUpDownItemCollection (DomainUpDown owner)
48 public override int Add (object item)
50 int ret = base.Add (item);
54 // Will trigger an update.
55 owner.SelectedIndex = owner.SelectedIndex;
61 public override void Insert (int index, object item)
63 base.Insert (index, item);
66 owner.SelectedIndex = owner.SelectedIndex;
68 if (index == owner.SelectedIndex)
69 owner.UpdateEditText ();
73 public override void Remove (object item)
76 if (Count < owner.SelectedIndex)
77 owner.SelectedIndex -= 1;
80 owner.UpdateEditText ();
84 public override void RemoveAt (int item)
88 if (Count < owner.SelectedIndex)
89 owner.SelectedIndex -= 1;
90 if (owner.SelectedIndex == item)
91 owner.UpdateEditText ();
94 owner.UpdateEditText ();
99 int selected_index = -1;
102 DomainUpDownItemCollection items;
105 #region ISupportInitialize methods
107 public void BeginInit ()
111 public void EndInit ()
117 public event EventHandler SelectedItemChanged;
119 public DomainUpDown () : base () {
120 items = new DomainUpDownItemCollection (this);
123 public override void DownButton ()
126 selected_index %= items.Count;
127 else if (selected_index < items.Count)
133 public override void UpButton ()
137 if (selected_index == -1)
138 selected_index = items.Count-1;
140 if (selected_index > -1)
146 public override void UpdateEditText ()
149 if (selected_index == -1)
152 Text = items [selected_index].ToString ();
156 protected override void OnChanged (object source, EventArgs e)
158 OnSelectedItemChanged (source, e);
161 protected void OnSelectedItemChanged (object source, EventArgs e)
163 if (SelectedItemChanged != null)
164 SelectedItemChanged (source, e);
167 protected override AccessibleObject CreateAccessibilityInstance ()
171 ao = base.CreateAccessibilityInstance ();
172 ao.description = "DomainUpDown";
177 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
179 public DomainUpDownItemCollection Items {
187 public int SelectedIndex {
189 return selected_index;
193 if (value < -1 || value > items.Count)
194 throw new ArgumentException (String.Format ("Boundaries are -1 to {0}", items.Count-1));
196 selected_index = value;
201 [DesignerSerializationVisibility(DesignerSerializationVisibility.Item)]
203 public object SelectedItem {
205 if (selected_index == -1)
208 return items [selected_index];
212 for (int i = 0; i < items.Count; i++)
213 if (items [i] == value){
220 [DefaultValue(false)]
228 // It never returns to unsorted state
238 [DefaultValue (false)]
249 public override string ToString ()
251 return String.Format ("{0} Count={1} Selected={2}", base.ToString (), items.Count, selected_index);