2 // ToolStripComboBox.cs
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.ComponentModel;
31 using System.Windows.Forms.Design;
33 namespace System.Windows.Forms
35 [DefaultProperty ("Items")]
36 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.MenuStrip | ToolStripItemDesignerAvailability.ContextMenuStrip)]
37 public class ToolStripComboBox : ToolStripControlHost
39 #region Public Constructors
40 public ToolStripComboBox () : base (new ToolStripComboBoxControl ())
44 [EditorBrowsable (EditorBrowsableState.Never)]
45 public ToolStripComboBox (Control c) : base (c)
47 throw new NotSupportedException ();
50 public ToolStripComboBox (string name) : this ()
56 #region Public Properties
59 [EditorBrowsable (EditorBrowsableState.Always)]
60 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
61 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
62 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
63 public AutoCompleteStringCollection AutoCompleteCustomSource {
64 get { return ComboBox.AutoCompleteCustomSource; }
65 set { ComboBox.AutoCompleteCustomSource = value; }
69 [DefaultValue (AutoCompleteMode.None)]
70 [EditorBrowsable (EditorBrowsableState.Always)]
71 public AutoCompleteMode AutoCompleteMode {
72 get { return ComboBox.AutoCompleteMode; }
73 set { ComboBox.AutoCompleteMode = value; }
77 [DefaultValue (AutoCompleteSource.None)]
78 [EditorBrowsable (EditorBrowsableState.Always)]
79 public AutoCompleteSource AutoCompleteSource {
80 get { return ComboBox.AutoCompleteSource; }
81 set { ComboBox.AutoCompleteSource = value; }
85 [EditorBrowsable (EditorBrowsableState.Never)]
86 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87 public override Image BackgroundImage {
88 get { return base.BackgroundImage; }
89 set { base.BackgroundImage = value; }
93 [EditorBrowsable (EditorBrowsableState.Never)]
94 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
95 public override ImageLayout BackgroundImageLayout {
96 get { return base.BackgroundImageLayout; }
97 set { base.BackgroundImageLayout = value; }
101 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102 public ComboBox ComboBox {
103 get { return (ComboBox)base.Control; }
108 [EditorBrowsable (EditorBrowsableState.Always)]
109 public int DropDownHeight {
110 get { return this.ComboBox.DropDownHeight; }
111 set { this.ComboBox.DropDownHeight = value; }
114 [DefaultValue (ComboBoxStyle.DropDown)]
115 [RefreshProperties (RefreshProperties.Repaint)]
116 public ComboBoxStyle DropDownStyle {
117 get { return this.ComboBox.DropDownStyle; }
118 set { this.ComboBox.DropDownStyle = value; }
121 public int DropDownWidth {
122 get { return this.ComboBox.DropDownWidth; }
123 set { this.ComboBox.DropDownWidth = value; }
127 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
128 public bool DroppedDown {
129 get { return this.ComboBox.DroppedDown; }
130 set { this.ComboBox.DroppedDown = value; }
133 [LocalizableAttribute (true)]
134 [DefaultValue (FlatStyle.Popup)]
135 public FlatStyle FlatStyle {
136 get { return ComboBox.FlatStyle; }
137 set { ComboBox.FlatStyle = value; }
141 [DefaultValue (true)]
142 public bool IntegralHeight {
143 get { return this.ComboBox.IntegralHeight; }
144 set { this.ComboBox.IntegralHeight = value; }
148 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
149 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
150 public ComboBox.ObjectCollection Items {
151 get { return this.ComboBox.Items; }
156 public int MaxDropDownItems {
157 get { return this.ComboBox.MaxDropDownItems; }
158 set { this.ComboBox.MaxDropDownItems = value; }
163 public int MaxLength {
164 get { return this.ComboBox.MaxLength; }
165 set { this.ComboBox.MaxLength = value; }
169 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
170 public int SelectedIndex {
171 get { return this.ComboBox.SelectedIndex; }
173 this.ComboBox.SelectedIndex = value;
175 if (this.ComboBox.SelectedIndex >= 0)
176 Text = Items [value].ToString ();
182 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
183 public Object SelectedItem {
184 get { return this.ComboBox.SelectedItem; }
185 set { this.ComboBox.SelectedItem = value; }
189 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
190 public string SelectedText {
191 get { return this.ComboBox.SelectedText; }
192 set { this.ComboBox.SelectedText = value; }
196 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
197 public int SelectionLength {
198 get { return this.ComboBox.SelectionLength; }
199 set { this.ComboBox.SelectionLength = value; }
203 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
204 public int SelectionStart {
205 get { return this.ComboBox.SelectionStart; }
206 set { this.ComboBox.SelectionStart = value; }
209 [DefaultValue (false)]
211 get { return this.ComboBox.Sorted; }
212 set { this.ComboBox.Sorted = value; }
216 #region Protected Properties
217 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
218 protected override Size DefaultSize { get { return new Size (100, 22); } }
221 #region Public Methods
222 public void BeginUpdate ()
224 this.ComboBox.BeginUpdate ();
227 public void EndUpdate ()
229 this.ComboBox.EndUpdate ();
232 public int FindString (string s)
234 return this.ComboBox.FindString (s);
237 public int FindString (string s, int startIndex)
239 return this.ComboBox.FindString (s, startIndex);
242 public int FindStringExact (string s)
244 return this.ComboBox.FindStringExact (s);
247 public int FindStringExact (string s, int startIndex)
249 return this.ComboBox.FindStringExact (s, startIndex);
252 public int GetItemHeight (int index)
254 return this.ComboBox.GetItemHeight (index);
257 public override Size GetPreferredSize (Size constrainingSize)
259 return base.GetPreferredSize (constrainingSize);
262 public void Select (int start, int length)
264 this.ComboBox.Select (start, length);
267 public void SelectAll ()
269 this.ComboBox.SelectAll ();
272 public override string ToString ()
274 return this.ComboBox.ToString ();
278 #region Protected Methods
279 protected virtual void OnDropDown (EventArgs e)
281 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
286 protected virtual void OnDropDownClosed (EventArgs e)
288 EventHandler eh = (EventHandler)(Events [DropDownClosedEvent]);
293 protected virtual void OnDropDownStyleChanged (EventArgs e)
295 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
300 protected virtual void OnSelectedIndexChanged (EventArgs e)
302 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
307 protected virtual void OnSelectionChangeCommitted (EventArgs e)
311 protected override void OnSubscribeControlEvents (Control control)
313 base.OnSubscribeControlEvents (control);
315 this.ComboBox.DropDown += new EventHandler (HandleDropDown);
316 this.ComboBox.DropDownClosed += new EventHandler(HandleDropDownClosed);
317 this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
318 this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
319 this.ComboBox.TextUpdate += new EventHandler(HandleTextUpdate);
322 protected virtual void OnTextUpdate (EventArgs e)
324 EventHandler eh = (EventHandler)(Events [TextUpdateEvent]);
329 protected override void OnUnsubscribeControlEvents (Control control)
331 base.OnUnsubscribeControlEvents (control);
335 #region Public Events
336 static object DropDownEvent = new object ();
337 static object DropDownClosedEvent = new object ();
338 static object DropDownStyleChangedEvent = new object ();
339 static object SelectedIndexChangedEvent = new object ();
340 static object TextUpdateEvent = new object ();
343 [EditorBrowsable (EditorBrowsableState.Never)]
344 public new event EventHandler DoubleClick {
345 add { base.DoubleClick += value; }
346 remove { base.DoubleClick -= value; }
349 public event EventHandler DropDown {
350 add { Events.AddHandler (DropDownEvent, value); }
351 remove { Events.RemoveHandler (DropDownEvent, value); }
354 public event EventHandler DropDownClosed {
355 add { Events.AddHandler (DropDownClosedEvent, value); }
356 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
359 public event EventHandler DropDownStyleChanged {
360 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
361 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
364 public event EventHandler SelectedIndexChanged {
365 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
366 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
369 public event EventHandler TextUpdate {
370 add { Events.AddHandler (TextUpdateEvent, value); }
371 remove { Events.RemoveHandler (TextUpdateEvent, value); }
375 #region Private Methods
376 private void HandleDropDown (object sender, EventArgs e)
381 private void HandleDropDownClosed (object sender, EventArgs e)
383 OnDropDownClosed (e);
386 private void HandleDropDownStyleChanged (object sender, EventArgs e)
388 OnDropDownStyleChanged (e);
391 private void HandleSelectedIndexChanged (object sender, EventArgs e)
393 OnSelectedIndexChanged (e);
396 private void HandleTextUpdate (object sender, EventArgs e)
402 private class ToolStripComboBoxControl : ComboBox
404 public ToolStripComboBoxControl () : base ()
406 this.border_style = BorderStyle.None;
407 this.FlatStyle = FlatStyle.Popup;