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 [DefaultValue (ComboBoxStyle.DropDown)]
60 public ComboBoxStyle DropDownStyle {
61 get { return this.ComboBox.DropDownStyle; }
62 set { this.ComboBox.DropDownStyle = value; }
65 public int DropDownWidth {
66 get { return this.ComboBox.DropDownWidth; }
67 set { this.ComboBox.DropDownWidth = value; }
71 public bool DroppedDown {
72 get { return this.ComboBox.DroppedDown; }
73 set { this.ComboBox.DroppedDown = value; }
78 public bool IntegralHeight {
79 get { return this.ComboBox.IntegralHeight; }
80 set { this.ComboBox.IntegralHeight = value; }
84 public ComboBox.ObjectCollection Items {
85 get { return this.ComboBox.Items; }
90 public int MaxDropDownItems {
91 get { return this.ComboBox.MaxDropDownItems; }
92 set { this.ComboBox.MaxDropDownItems = value; }
97 public int MaxLength {
98 get { return this.ComboBox.MaxLength; }
99 set { this.ComboBox.MaxLength = value; }
103 public int SelectedIndex {
104 get { return this.ComboBox.SelectedIndex; }
105 set { this.ComboBox.SelectedIndex = value; }
110 public Object SelectedItem {
111 get { return this.ComboBox.SelectedItem; }
112 set { this.ComboBox.SelectedItem = value; }
116 public string SelectedText {
117 get { return this.ComboBox.SelectedText; }
118 set { this.ComboBox.SelectedText = value; }
122 public int SelectionLength {
123 get { return this.ComboBox.SelectionLength; }
124 set { this.ComboBox.SelectionLength = value; }
128 public int SelectionStart {
129 get { return this.ComboBox.SelectionStart; }
130 set { this.ComboBox.SelectionStart = value; }
133 [DefaultValue (false)]
135 get { return this.ComboBox.Sorted; }
136 set { this.ComboBox.Sorted = value; }
140 #region Protected Properties
141 protected internal override Padding DefaultMargin { get { return new Padding (2); } }
142 protected override Size DefaultSize { get { return new Size (100, 22); } }
145 #region Public Methods
146 public void BeginUpdate ()
148 this.ComboBox.BeginUpdate ();
151 public void EndUpdate ()
153 this.ComboBox.EndUpdate ();
156 public int FindString (string s)
158 return this.ComboBox.FindString (s);
161 public int FindString (string s, int startIndex)
163 return this.ComboBox.FindString (s, startIndex);
166 public int FindStringExact (string s)
168 return this.ComboBox.FindStringExact (s);
171 public int FindStringExact (string s, int startIndex)
173 return this.ComboBox.FindStringExact (s, startIndex);
176 public int GetItemHeight (int index)
178 return this.ComboBox.GetItemHeight (index);
181 public override Size GetPreferredSize (Size constrainingSize)
183 return base.GetPreferredSize (constrainingSize);
186 public void Select (int start, int length)
188 this.ComboBox.Select (start, length);
191 public void SelectAll ()
193 this.ComboBox.SelectAll ();
196 public override string ToString ()
198 return this.ComboBox.ToString ();
202 #region Protected Methods
203 protected virtual void OnDropDown (EventArgs e)
205 if (DropDown != null) DropDown (this, e);
208 protected virtual void OnDropDownClosed (EventArgs e)
210 if (DropDownClosed != null) DropDownClosed (this, e);
213 protected virtual void OnDropDownStyleChanged (EventArgs e)
215 if (DropDownStyleChanged != null) DropDownStyleChanged (this, e);
218 protected virtual void OnSelectedIndexChanged (EventArgs e)
220 if (SelectedIndexChanged != null) SelectedIndexChanged (this, e);
223 protected virtual void OnSelectionChangeCommitted (EventArgs e)
227 [MonoTODO ("Needs to hook into DropDownClosed and TextUpdate when ComboBox 2.0 has them.")]
228 protected override void OnSubscribeControlEvents (Control control)
230 base.OnSubscribeControlEvents (control);
232 this.ComboBox.DropDown += new EventHandler (HandleDropDown);
233 this.ComboBox.DropDownStyleChanged += new EventHandler (HandleDropDownStyleChanged);
234 this.ComboBox.SelectedIndexChanged += new EventHandler (HandleSelectedIndexChanged);
237 protected virtual void OnTextUpdate (EventArgs e)
239 if (TextUpdate != null) TextUpdate (this, e);
242 protected override void OnUnsubscribeControlEvents (Control control)
244 base.OnUnsubscribeControlEvents (control);
248 #region Public Events
250 [EditorBrowsable (EditorBrowsableState.Never)]
251 public event EventHandler DoubleClick;
252 public event EventHandler DropDown;
253 public event EventHandler DropDownClosed;
254 public event EventHandler DropDownStyleChanged;
255 public event EventHandler SelectedIndexChanged;
256 public event EventHandler TextUpdate;
259 #region Private Methods
260 private void HandleDropDown (object sender, EventArgs e)
265 private void HandleDropDownStyleChanged (object sender, EventArgs e)
267 OnDisplayStyleChanged (e);
270 private void HandleSelectedIndexChanged (object sender, EventArgs e)
272 OnSelectedIndexChanged (e);
276 private class ToolStripComboBoxControl : ComboBox
278 public ToolStripComboBoxControl () : base ()
280 this.border_style = BorderStyle.None;