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