#if FRAMEWORK20 using System; using System.Text; using System.ComponentModel; using System.Drawing; using DevComponents.Editors; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Collections; using System.Drawing.Design; using System.Security.Permissions; namespace DevComponents.DotNetBar.Controls { /// /// Represents single line text box control with the drop down button to display custom control on popup and additional custom buttons. /// [ToolboxBitmap(typeof(TextBoxDropDown), "Controls.TextBoxDropDown.ico"), ToolboxItem(true), DefaultProperty("Text"), DefaultBindingProperty("Text"), DefaultEvent("TextChanged"), Designer("DevComponents.DotNetBar.Design.TextBoxDropDownDesigner, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf")] public class TextBoxDropDown : PopupItemControl, IInputButtonControl, ICommandSource { #region Private Variables private TextBoxX _TextBox = null; private ButtonItem _PopupItem = null; private static string _DropDownItemContainerName = "sysPopupItemContainer"; private static string _DropDownControlContainerName = "sysPopupControlContainer"; private Color _FocusHighlightColor = ColorScheme.GetColor(0xFFFF88); private bool _FocusHighlightEnabled = false; #endregion #region Events /// /// Occurs when Clear button is clicked and allows you to cancel the default action performed by the button. /// public event CancelEventHandler ButtonClearClick; /// /// Occurs when Drop-Down button that shows popup is clicked and allows you to cancel showing of the popup. /// public event CancelEventHandler ButtonDropDownClick; /// /// Occurs when ButtonCustom control is clicked. /// public event EventHandler ButtonCustomClick; /// /// Occurs when ButtonCustom2 control is clicked. /// public event EventHandler ButtonCustom2Click; /// /// Occurs when the text alignment is changed. /// [Description("Occurs when the text alignment is changed.")] public event EventHandler TextAlignChanged; /// /// Occurs when the value of the Modified property has changed. /// public event EventHandler ModifiedChanged; /// /// Occurs before the popup is opened and it allows canceling of popup by setting CancelEventArgs.Cancel=true. /// [Description("Occurs before the popup is opened and it allows canceling of popup by setting CancelEventArgs.Cancel=true.")] public event CancelEventHandler BeforePopupOpen; /// /// Raises BeforeMultiColumnPopupOpen event. /// /// Provides event arguments. protected virtual void OnBeforePopupOpen(CancelEventArgs e) { CancelEventHandler handler = BeforePopupOpen; if (handler != null) handler(this, e); } #endregion #region Constructor /// /// Initializes a new instance of the TextBoxDropDown class. /// public TextBoxDropDown() { this.SetStyle(ControlStyles.Selectable, false); _TextBox = new TextBoxX(); base.BackColor = SystemColors.Window; InitControl(); } private void InitControl() { _BackgroundStyle.SetColorScheme(this.ColorScheme); _BackgroundStyle.StyleChanged += new EventHandler(this.VisualPropertyChanged); _ButtonCustom = new InputButtonSettings(this); _ButtonCustom2 = new InputButtonSettings(this); _ButtonClear = new InputButtonSettings(this); _ButtonDropDown = new InputButtonSettings(this); CreateButtonGroup(); _TextBox.BorderStyle = BorderStyle.None; _TextBox.TextChanged += new EventHandler(TextBoxTextChanged); _TextBox.TextAlignChanged += new EventHandler(TextBoxTextAlignChanged); _TextBox.SizeChanged += new EventHandler(TextBoxSizeChanged); _TextBox.ModifiedChanged += new EventHandler(TextBoxModifiedChanged); _TextBox.KeyPress += new KeyPressEventHandler(TextBoxKeyPress); _TextBox.KeyDown += new KeyEventHandler(TextBoxKeyDown); this.Controls.Add(_TextBox); } #endregion #region Internal Implementation private bool _AutoSelectAll = false; /// /// Indicates whether all text is auto-selected when control gets input focus. Default value is false. /// [DefaultValue(false), Category("Behavior"), Description("Indicates whether all text is auto-selected when control gets input focus.")] public bool AutoSelectAll { get { return _AutoSelectAll; } set { _AutoSelectAll = value; } } protected override void OnBindingContextChanged(EventArgs e) { if (_DropDownControl != null) _DropDownControl.BindingContext = this.BindingContext; base.OnBindingContextChanged(e); } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.F4) { if (_PopupItem.Expanded) CloseDropDown(); else InvokeShowDropDown(); return true; } else if (keyData == Keys.Escape && this.IsPopupOpen) { CloseDropDown(); return true; } else if (_ButtonGroup.ProcessCmdKey(ref msg, keyData)) return true; return base.ProcessCmdKey(ref msg, keyData); } void TextBoxKeyPress(object sender, KeyPressEventArgs e) { if (_InternalReadOnly) e.Handled = true; } void TextBoxKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Down && e.Modifiers == Keys.Alt) { if (ButtonDropDown.Visible) { InvokeShowDropDown(); e.Handled = true; } } } private void TextBoxModifiedChanged(object sender, EventArgs e) { OnModifiedChanged(e); } protected virtual void OnModifiedChanged(EventArgs e) { EventHandler handler = ModifiedChanged; if (handler != null) { handler(this, e); } } private void TextBoxSizeChanged(object sender, EventArgs e) { if (!_InternalSizeUpdate) UpdateLayout(); } private void TextBoxTextChanged(object sender, EventArgs e) { base.Text = _TextBox.Text; ExecuteCommand(); } private void TextBoxTextAlignChanged(object sender, EventArgs e) { OnTextAlignChanged(e); } /// /// Raises the TextAlignChanged event. /// protected virtual void OnTextAlignChanged(EventArgs e) { EventHandler eh = TextAlignChanged; if (eh != null) eh(this, e); } protected override void OnEnabledChanged(EventArgs e) { if (!this.Enabled) _TextBox.ResetBackColor(); else if (_TextBox.BackColor != this.BackColor) _TextBox.BackColor = this.BackColor; base.OnEnabledChanged(e); } private Color _OldBackColor = Color.Empty; protected override void OnEnter(EventArgs e) { if (_FocusHighlightEnabled) { _OldBackColor = this.BackColor; this.BackColor = _FocusHighlightColor; } if (_AutoSelectAll) _TextBox.SelectAll(); base.OnEnter(e); } protected override void OnLeave(EventArgs e) { if (_FocusHighlightEnabled && !_OldBackColor.IsEmpty) { this.BackColor = _OldBackColor; if (_OldBackColor == SystemColors.Window) _TextBox.ResetBackColor(); _OldBackColor = Color.Empty; } base.OnLeave(e); } protected override bool QueryPopupCloseMouseDown() { Point p = this.PointToClient(MousePosition); if(this.ClientRectangle.Contains(p)) return false; return base.QueryPopupCloseMouseDown(); } /// /// Gets or sets whether FocusHighlightColor is used as background color to highlight text box when it has input focus. Default value is false. /// [DefaultValue(false), Browsable(true), Category("Appearance"), Description("Indicates whether FocusHighlightColor is used as background color to highlight text box when it has input focus.")] public bool FocusHighlightEnabled { get { return _FocusHighlightEnabled; } set { if (_FocusHighlightEnabled != value) { _FocusHighlightEnabled = value; //_TextBox.FocusHighlightEnabled = value; if (this.Focused) this.Invalidate(true); } } } /// /// Gets or sets the color used as background color to highlight text box when it has input focus and focus highlight is enabled. /// [Browsable(true), Category("Appearance"), Description("Indicates color used as background color to highlight text box when it has input focus and focus highlight is enabled.")] public Color FocusHighlightColor { get { return _FocusHighlightColor; } set { if (_FocusHighlightColor != value) { _FocusHighlightColor = value; _TextBox.FocusHighlightColor = value; if (this.Focused) this.Invalidate(true); } } } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public bool ShouldSerializeFocusHighlightColor() { return !_FocusHighlightColor.Equals(ColorScheme.GetColor(0xFFFF88)); } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public void ResetFocusHighlightColor() { FocusHighlightColor = ColorScheme.GetColor(0xFFFF88); } private ElementStyle _BackgroundStyle = new ElementStyle(); /// /// Specifies the background style of the control. /// [Category("Style"), Description("Gets or sets control background style."), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public ElementStyle BackgroundStyle { get { return _BackgroundStyle; } } /// /// Resets style to default value. Used by windows forms designer. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ResetBackgroundStyle() { _BackgroundStyle.StyleChanged -= new EventHandler(this.VisualPropertyChanged); _BackgroundStyle = new ElementStyle(); _BackgroundStyle.StyleChanged += new EventHandler(this.VisualPropertyChanged); this.Invalidate(); } private void VisualPropertyChanged(object sender, EventArgs e) { OnVisualPropertyChanged(); } protected virtual void OnVisualPropertyChanged() { _ButtonGroup.InvalidateArrange(); this.Invalidate(); } protected override void Dispose(bool disposing) { if (_BackgroundStyle != null) _BackgroundStyle.StyleChanged -= new EventHandler(VisualPropertyChanged); base.Dispose(disposing); } private InputButtonSettings _ButtonDropDown = null; /// /// Gets the object that describes the settings for the button that shows drop-down when clicked. /// [Category("Buttons"), Description("Describes the settings for the button that shows drop-down when clicked."), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public InputButtonSettings ButtonDropDown { get { return _ButtonDropDown; } } private InputButtonSettings _ButtonClear = null; /// /// Gets the object that describes the settings for the button that clears the content of the control when clicked. /// [Category("Buttons"), Description("Describes the settings for the button that clears the content of the control when clicked."), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public InputButtonSettings ButtonClear { get { return _ButtonClear; } } private InputButtonSettings _ButtonCustom = null; /// /// Gets the object that describes the settings for the custom button that can execute an custom action of your choosing when clicked. /// [Category("Buttons"), Description("Describes the settings for the custom button that can execute an custom action of your choosing when clicked."), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public InputButtonSettings ButtonCustom { get { return _ButtonCustom; } } private InputButtonSettings _ButtonCustom2 = null; /// /// Gets the object that describes the settings for the custom button that can execute an custom action of your choosing when clicked. /// [Category("Buttons"), Description("Describes the settings for the custom button that can execute an custom action of your choosing when clicked."), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public InputButtonSettings ButtonCustom2 { get { return _ButtonCustom2; } } void IInputButtonControl.InputButtonSettingsChanged(InputButtonSettings inputButtonSettings) { OnInputButtonSettingsChanged(inputButtonSettings); } protected virtual void OnInputButtonSettingsChanged(InputButtonSettings inputButtonSettings) { UpdateButtons(); } private VisualGroup _ButtonGroup = null; private void UpdateButtons() { RecreateButtons(); _ButtonGroup.InvalidateArrange(); this.Invalidate(); } internal VisualGroup ButtonGroup { get { return (_ButtonGroup); } } protected virtual void RecreateButtons() { VisualItem[] buttons = CreateOrderedButtonList(); // Remove all system buttons that are already in the list VisualGroup group = _ButtonGroup; VisualItem[] items = new VisualItem[group.Items.Count]; group.Items.CopyTo(items); foreach (VisualItem item in items) { if (item.ItemType == eSystemItemType.SystemButton) { group.Items.Remove(item); if (item == _ButtonCustom.ItemReference) item.MouseUp -= new MouseEventHandler(CustomButtonMouseUp); else if (item == _ButtonCustom2.ItemReference) item.MouseUp -= new MouseEventHandler(CustomButton2MouseUp); } } // Add new buttons to the list group.Items.AddRange(buttons); } private void CustomButtonMouseUp(object sender, MouseEventArgs e) { if (_ButtonCustom.ItemReference.RenderBounds.Contains(e.X, e.Y)) OnButtonCustomClick(e); } protected virtual void OnButtonCustomClick(EventArgs e) { if (ButtonCustomClick != null) ButtonCustomClick(this, e); } private void CustomButton2MouseUp(object sender, MouseEventArgs e) { if (_ButtonCustom2.ItemReference.RenderBounds.Contains(e.X, e.Y)) OnButtonCustom2Click(e); } protected virtual void OnButtonCustom2Click(EventArgs e) { if (ButtonCustom2Click != null) ButtonCustom2Click(this, e); } private VisualItem[] CreateOrderedButtonList() { SortedList list = CreateSortedButtonList(); VisualItem[] items = new VisualItem[list.Count]; list.Values.CopyTo(items, 0); return items; } private void CustomButtonClick(object sender, EventArgs e) { OnButtonCustomClick(e); } private void CustomButton2Click(object sender, EventArgs e) { OnButtonCustom2Click(e); } private void ClearButtonClick(object sender, EventArgs e) { ClearButtonClick(); } protected virtual SortedList CreateSortedButtonList() { SortedList list = new SortedList(4); if (_ButtonCustom.Visible) { VisualItem button = CreateButton(_ButtonCustom); if (_ButtonCustom.ItemReference != null) { _ButtonCustom.ItemReference.MouseUp -= CustomButtonMouseUp; _ButtonCustom.ItemReference.Click -= CustomButtonClick; } _ButtonCustom.ItemReference = button; button.Click += CustomButtonClick; button.MouseUp += CustomButtonMouseUp; button.Enabled = _ButtonCustom.Enabled; list.Add(_ButtonCustom, button); } if (_ButtonCustom2.Visible) { VisualItem button = CreateButton(_ButtonCustom2); if (_ButtonCustom.ItemReference != null) { _ButtonCustom.ItemReference.MouseUp -= CustomButton2MouseUp; _ButtonCustom.ItemReference.Click -= CustomButton2Click; } _ButtonCustom2.ItemReference = button; button.Click += CustomButton2Click; button.MouseUp += CustomButton2MouseUp; button.Enabled = _ButtonCustom2.Enabled; list.Add(_ButtonCustom2, button); } if (_ButtonClear.Visible) { VisualItem button = CreateButton(_ButtonClear); if (_ButtonClear.ItemReference != null) { _ButtonClear.ItemReference.MouseUp -= ClearButtonMouseUp; _ButtonClear.ItemReference.Click -= ClearButtonClick; } _ButtonClear.ItemReference = button; button.MouseUp += ClearButtonMouseUp; button.Click += ClearButtonClick; list.Add(_ButtonClear, button); } if (_ButtonDropDown.Visible) { VisualItem button = CreateButton(_ButtonDropDown); if (_ButtonDropDown.ItemReference != null) { _ButtonDropDown.ItemReference.MouseDown -= DropDownButtonMouseDown; _ButtonDropDown.ItemReference.Click -= DropDownButtonClick; } _ButtonDropDown.ItemReference = button; button.MouseDown += DropDownButtonMouseDown; button.Click += DropDownButtonClick; list.Add(_ButtonDropDown, button); } return list; } private void DropDownButtonClick(object sender, EventArgs e) { if (e is KeyEventArgs) // Only if invoked through keyboard shortcut { if (_PopupItem.Expanded) CloseDropDown(); else InvokeShowDropDown(); } } protected virtual VisualItem CreateButton(InputButtonSettings buttonSettings) { VisualItem item = null; if (buttonSettings == _ButtonDropDown) { item = new VisualDropDownButton(); ApplyButtonSettings(buttonSettings, item as VisualButton); } else { item = new VisualCustomButton(); ApplyButtonSettings(buttonSettings, item as VisualButton); } VisualButton button = item as VisualButton; button.ClickAutoRepeat = false; if (buttonSettings == _ButtonClear) { if (buttonSettings.Image == null && string.IsNullOrEmpty(buttonSettings.Text)) { //if (Dpi.Factor.Width > 1) button.Symbol = "\uf00d"; //else // button.Image = DevComponents.DotNetBar.BarFunctions.LoadBitmap("SystemImages.DateReset.png"); } } return item; } protected virtual void ApplyButtonSettings(InputButtonSettings buttonSettings, VisualButton button) { button.Text = buttonSettings.Text; button.Image = buttonSettings.Image; button.ItemType = eSystemItemType.SystemButton; button.Enabled = buttonSettings.Enabled; button.Shortcut = buttonSettings.Shortcut; button.Tooltip = buttonSettings.Tooltip; button.Symbol = buttonSettings.Symbol; button.SymbolSet = buttonSettings.SymbolSet; button.SymbolColor = buttonSettings.SymbolColor; } private void CreateButtonGroup() { VisualGroup group = new VisualGroup(); group.HorizontalItemSpacing = 0; group.ArrangeInvalid += new EventHandler(ButtonGroupArrangeInvalid); group.RenderInvalid += new EventHandler(ButtonGroupRenderInvalid); group.ResetMouseHover += ButtonGroupResetMouseHover; _ButtonGroup = group; } private void ButtonGroupRenderInvalid(object sender, EventArgs e) { Invalidate(); } private void ButtonGroupArrangeInvalid(object sender, EventArgs e) { Invalidate(); } private void ButtonGroupResetMouseHover(object sender, EventArgs e) { DevComponents.AdvTree.Interop.WinApi.ResetHover(this); } private bool _MouseOver = false; private PaintInfo CreatePaintInfo(Graphics g) { PaintInfo p = new PaintInfo(); p.Graphics = g; p.DefaultFont = this.Font; p.ForeColor = this.ForeColor; p.RenderOffset = new System.Drawing.Point(); Size s = this.Size; bool disposeStyle = false; ElementStyle style = GetBackgroundStyle(out disposeStyle); int styleSpace = (ElementStyleLayout.TopWhiteSpace(style, eSpacePart.Border) + ElementStyleLayout.BottomWhiteSpace(style, eSpacePart.Border)); if (styleSpace > 0) styleSpace += 2; s.Height -= styleSpace; styleSpace = (ElementStyleLayout.LeftWhiteSpace(style, eSpacePart.Border) + ElementStyleLayout.RightWhiteSpace(style, eSpacePart.Border)); if (styleSpace > 0) styleSpace += 2; s.Width -= styleSpace; p.AvailableSize = s; p.ParentEnabled = this.Enabled; p.MouseOver = _MouseOver || this.Focused; if (disposeStyle) style.Dispose(); return p; } private ElementStyle GetBackgroundStyle(out bool disposeStyle) { disposeStyle = false; _BackgroundStyle.SetColorScheme(this.GetColorScheme()); return ElementStyleDisplay.GetElementStyle(_BackgroundStyle, out disposeStyle); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; Rectangle clientRect = this.ClientRectangle; bool enabled = this.Enabled; if (!this.Enabled) e.Graphics.FillRectangle(SystemBrushes.Control, clientRect); else { using (SolidBrush brush = new SolidBrush(this.BackColor)) e.Graphics.FillRectangle(brush, clientRect); } bool disposeStyle = false; ElementStyle style = GetBackgroundStyle(out disposeStyle); if (style.Custom) { SmoothingMode sm = g.SmoothingMode; if (this.AntiAlias) g.SmoothingMode = SmoothingMode.AntiAlias; ElementStyleDisplayInfo displayInfo = new ElementStyleDisplayInfo(style, g, clientRect); if (!enabled) { ElementStyleDisplay.PaintBorder(displayInfo); } else ElementStyleDisplay.Paint(displayInfo); if (this.AntiAlias) g.SmoothingMode = sm; } PaintButtons(g); if (disposeStyle) style.Dispose(); } public override Color BackColor { get { return base.BackColor; } set { base.BackColor = value; } } [EditorBrowsable(EditorBrowsableState.Never)] public bool ShouldSerializeBackColor() { return this.BackColor != SystemColors.Window; } protected override void OnBackColorChanged(EventArgs e) { base.OnBackColorChanged(e); if (_TextBox.Parent != null) _TextBox.BackColor = this.BackColor; } private void PaintButtons(Graphics g) { PaintInfo p = CreatePaintInfo(g); if (!_ButtonGroup.IsLayoutValid) { UpdateLayout(p); } bool disposeStyle = false; ElementStyle style = GetBackgroundStyle(out disposeStyle); _ButtonGroup.RenderBounds = GetButtonsRenderBounds(style); _ButtonGroup.ProcessPaint(p); if (disposeStyle) style.Dispose(); } private Rectangle GetButtonsRenderBounds(ElementStyle style) { int y = ElementStyleLayout.TopWhiteSpace(style, eSpacePart.Border); if (y > 0) y += 1; if (this.RightToLeft == RightToLeft.Yes) { return new Rectangle((ElementStyleLayout.LeftWhiteSpace(style, eSpacePart.Border) + 1), y, _ButtonGroup.Size.Width, _ButtonGroup.Size.Height); } return new Rectangle(this.Width - (ElementStyleLayout.RightWhiteSpace(style, eSpacePart.Border) + 1) - _ButtonGroup.Size.Width, y, _ButtonGroup.Size.Width, _ButtonGroup.Size.Height); } protected override void OnResize(EventArgs e) { if (BarFunctions.IsHandleValid(this)) { _ButtonGroup.InvalidateArrange(); UpdateLayout(); this.Invalidate(); } base.OnResize(e); } protected override void OnGotFocus(EventArgs e) { _TextBox.Focus(); base.OnGotFocus(e); } protected override void OnRightToLeftChanged(EventArgs e) { _ButtonGroup.InvalidateArrange(); UpdateLayout(); this.Invalidate(); base.OnRightToLeftChanged(e); } protected override void OnFontChanged(EventArgs e) { _ButtonGroup.InvalidateArrange(); UpdateLayout(); this.Invalidate(); base.OnFontChanged(e); } protected override void OnForeColorChanged(EventArgs e) { _TextBox.ForeColor = this.ForeColor; base.OnForeColorChanged(e); } private void UpdateLayout() { if (!BarFunctions.IsHandleValid(this)) return; using (Graphics g = BarFunctions.CreateGraphics(this)) UpdateLayout(CreatePaintInfo(g)); } private bool _InternalSizeUpdate = false; private void UpdateLayout(PaintInfo p) { if (_InternalSizeUpdate) return; _InternalSizeUpdate = true; try { if (!_ButtonGroup.IsLayoutValid) { _ButtonGroup.PerformLayout(p); } bool disposeStyle = false; ElementStyle style = GetBackgroundStyle(out disposeStyle); Rectangle textBoxControlRect = ElementStyleLayout.GetInnerRect(style, this.ClientRectangle); if (RenderButtons) { Rectangle buttonsRect = GetButtonsRenderBounds(style); if (this.RightToLeft == RightToLeft.Yes) { textBoxControlRect.X += buttonsRect.Right; textBoxControlRect.Width -= buttonsRect.Right; } else { textBoxControlRect.Width -= (textBoxControlRect.Right - buttonsRect.X); } } if (_TextBox.Multiline == false && (_TextBox.PreferredHeight < textBoxControlRect.Height)) { textBoxControlRect.Y += (textBoxControlRect.Height - _TextBox.PreferredHeight) / 2; } _TextBox.Bounds = textBoxControlRect; if (disposeStyle) style.Dispose(); } finally { _InternalSizeUpdate = false; } } private Control _PreviousDropDownControlParent = null; private void DropDownButtonMouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left || _CloseTime != DateTime.MinValue && DateTime.Now.Subtract(_CloseTime).TotalMilliseconds < 250) { _CloseTime = DateTime.MinValue; return; } InvokeShowDropDown(); if ((Control.MouseButtons & MouseButtons.Left) == 0) _ButtonGroup.ProcessMouseUp(e); } private void InvokeShowDropDown() { CancelEventArgs cancelArgs = new CancelEventArgs(); OnButtonDropDownClick(cancelArgs); if (cancelArgs.Cancel) return; if (IsPopupOpen) CloseDropDown(); else ShowDropDown(); } /// /// Gets or sets a value indicating whether the drop-down is open. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public bool IsPopupOpen { get { return _PopupItem.Expanded; } set { if (value != _PopupItem.Expanded) { if (value) ShowDropDown(); else CloseDropDown(); } } } private bool _ShowingPopup = false; /// /// Shows drop-down popup. Note that popup will be shown only if there is a DropDownControl assigned or DropDownItems collection has at least one item. /// public void ShowDropDown() { if (_DropDownControl == null && _PopupItem.SubItems.Count == 0 || _ShowingPopup || _PopupItem.Expanded) return; CancelEventArgs ce = new CancelEventArgs(); OnBeforePopupOpen(ce); if (ce.Cancel) return; _ShowingPopup = true; try { ControlContainerItem cc = null; ItemContainer ic = null; if (_DropDownControl != null) { ic = new ItemContainer(); ic.Name = _DropDownItemContainerName; cc = new ControlContainerItem(_DropDownControlContainerName); ic.SubItems.Add(cc); _PopupItem.SubItems.Insert(0, ic); } if (_PopupItem.SubItems.Count == 0) { if (ic != null) _PopupItem.SubItems.Remove(ic); return; } if (_DropDownControl != null) { _PreviousDropDownControlParent = _DropDownControl.Parent; cc.Control = _DropDownControl; if (!_DropDownControl.IsHandleCreated) { IntPtr handle = _DropDownControl.Handle; // Forces creation of the control so its size etc. can be accessed reliably } } _PopupItem.SetDisplayRectangle(this.ClientRectangle); if (this.RightToLeft == RightToLeft.No) { Point pl = new Point(this.Width - _PopupItem.PopupSize.Width, this.Height); ScreenInformation screen = BarFunctions.ScreenFromControl(this); Point ps = PointToScreen(pl); if (screen != null && screen.WorkingArea.X > ps.X) { pl.X = 0; } _PopupItem.PopupLocation = pl; } _PopupItem.Expanded = !_PopupItem.Expanded; } finally { _ShowingPopup = false; } if (_DropDownControl != null && _AutoFocusDropDownControl && _DropDownControl.CanFocus) BarUtilities.InvokeDelayed(new MethodInvoker(delegate { _DropDownControl.Focus(); })); } /// /// Closes the drop-down popup if it is open. /// public void CloseDropDown() { if (_PopupItem.Expanded) _PopupItem.Expanded = false; } private DateTime _CloseTime = DateTime.MinValue; private void DropDownPopupClose(object sender, EventArgs e) { _CloseTime = DateTime.Now; ItemContainer ic = null; if (_PopupItem.SubItems.Contains(_DropDownItemContainerName)) ic = _PopupItem.SubItems[_DropDownItemContainerName] as ItemContainer; if (ic != null) { ControlContainerItem cc = ic.SubItems[_DropDownControlContainerName] as ControlContainerItem; if (cc != null) { cc.Control = null; ic.SubItems.Remove(cc); if (_DropDownControl != null) { _DropDownControl.Parent = _PreviousDropDownControlParent; _PreviousDropDownControlParent = null; } } _PopupItem.SubItems.Remove(ic); ic.Dispose(); } } private bool _AutoFocusDropDownControl = false; /// /// Indicates whether DropDownControl is automatically focused when popup is open. /// [DefaultValue(false), Category("Behavior"), Description("Indicates whether DropDownControl is automatically focused when popup is open.")] public bool AutoFocusDropDownControl { get { return _AutoFocusDropDownControl; } set { _AutoFocusDropDownControl = value; } } private void ClearButtonClick() { CancelEventArgs cancelArgs = new CancelEventArgs(); OnButtonClearClick(cancelArgs); if (cancelArgs.Cancel) return; _TextBox.Text = ""; } private void ClearButtonMouseUp(object sender, EventArgs e) { ClearButtonClick(); } /// /// Raises the ButtonClearClick event. /// /// protected virtual void OnButtonClearClick(CancelEventArgs e) { if (ButtonClearClick != null) ButtonClearClick(this, e); } /// /// Raises the ButtonDropDownClick event. /// /// protected virtual void OnButtonDropDownClick(CancelEventArgs e) { if (ButtonDropDownClick != null) ButtonDropDownClick(this, e); } private Control _DropDownControl = null; /// /// Gets or sets the reference of the control that will be displayed on popup that is shown when drop-down button is clicked. /// [DefaultValue(null), Description("Indicates reference of the control that will be displayed on popup that is shown when drop-down button is clicked.")] public Control DropDownControl { get { return _DropDownControl; } set { if (value == this) return; _DropDownControl = value; OnDropDownControlChanged(); } } /// /// Called when DropDownControl property has changed. /// protected virtual void OnDropDownControlChanged() { if (_DropDownControl != null) { _DropDownControl.BindingContext = this.BindingContext; if (!this.DesignMode) _DropDownControl.Visible = false; } } protected override PopupItem CreatePopupItem() { ButtonItem button = new ButtonItem("sysPopupProvider"); button.PopupFinalized += new EventHandler(DropDownPopupClose); _PopupItem = button; return button; } /// /// Gets the collection of BaseItem derived items displayed on popup menu. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public SubItemsCollection DropDownItems { get { return _PopupItem.SubItems; } } protected override void RecalcSize() { } public override void PerformClick() { } private bool RenderButtons { get { return _ButtonCustom != null && _ButtonCustom.Visible || _ButtonCustom2 != null && _ButtonCustom2.Visible || _ButtonDropDown != null && _ButtonDropDown.Visible || _ButtonClear != null && _ButtonClear.Visible; } } protected override void OnMouseMove(MouseEventArgs e) { if (RenderButtons) { _ButtonGroup.ProcessMouseMove(e); } base.OnMouseMove(e); } protected override void OnMouseLeave(EventArgs e) { if (RenderButtons) { _ButtonGroup.ProcessMouseLeave(); } base.OnMouseLeave(e); } protected override void OnMouseHover(EventArgs e) { if (RenderButtons) { _ButtonGroup.ProcessMouseHover(e); } base.OnMouseHover(e); } protected override void OnMouseDown(MouseEventArgs e) { if (RenderButtons) _ButtonGroup.ProcessMouseDown(e); base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { if (RenderButtons) _ButtonGroup.ProcessMouseUp(e); base.OnMouseUp(e); } internal void ProcessMouseUpOnGroup() { if (RenderButtons) _ButtonGroup.ProcessMouseUp(new MouseEventArgs(MouseButtons.Left, 0, -10, -10, 0)); this.Invalidate(); } /// /// Gets the reference to internal TextBox control. Use it to get access to the text box events and properties. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public TextBoxX TextBox { get { return _TextBox; } } private bool _InternalReadOnly = false; internal bool InternalReadOnly { get { return _InternalReadOnly; } set { _InternalReadOnly = value; } } /// /// Gets or sets whether watermark text is displayed when control is empty. Default value is true. /// [DefaultValue(true), Description("Indicates whether watermark text is displayed when control is empty.")] public virtual bool WatermarkEnabled { get { return _TextBox.WatermarkEnabled; } set { _TextBox.WatermarkEnabled = value; this.Invalidate(true); } } /// /// Gets or sets the watermark (tip) text displayed inside of the control when Text is not set and control does not have input focus. This property supports text-markup. /// [Browsable(true), DefaultValue(""), Localizable(true), Category("Appearance"), Description("Indicates watermark text displayed inside of the control when Text is not set and control does not have input focus."), Editor("DevComponents.DotNetBar.Design.TextMarkupUIEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral, PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor))] public string WatermarkText { get { return _TextBox.WatermarkText; } set { if (value == null) value = ""; _TextBox.WatermarkText = value; this.Invalidate(true); } } /// /// Gets or sets the watermark font. /// [Browsable(true), Category("Appearance"), Description("Indicates watermark font."), DefaultValue(null)] public Font WatermarkFont { get { return _TextBox.WatermarkFont; } set { _TextBox.WatermarkFont = value; this.Invalidate(true); } } /// /// Gets or sets the watermark text color. /// [Browsable(true), Category("Appearance"), Description("Indicates watermark text color.")] public Color WatermarkColor { get { return _TextBox.WatermarkColor; } set { _TextBox.WatermarkColor = value; this.Invalidate(); } } /// /// Indicates whether property should be serialized by Windows Forms designer. /// [EditorBrowsable(EditorBrowsableState.Never)] public bool ShouldSerializeWatermarkColor() { return _TextBox.WatermarkColor != SystemColors.GrayText; } /// /// Resets the property to default value. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ResetWatermarkColor() { this.WatermarkColor = SystemColors.GrayText; } /// /// Gets or sets the watermark hiding behaviour. Default value indicates that watermark is hidden when control receives input focus. /// [DefaultValue(eWatermarkBehavior.HideOnFocus), Category("Behavior"), Description("Indicates watermark hiding behaviour.")] public eWatermarkBehavior WatermarkBehavior { get { return _TextBox.WatermarkBehavior; } set { _TextBox.WatermarkBehavior = value; this.Invalidate(true); } } /// /// Gets or sets the watermark image displayed inside of the control when Text is not set and control does not have input focus. /// [DefaultValue(null), Category("Appearance"), Description("Indicates watermark image displayed inside of the control when Text is not set and control does not have input focus.")] public Image WatermarkImage { get { return _TextBox.WatermarkImage; } set { _TextBox.WatermarkImage = value; } } /// /// Gets or sets the watermark image alignment. /// [DefaultValue(ContentAlignment.MiddleLeft), Category("Appearance"), Description("Indicates watermark image alignment.")] public ContentAlignment WatermarkImageAlignment { get { return _TextBox.WatermarkImageAlignment; } set { _TextBox.WatermarkImageAlignment = value; } } #endregion #region ICommandSource Members protected virtual void ExecuteCommand() { if (_Command == null) return; CommandManager.ExecuteCommand(this); } /// /// Gets or sets the command assigned to the item. Default value is null. /// Note that if this property is set to null Enabled property will be set to false automatically to disable the item. /// [DefaultValue(null), Category("Commands"), Description("Indicates the command assigned to the item.")] public Command Command { get { return (Command)((ICommandSource)this).Command; } set { ((ICommandSource)this).Command = value; } } private ICommand _Command = null; [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] ICommand ICommandSource.Command { get { return _Command; } set { bool changed = false; if (_Command != value) changed = true; if (_Command != null) CommandManager.UnRegisterCommandSource(this, _Command); _Command = value; if (value != null) CommandManager.RegisterCommand(this, value); if (changed) OnCommandChanged(); } } /// /// Called when Command property value changes. /// protected virtual void OnCommandChanged() { } private object _CommandParameter = null; /// /// Gets or sets user defined data value that can be passed to the command when it is executed. /// [Browsable(true), DefaultValue(null), Category("Commands"), Description("Indicates user defined data value that can be passed to the command when it is executed."), System.ComponentModel.TypeConverter(typeof(System.ComponentModel.StringConverter)), System.ComponentModel.Localizable(true)] public object CommandParameter { get { return _CommandParameter; } set { _CommandParameter = value; } } #endregion #region TextBox Property Forwarding /// /// Gets or sets the text as it is currently displayed to the user. /// [Category("Appearance"), DefaultValue("Indicates text as it is currently displayed to the user"), Bindable(true), RefreshProperties(RefreshProperties.Repaint), Localizable(true)] public override string Text { get { return base.Text; } set { _TextBox.Text = value; } } /// /// Returns a string that represents the current text in text box. This method overrides ToString. /// /// A String that contains information about the current TextBox. The string includes the type, a simplified view of the input string, and the formatted input string. public override string ToString() { return _TextBox.ToString(); } /// /// Gets the preferred height for a text box. /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)] public int PreferredHeight { get { int height = _TextBox.PreferredHeight; bool disposeStyle = false; ElementStyle style = GetBackgroundStyle(out disposeStyle); height += ElementStyleLayout.VerticalStyleWhiteSpace(style); if (disposeStyle) { style.Dispose(); } return height; } } protected override void OnHandleCreated(EventArgs e) { UpdateLayout(); // Fixes an issue where button if visible is not showing under certain use cases base.OnHandleCreated(e); } public override Size GetPreferredSize(Size proposedSize) { Size ps = _TextBox.GetPreferredSize(proposedSize); ps.Height = PreferredHeight; return ps; } /// /// Gets or sets a custom StringCollection to use when the AutoCompleteSource property is set to CustomSource. /// A StringCollection to use with AutoCompleteSource. /// [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Localizable(true), Description("Indicates custom StringCollection to use when the AutoCompleteSource property is set to CustomSource."), Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version= 2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] public AutoCompleteStringCollection AutoCompleteCustomSource { get { return _TextBox.AutoCompleteCustomSource; } set { _TextBox.AutoCompleteCustomSource = value; } } /// /// Gets or sets an option that controls how automatic completion works for the TextBox. /// One of the values of AutoCompleteMode. The values are Append, None, Suggest, and SuggestAppend. The default is None. /// [Description("Gets or sets an option that controls how automatic completion works for the TextBox."), Browsable(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue(0)] public AutoCompleteMode AutoCompleteMode { get { return _TextBox.AutoCompleteMode; } set { _TextBox.AutoCompleteMode = value; } } /// /// Gets or sets a value specifying the source of complete strings used for automatic completion. /// One of the values of AutoCompleteSource. The options are AllSystemSources, AllUrl, FileSystem, HistoryList, RecentlyUsedList, CustomSource, and None. The default is None. /// [DefaultValue(0x80), TypeConverter(typeof(TextBoxAutoCompleteSourceConverter)), Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Description("Gets or sets a value specifying the source of complete strings used for automatic completion.")] public AutoCompleteSource AutoCompleteSource { get { return _TextBox.AutoCompleteSource; } set { _TextBox.AutoCompleteSource = value; } } /// /// Gets or sets whether the TextBox control modifies the case of characters as they are typed. /// One of the CharacterCasing enumeration values that specifies whether the TextBox control modifies the case of characters. The default is CharacterCasing.Normal. /// [Category("Behavior"), Description("Indicates whether the TextBox control modifies the case of characters as they are typed."), DefaultValue(0)] public CharacterCasing CharacterCasing { get { return _TextBox.CharacterCasing; } set { _TextBox.CharacterCasing = value; } } /// /// Gets or sets the character used to mask characters of a password in a single-line TextBox control. /// The character used to mask characters entered in a single-line TextBox control. Set the value of this property to 0 (character value) if you do not want the control to mask characters as they are typed. Equals 0 (character value) by default. /// [RefreshProperties(RefreshProperties.Repaint), Localizable(true), Description("Gets or sets the character used to mask characters of a password in a single-line TextBox control."), Category("Behavior"), DefaultValue('\0')] public char PasswordChar { get { return _TextBox.PasswordChar; } set { _TextBox.PasswordChar = value; } } /// /// Gets or sets how text is aligned in a TextBox control. /// One of the HorizontalAlignment enumeration values that specifies how text is aligned in the control. The default is HorizontalAlignment.Left. /// [DefaultValue(0), Category("Appearance"), Localizable(true), Description("Indicates how text is aligned in a TextBox control.")] public HorizontalAlignment TextAlign { get { return _TextBox.TextAlign; } set { _TextBox.TextAlign = value; } } /// /// Gets or sets a value indicating whether the text in the TextBox control should appear as the default password character. /// true if the text in the TextBox control should appear as the default password character; otherwise, false. /// [Category("Behavior"), DefaultValue(false), Description("Gets or sets a value indicating whether the text in the TextBox control should appear as the default password character."), RefreshProperties(RefreshProperties.Repaint)] public bool UseSystemPasswordChar { get { return _TextBox.UseSystemPasswordChar; } set { _TextBox.UseSystemPasswordChar = value; } } /// /// Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus. /// true if the selected text does not appear highlighted when the text box control loses focus; false, if the selected text remains highlighted when the text box control loses focus. The default is true. /// [Category("Behavior"), DefaultValue(true), Description("Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.")] public bool HideSelection { get { return _TextBox.HideSelection; } set { _TextBox.HideSelection = value; } } /// /// Gets or sets the maximum number of characters the user can type or paste into the text box control. /// The number of characters that can be entered into the control. The default is 32767. /// [Category("Behavior"), Description("Gets or sets the maximum number of characters the user can type or paste into the text box control."), DefaultValue(0x7fff), Localizable(true)] public virtual int MaxLength { get { return _TextBox.MaxLength; } set { _TextBox.MaxLength = value; } } /// /// Gets or sets a value that indicates that the text box control has been modified by the user since the control was created or its contents were last set. /// true if the control's contents have been modified; otherwise, false. The default is false. /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)] public bool Modified { get { return _TextBox.Modified; } set { _TextBox.Modified = value; } } /// /// Gets or sets a value indicating whether text in the text box is read-only. /// true if the text box is read-only; otherwise, false. The default is false. /// [DefaultValue(false), RefreshProperties(RefreshProperties.Repaint), Category("Behavior"), Description("Gets or sets a value indicating whether text in the text box is read-only.")] public bool ReadOnly { get { return _TextBox.ReadOnly; } set { _TextBox.ReadOnly = value; } } /// /// Gets or sets a value indicating the currently selected text in the control. /// A string that represents the currently selected text in the text box. /// [Description("Gets or sets a value indicating the currently selected text in the control."), Browsable(false), Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public virtual string SelectedText { get { return _TextBox.SelectedText; } set { _TextBox.SelectedText = value; } } /// /// Gets or sets the number of characters selected in the text box. /// The number of characters selected in the text box. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Description("Gets or sets the number of characters selected in the text box."), Category("Appearance")] public virtual int SelectionLength { get { return _TextBox.SelectionLength; } set { _TextBox.SelectionLength = value; } } /// /// Gets or sets the starting point of text selected in the text box. /// The starting position of text selected in the text box. /// [Description("Gets or sets the starting point of text selected in the text box."), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Category("Appearance"), Browsable(false)] public int SelectionStart { get { return _TextBox.SelectionStart; } set { _TextBox.SelectionStart = value; } } /// /// Gets the length of text in the control.Returns number of characters contained in the text of the control. /// [Browsable(false)] public virtual int TextLength { get { return _TextBox.TextLength; } } /// /// Appends text to the current text of a text box. /// /// The text to append to the current contents of the text box. public void AppendText(string text) { _TextBox.AppendText(text); } /// /// Clears all text from the text box control. /// public void Clear() { _TextBox.Clear(); } /// /// Clears information about the most recent operation from the undo buffer of the text box. /// public void ClearUndo() { _TextBox.ClearUndo(); } /// /// Copies the current selection in the text box to the Clipboard. /// [UIPermission(SecurityAction.Demand, Clipboard = UIPermissionClipboard.OwnClipboard)] public void Copy() { _TextBox.Copy(); } /// /// Moves the current selection in the text box to the Clipboard. /// public void Cut() { _TextBox.Cut(); } /// /// Specifies that the value of the SelectionLength property is zero so that no characters are selected in the control. /// public void DeselectAll() { _TextBox.DeselectAll(); } /// /// Retrieves the character that is closest to the specified location within the control. /// /// The location from which to seek the nearest character. /// The character at the specified location. public virtual char GetCharFromPosition(Point pt) { return _TextBox.GetCharFromPosition(pt); } /// /// Retrieves the index of the character nearest to the specified location. /// /// The location to search. /// The zero-based character index at the specified location. public virtual int GetCharIndexFromPosition(Point pt) { return _TextBox.GetCharIndexFromPosition(pt); } /// /// Retrieves the index of the first character of a given line. /// /// The line for which to get the index of its first character. /// The zero-based character index in the specified line. public int GetFirstCharIndexFromLine(int lineNumber) { return _TextBox.GetFirstCharIndexFromLine(lineNumber); } /// /// Retrieves the index of the first character of the current line. /// /// The zero-based character index in the current line. public int GetFirstCharIndexOfCurrentLine() { return _TextBox.GetFirstCharIndexOfCurrentLine(); } /// /// Retrieves the line number from the specified character position within the text of the control. /// /// The character index position to search. /// The zero-based line number in which the character index is located. public virtual int GetLineFromCharIndex(int index) { return _TextBox.GetLineFromCharIndex(index); } /// /// Retrieves the location within the control at the specified character index. /// /// The index of the character for which to retrieve the location. /// The location of the specified character. public virtual Point GetPositionFromCharIndex(int index) { return _TextBox.GetPositionFromCharIndex(index); } /// /// Replaces the current selection in the text box with the contents of the Clipboard. /// [UIPermission(SecurityAction.Demand, Clipboard = UIPermissionClipboard.OwnClipboard)] public void Paste() { _TextBox.Paste(); } /// /// Selects a range of text in the text box. /// /// The position of the first character in the current text selection within the text box. /// The number of characters to select. public void Select(int start, int length) { _TextBox.Select(start, length); } /// /// Selects all text in the text box. /// public void SelectAll() { _TextBox.SelectAll(); } /// /// Undoes the last edit operation in the text box. /// public void Undo() { _TextBox.Undo(); } /// /// Replaces the specified selection in the TextBox with the contents of the Clipboard. /// /// The text to replace. public void Paste(string text) { _TextBox.Paste(text); } #endregion } #region TextBoxAutoCompleteSourceConverter internal class TextBoxAutoCompleteSourceConverter : EnumConverter { // Methods public TextBoxAutoCompleteSourceConverter(Type type) : base(type) { } public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { TypeConverter.StandardValuesCollection standardValues = base.GetStandardValues(context); ArrayList values = new ArrayList(); int count = standardValues.Count; for (int i = 0; i < count; i++) { if (!standardValues[i].ToString().Equals("ListItems")) { values.Add(standardValues[i]); } } return new TypeConverter.StandardValuesCollection(values); } } #endregion } #endif