using System; using System.ComponentModel; using System.Security.Permissions; using System.Windows.Forms; using Dongke.WinForm.Utilities; namespace Dongke.WinForm.Controls { /// /// 自定义弹出下拉框控件 /// public abstract class PopupComboBox : CmbComboBox { #region 事件声明 #region EditReadOnlyChanged /// /// 当 EditReadOnly 属性的值更改时发生 /// [Description("当 EditReadOnly 属性的值更改时发生。"), Category("CustomerEx")] [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new event EventHandler EditReadOnlyChanged { add { base.EditReadOnlyChanged += value; } remove { base.EditReadOnlyChanged -= value; } } #endregion #endregion #region 成员变量 /// /// 弹出控件 /// private PopupControlHost _popupDropDown = null; /// /// 弹出控件 /// private Control _dropDownControl = null; /// /// 是否显示原有的ListBox /// private bool _showComboBoxDropDown = false; private int _dropDownHeight = 0; private int _dropDownWidth = 0; private bool _integralHeight = false; #endregion #region 构造函数 /// /// 自定义弹出下拉框控件 /// public PopupComboBox() { base.FormattingEnabled = false; base.EditReadOnly = true; this._popupDropDown = new PopupControlHost(this); this._popupDropDown.SelectedOK += PopupDropDown_SelectedOK; this._popupDropDown.SelectedCancel += PopupDropDown_SelectedCancel; this.SetShowComboBoxDropDown(); } #endregion #region 属性 /// /// 自定义的下拉项目 /// protected PopupControlHost PopupDropDown { get { return this._popupDropDown; } } /// /// Gets or sets the drop down control. /// /// The drop down control. protected Control DropDownControl { get { return this._dropDownControl; } set { if (this._dropDownControl != value) { this._dropDownControl = value; this._popupDropDown.Content = this._dropDownControl; } } } /// /// 获取或设置一个值,该值指示是否显示原有的ListBox。 /// protected virtual bool ShowComboBoxDropDown { get { return this._showComboBoxDropDown; } set { if (this._showComboBoxDropDown != value) { this._showComboBoxDropDown = value; this.SetShowComboBoxDropDown(); } } } #endregion #region 重写属性 /// /// 获取或设置与此控件关联的文本。 /// public override string Text { get { return base.Text; } set { //base.Text = value; } } /// /// 获取或设置一个值,该值指示文本框中的文本是否为只读。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public override bool EditReadOnly { get { return base.EditReadOnly; } set { base.EditReadOnly = value; } } /// /// 获取或设置组合框下拉部分的宽度。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new virtual int DropDownWidth { get { if (this._showComboBoxDropDown) { return base.DropDownWidth; } return this._popupDropDown.Width; } set { if (value > 0) { if (this._showComboBoxDropDown) { base.DropDownWidth = value; } else { this._popupDropDown.Width = value; } } } } /// /// 获取或设置 System.Windows.Forms.ComboBox 下拉部分的高度(以像素为单位)。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new virtual int DropDownHeight { get { if (this._showComboBoxDropDown) { return base.DropDownHeight; } return this._popupDropDown.Height; } set { if (value > 0) { if (this._showComboBoxDropDown) { base.DropDownHeight = value; } else { this._popupDropDown.Height = value; } } } } /// /// 获取或设置一个值,该值指示控件是否应调整大小以避免只显示项的局部。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new virtual bool IntegralHeight { get { return base.IntegralHeight; } set { if (this._showComboBoxDropDown) { base.IntegralHeight = value; } } } /// /// 获取一个值,该值指示控件是否具有焦点。 /// public override bool Focused { get { if (base.Focused) { return true; } return this._popupDropDown.Focused || this._dropDownControl.Focused; } } /// /// 获取或设置在 System.Windows.Forms.ComboBox.AutoCompleteSource 属性设置为 CustomSource /// 时要使用的自定义 System.Collections.Specialized.StringCollection。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new AutoCompleteStringCollection AutoCompleteCustomSource { get { return base.AutoCompleteCustomSource; } set { base.AutoCompleteCustomSource = value; } } /// /// 获取或设置控制自动完成如何作用于 System.Windows.Forms.ComboBox 的选项。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new AutoCompleteMode AutoCompleteMode { get { return base.AutoCompleteMode; } set { base.AutoCompleteMode = value; } } /// /// 获取或设置一个值,该值指定用于自动完成的完整字符串源。 /// [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [EditorBrowsable(EditorBrowsableState.Never)] public new AutoCompleteSource AutoCompleteSource { get { return base.AutoCompleteSource; } set { base.AutoCompleteSource = value; } } #endregion #region 重写事件 #region 属性改变 /// /// 字体改变事件 /// /// protected override void OnFontChanged(EventArgs e) { base.OnAutoSizeChanged(e); this._dropDownControl.Font = this.Font; } /// /// 背景色改变事件 /// /// protected override void OnBackColorChanged(EventArgs e) { base.OnBackColorChanged(e); this._dropDownControl.BackColor = this.BackColor; } #endregion #region 行为事件 /// /// 当显示下拉部分时 /// /// protected override void OnDropDown(EventArgs e) { base.OnDropDown(e); } /// /// 当下拉部分不再可见时 /// /// protected override void OnDropDownClosed(EventArgs e) { base.OnDropDownClosed(e); } #endregion #region 键盘事件 #endregion #region 焦点事件 #endregion #region 鼠标事件 #endregion #endregion #region 重写方法 /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing) { if (this._popupDropDown != null) { this._popupDropDown.Dispose(); } if (this._dropDownControl != null) { this._dropDownControl.Dispose(); } } base.Dispose(disposing); } /// /// Processes Windows messages. /// /// The Windows to process. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] protected override void WndProc(ref Message m) { if (!this.ReadOnly) { if (m.Msg == (int)WindowsMessage.WM_LBUTTONDBLCLK || m.Msg == (int)WindowsMessage.WM_LBUTTONDOWN) { if (!this._showComboBoxDropDown) { if (this._popupDropDown.IsOpen) { this.CloseDropDown(); } else { this.ShowDropDown(); } return; } } } base.WndProc(ref m); } #endregion #region 事件处理 /// /// 确定 /// /// /// private void PopupDropDown_SelectedOK(object sender, EventArgs e) { this.PopupSelectedOK(); } /// /// 取消 /// /// /// private void PopupDropDown_SelectedCancel(object sender, EventArgs e) { this.PopupSelectedCancel(); } #endregion #region 公有方法 /// /// 显示下拉列表。 /// public virtual void ShowDropDown() { if (this.ReadOnly) { return; } if (this._popupDropDown != null && !this._popupDropDown.IsOpen) { this.Focus(); this._popupDropDown.Show(); } } /// /// 关闭下拉列表。 /// public virtual void CloseDropDown() { if (this._popupDropDown != null && this._popupDropDown.IsOpen) { this._popupDropDown.Close(); } } #endregion #region 内部方法 #endregion #region 保护方法 /// /// 确定 /// protected virtual void PopupSelectedOK() { } /// /// 取消 /// protected virtual void PopupSelectedCancel() { } /// /// 设置文本 /// /// 文本 protected virtual void SetText(string text) { base.Text = text; } #endregion #region 私有方法 /// /// 设置下拉列表 /// private void SetShowComboBoxDropDown() { if (this._showComboBoxDropDown) { base.DropDownHeight = this._dropDownHeight; base.DropDownWidth = this._dropDownWidth; base.IntegralHeight = this._integralHeight; } else { this._dropDownHeight = base.DropDownHeight; this._dropDownWidth = base.DropDownWidth; this._integralHeight = base.IntegralHeight; base.DropDownHeight = 1; base.DropDownWidth = 1; base.IntegralHeight = false; } } #endregion } }