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;
32 namespace System.Windows.Forms
34 [DefaultProperty ("Items")]
35 public class ToolStripComboBox : ToolStripControlHost
37 #region Public Constructors
38 public ToolStripComboBox () : base (new ToolStripComboBoxControl ())
42 public ToolStripComboBox (Control c) : base (c)
44 throw new NotSupportedException ();
47 public ToolStripComboBox (string name) : this ()
49 base.Control.Name = name;
53 #region Public Properties
55 public ComboBox ComboBox {
56 get { return (ComboBox)base.Control; }
59 [MonoTODO ("Stub, will not actually affect anything.")]
60 public int DropDownHeight {
65 [DefaultValue (ComboBoxStyle.DropDown)]
66 public ComboBoxStyle DropDownStyle {
67 get { return this.ComboBox.DropDownStyle; }
68 set { this.ComboBox.DropDownStyle = value; }
71 public int DropDownWidth {
72 get { return this.ComboBox.DropDownWidth; }
73 set { this.ComboBox.DropDownWidth = value; }
77 public bool DroppedDown {
78 get { return this.ComboBox.DroppedDown; }
79 set { this.ComboBox.DroppedDown = value; }
84 public bool IntegralHeight {
85 get { return this.ComboBox.IntegralHeight; }
86 set { this.ComboBox.IntegralHeight = value; }
90 public ComboBox.ObjectCollection Items {
91 get { return this.ComboBox.Items; }
96 public int MaxDropDownItems {
97 get { return this.ComboBox.MaxDropDownItems; }
98 set { this.ComboBox.MaxDropDownItems = value; }
103 public int MaxLength {
104 get { return this.ComboBox.MaxLength; }
105 set { this.ComboBox.MaxLength = value; }
109 public int SelectedIndex {
110 get { return this.ComboBox.SelectedIndex; }
111 set { this.ComboBox.SelectedIndex = value; }
116 public Object SelectedItem {
117 get { return this.ComboBox.SelectedItem; }
118 set { this.ComboBox.SelectedItem = value; }
122 public string SelectedText {
123 get { return this.ComboBox.SelectedText; }
124 set { this.ComboBox.SelectedText = value; }
128 public int SelectionLength {
129 get { return this.ComboBox.SelectionLength; }
130 set { this.ComboBox.SelectionLength = value; }
134 public int SelectionStart {
135 get { return this.ComboBox.SelectionStart; }
136 set { this.ComboBox.SelectionStart = value; }
139 [DefaultValue (false)]
141 get { return this.ComboBox.Sorted; }
142 set { this.ComboBox.Sorted = value; }
146 #region Protected Properties
147 protected internal override Padding DefaultMargin { get { return new Padding (2); } }
148 protected override Size DefaultSize { get { return new Size (100, 22); } }
151 #region Public Methods
152 public void BeginUpdate ()
154 this.ComboBox.BeginUpdate ();
157 public void EndUpdate ()
159 this.ComboBox.EndUpdate ();
162 public int FindString (string s)
164 return this.ComboBox.FindString (s);
167 public int FindString (string s, int startIndex)
169 return this.ComboBox.FindString (s, startIndex);
172 public int FindStringExact (string s)
174 return this.ComboBox.FindStringExact (s);
177 public int FindStringExact (string s, int startIndex)
179 return this.ComboBox.FindStringExact (s, startIndex);
182 public int GetItemHeight (int index)
184 return this.ComboBox.GetItemHeight (index);
187 public override Size GetPreferredSize (Size constrainingSize)
189 return base.GetPreferredSize (constrainingSize);
192 public void Select (int start, int length)
194 this.ComboBox.Select (start, length);
197 public void SelectAll ()
199 this.ComboBox.SelectAll ();
202 public override string ToString ()
204 return this.ComboBox.ToString ();
208 #region Protected Methods
209 protected virtual void OnDropDown (EventArgs e)
211 if (DropDown != null) DropDown (this, e);
214 protected virtual void OnDropDownClosed (EventArgs e)
216 if (DropDownClosed != null) DropDownClosed (this, e);
219 protected virtual void OnDropDownStyleChanged (EventArgs e)
221 if (DropDownStyleChanged != null) DropDownStyleChanged (this, e);
224 protected virtual void OnSelectedIndexChanged (EventArgs e)
226 if (SelectedIndexChanged != null) SelectedIndexChanged (this, e);
229 protected virtual void OnSelectionChangeCommitted (EventArgs e)
233 [MonoTODO ("Needs to hook into DropDownClosed and TextUpdate when ComboBox 2.0 has them.")]
234 protected override void OnSubscribeControlEvents (Control control)
236 base.OnSubscribeControlEvents (control);
238 this.ComboBox.DropDown += new EventHandler (HandleDropDown);
239 this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
240 this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
243 protected virtual void OnTextUpdate (EventArgs e)
245 if (TextUpdate != null) TextUpdate (this, e);
248 protected override void OnUnsubscribeControlEvents (Control control)
250 base.OnUnsubscribeControlEvents (control);
254 #region Public Events
256 [EditorBrowsable (EditorBrowsableState.Never)]
257 public new event EventHandler DoubleClick;
258 public event EventHandler DropDown;
259 public event EventHandler DropDownClosed;
260 public event EventHandler DropDownStyleChanged;
261 public event EventHandler SelectedIndexChanged;
262 public event EventHandler TextUpdate;
265 #region Private Methods
266 private void HandleDropDown (object sender, EventArgs e)
271 private void HandleDropDownStyleChanged (object sender, EventArgs e)
273 OnDisplayStyleChanged (e);
276 private void HandleSelectedIndexChanged (object sender, EventArgs e)
278 OnSelectedIndexChanged (e);
282 private class ToolStripComboBoxControl : ComboBox
284 public ToolStripComboBoxControl () : base ()
286 this.border_style = BorderStyle.None;