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 ())
42 // The default size of a new ToolStripComboBox doesn't seem
44 Size = new Size (121, 21);
47 [EditorBrowsable (EditorBrowsableState.Never)]
48 public ToolStripComboBox (Control c) : base (c)
50 throw new NotSupportedException ();
53 public ToolStripComboBox (string name) : this ()
59 #region Public Properties
62 [EditorBrowsable (EditorBrowsableState.Always)]
63 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
64 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
65 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
66 public AutoCompleteStringCollection AutoCompleteCustomSource {
67 get { return ComboBox.AutoCompleteCustomSource; }
68 set { ComboBox.AutoCompleteCustomSource = value; }
72 [DefaultValue (AutoCompleteMode.None)]
73 [EditorBrowsable (EditorBrowsableState.Always)]
74 public AutoCompleteMode AutoCompleteMode {
75 get { return ComboBox.AutoCompleteMode; }
76 set { ComboBox.AutoCompleteMode = value; }
80 [DefaultValue (AutoCompleteSource.None)]
81 [EditorBrowsable (EditorBrowsableState.Always)]
82 public AutoCompleteSource AutoCompleteSource {
83 get { return ComboBox.AutoCompleteSource; }
84 set { ComboBox.AutoCompleteSource = value; }
88 [EditorBrowsable (EditorBrowsableState.Never)]
89 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
90 public override Image BackgroundImage {
91 get { return base.BackgroundImage; }
92 set { base.BackgroundImage = value; }
96 [EditorBrowsable (EditorBrowsableState.Never)]
97 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
98 public override ImageLayout BackgroundImageLayout {
99 get { return base.BackgroundImageLayout; }
100 set { base.BackgroundImageLayout = value; }
104 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
105 public ComboBox ComboBox {
106 get { return (ComboBox)base.Control; }
111 [EditorBrowsable (EditorBrowsableState.Always)]
112 public int DropDownHeight {
113 get { return this.ComboBox.DropDownHeight; }
114 set { this.ComboBox.DropDownHeight = value; }
117 [DefaultValue (ComboBoxStyle.DropDown)]
118 [RefreshProperties (RefreshProperties.Repaint)]
119 public ComboBoxStyle DropDownStyle {
120 get { return this.ComboBox.DropDownStyle; }
121 set { this.ComboBox.DropDownStyle = value; }
124 public int DropDownWidth {
125 get { return this.ComboBox.DropDownWidth; }
126 set { this.ComboBox.DropDownWidth = value; }
130 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
131 public bool DroppedDown {
132 get { return this.ComboBox.DroppedDown; }
133 set { this.ComboBox.DroppedDown = value; }
136 [LocalizableAttribute (true)]
137 [DefaultValue (FlatStyle.Popup)]
138 public FlatStyle FlatStyle {
139 get { return ComboBox.FlatStyle; }
140 set { ComboBox.FlatStyle = value; }
144 [DefaultValue (true)]
145 public bool IntegralHeight {
146 get { return this.ComboBox.IntegralHeight; }
147 set { this.ComboBox.IntegralHeight = value; }
151 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
152 [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
153 public ComboBox.ObjectCollection Items {
154 get { return this.ComboBox.Items; }
159 public int MaxDropDownItems {
160 get { return this.ComboBox.MaxDropDownItems; }
161 set { this.ComboBox.MaxDropDownItems = value; }
166 public int MaxLength {
167 get { return this.ComboBox.MaxLength; }
168 set { this.ComboBox.MaxLength = value; }
172 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173 public int SelectedIndex {
174 get { return this.ComboBox.SelectedIndex; }
176 this.ComboBox.SelectedIndex = value;
178 if (this.ComboBox.SelectedIndex >= 0)
179 Text = Items [value].ToString ();
185 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
186 public Object SelectedItem {
187 get { return this.ComboBox.SelectedItem; }
188 set { this.ComboBox.SelectedItem = value; }
192 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
193 public string SelectedText {
194 get { return this.ComboBox.SelectedText; }
195 set { this.ComboBox.SelectedText = value; }
199 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
200 public int SelectionLength {
201 get { return this.ComboBox.SelectionLength; }
202 set { this.ComboBox.SelectionLength = value; }
206 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
207 public int SelectionStart {
208 get { return this.ComboBox.SelectionStart; }
209 set { this.ComboBox.SelectionStart = value; }
212 [DefaultValue (false)]
214 get { return this.ComboBox.Sorted; }
215 set { this.ComboBox.Sorted = value; }
219 #region Protected Properties
220 protected internal override Padding DefaultMargin { get { return new Padding (1, 0, 1, 0); } }
221 protected override Size DefaultSize { get { return new Size (100, 22); } }
224 #region Public Methods
225 public void BeginUpdate ()
227 this.ComboBox.BeginUpdate ();
230 public void EndUpdate ()
232 this.ComboBox.EndUpdate ();
235 public int FindString (string s)
237 return this.ComboBox.FindString (s);
240 public int FindString (string s, int startIndex)
242 return this.ComboBox.FindString (s, startIndex);
245 public int FindStringExact (string s)
247 return this.ComboBox.FindStringExact (s);
250 public int FindStringExact (string s, int startIndex)
252 return this.ComboBox.FindStringExact (s, startIndex);
255 public int GetItemHeight (int index)
257 return this.ComboBox.GetItemHeight (index);
260 public override Size GetPreferredSize (Size constrainingSize)
262 return base.GetPreferredSize (constrainingSize);
265 public void Select (int start, int length)
267 this.ComboBox.Select (start, length);
270 public void SelectAll ()
272 this.ComboBox.SelectAll ();
275 public override string ToString ()
277 return this.ComboBox.ToString ();
281 #region Protected Methods
282 protected virtual void OnDropDown (EventArgs e)
284 EventHandler eh = (EventHandler)(Events [DropDownEvent]);
289 protected virtual void OnDropDownClosed (EventArgs e)
291 EventHandler eh = (EventHandler)(Events [DropDownClosedEvent]);
296 protected virtual void OnDropDownStyleChanged (EventArgs e)
298 EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
303 protected virtual void OnSelectedIndexChanged (EventArgs e)
305 EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
310 protected virtual void OnSelectionChangeCommitted (EventArgs e)
314 protected override void OnSubscribeControlEvents (Control control)
316 base.OnSubscribeControlEvents (control);
318 this.ComboBox.DropDown += new EventHandler (HandleDropDown);
319 this.ComboBox.DropDownClosed += new EventHandler(HandleDropDownClosed);
320 this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
321 this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
322 this.ComboBox.TextChanged += new EventHandler (HandleTextChanged);
323 this.ComboBox.TextUpdate += new EventHandler (HandleTextUpdate);
326 protected virtual void OnTextUpdate (EventArgs e)
328 EventHandler eh = (EventHandler)(Events [TextUpdateEvent]);
333 protected override void OnUnsubscribeControlEvents (Control control)
335 base.OnUnsubscribeControlEvents (control);
339 #region Public Events
340 static object DropDownEvent = new object ();
341 static object DropDownClosedEvent = new object ();
342 static object DropDownStyleChangedEvent = new object ();
343 static object SelectedIndexChangedEvent = new object ();
344 static object TextUpdateEvent = new object ();
347 [EditorBrowsable (EditorBrowsableState.Never)]
348 public new event EventHandler DoubleClick {
349 add { base.DoubleClick += value; }
350 remove { base.DoubleClick -= value; }
353 public event EventHandler DropDown {
354 add { Events.AddHandler (DropDownEvent, value); }
355 remove { Events.RemoveHandler (DropDownEvent, value); }
358 public event EventHandler DropDownClosed {
359 add { Events.AddHandler (DropDownClosedEvent, value); }
360 remove { Events.RemoveHandler (DropDownClosedEvent, value); }
363 public event EventHandler DropDownStyleChanged {
364 add { Events.AddHandler (DropDownStyleChangedEvent, value); }
365 remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
368 public event EventHandler SelectedIndexChanged {
369 add { Events.AddHandler (SelectedIndexChangedEvent, value); }
370 remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
373 public event EventHandler TextUpdate {
374 add { Events.AddHandler (TextUpdateEvent, value); }
375 remove { Events.RemoveHandler (TextUpdateEvent, value); }
379 #region Private Methods
380 private void HandleDropDown (object sender, EventArgs e)
385 private void HandleDropDownClosed (object sender, EventArgs e)
387 OnDropDownClosed (e);
390 private void HandleDropDownStyleChanged (object sender, EventArgs e)
392 OnDropDownStyleChanged (e);
395 private void HandleSelectedIndexChanged (object sender, EventArgs e)
397 OnSelectedIndexChanged (e);
400 private void HandleTextChanged (object sender, EventArgs e)
405 private void HandleTextUpdate (object sender, EventArgs e)
411 private class ToolStripComboBoxControl : ComboBox
413 public ToolStripComboBoxControl () : base ()
415 this.border_style = BorderStyle.None;
416 this.FlatStyle = FlatStyle.Popup;