| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:DKListBoxComboBox.cs
- * 2.功能描述:下拉列表控件:根据输入内容进行模糊查询
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/09/04 1.00 新建
- *******************************************************************************/
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using Dongke.IBOSS.PRD.Basics.Library;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- /// <summary>
- /// 下拉列表控件:根据输入内容进行模糊查询
- /// </summary>
- public partial class DKListBoxComboBox : PopupComboBox
- {
- #region 委托声明
- #endregion
- #region 事件声明
- #endregion
- #region 常量
- #endregion
- #region 成员变量
- ListControlContainer _containerControl = null;
- /// <summary>
- /// 多选控件
- /// </summary>
- private C_ListBox _listBox;
- /// <summary>
- /// 只能从下拉选项中选择值
- /// </summary>
- private bool _dropDownOnly = true;
- /// <summary>
- /// 下拉列表显示中
- /// </summary>
- private bool _isDropDownShowing = false;
- /// <summary>
- /// 下拉列表显示状态
- /// </summary>
- private bool _isDropDownClose = true;
- /// <summary>
- /// Form事件设定
- /// </summary>
- private bool _isSetting = false;
- #endregion
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public DKListBoxComboBox()
- : base()
- {
- InitializeComponent();
- this._listBox = new C_ListBox();
- this._containerControl = new ListControlContainer();
- this._containerControl.Controls.Add(this._listBox);
- this._containerControl.Padding = new Padding(0, 0, 0, 12);
- this._listBox.Dock = DockStyle.Fill;
- this._listBox.IntegralHeight = false;
- this.DropDownControl = this._containerControl;
- this.dropDown.Resizable = true;
- this.dropDown.FocusOnOpen = false;
- this.dropDown.AutoClose = false;
- this.dropDown.Opening += this.dropDown_Opening;
- this.dropDown.Closed += this.dropDown_Closed;
- this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
- this.Leave += this.DKListBoxComboBox_Leave;
- this.KeyDown += this.DKListBoxComboBox_KeyDown;
- this.TextChanged += this.DKListBoxComboBox_TextChanged;
- this.KeyUp += this.DKListBoxComboBox_KeyUp;
- this.LocationChanged += this.DKListBoxComboBox_LocationChanged;
- this.SizeChanged += this.DKListBoxComboBox_SizeChanged;
- //this.DropDown += DKListBoxComboBox_DropDown;
- //this.DropDownClosed += DKListBoxComboBox_DropDownClosed;
- }
- void ShowCbDropDown()
- {
- //this.OnDropDown(null);
- this.ShowDropDown();
- }
- void HideCbDropDown()
- {
- //this.OnDropDownClosed(null);
- this.HideDropDown();
- }
- //void DKListBoxComboBox_DropDownClosed(object sender, EventArgs e)
- //{
- // try
- // {
- // this.HideDropDown();
- // }
- // catch (Exception ex)
- // {
- // throw ex;
- // }
- //}
- //void DKListBoxComboBox_DropDown(object sender, EventArgs e)
- //{
- // try
- // {
- // this.ShowDropDown();
- // }
- // catch (Exception ex)
- // {
- // throw ex;
- // }
- //}
- #endregion
- #region 单例模式
- #endregion
- #region 属性
-
- /// <summary>
- /// 获取或设置控件的数据源
- /// </summary>
- public new DataTable DataSource
- {
- get
- {
- return this._listBox.DataSource as DataTable;
- }
- set
- {
- this._listBox.DataSource = value;
- }
- }
- /// <summary>
- /// 获取或设置一个字符串,该字符串指定要显示其内容的列表框中所含对象的属性
- /// </summary>
- public new string DisplayMember
- {
- get
- {
- return this._listBox.DisplayMember;
- }
- set
- {
- this._listBox.DisplayMember = value;
- }
- }
- /// <summary>
- /// 获取或设置一个字符串,该字符串指定要从中取值的数据源的属性
- /// </summary>
- public new string ValueMember
- {
- get
- {
- return this._listBox.ValueMember;
- }
- set
- {
- this._listBox.ValueMember = value;
- }
- }
- /// <summary>
- /// 设定或者获取控件是否只能从下拉选项中选择值
- /// </summary>
- [Description("设定或者获取控件是否只能从下拉选项中选择值")]
- [DefaultValue(true)]
- public bool DropDownOnly
- {
- get
- {
- return this._dropDownOnly;
- }
- set
- {
- this._dropDownOnly = value;
- }
- }
- /// <summary>
- /// Items
- /// </summary>
- [Browsable(true)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
- public new ListBox.ObjectCollection Items
- {
- get
- {
- return this._listBox.Items;
- }
- }
- /// <summary>
- /// 选项
- /// </summary>
- [Browsable(true)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
- public C_ListBox ListBox
- {
- get
- {
- return this._listBox;
- }
- }
- public new object SelectedValue
- {
- get
- {
- return this._listBox.SelectedValue;
- }
- set
- {
- this.SetDataSource(null);
- if (value != null)
- {
- this._listBox.SelectedValue = value;
- }
- }
- }
- public new string Text
- {
- get
- {
- return base.Text;
- }
- set
- {
- base.Text = value;
- //this.SetDataSource(value);
- this.ListBox_SelectedIndexChanged(this, null);
- }
- }
- #endregion
- #region 事件处理
- /// <summary>
- /// 下拉列表打开
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dropDown_Opening(object sender, CancelEventArgs e)
- {
- //try
- //{
- // this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
- // this._listBox.ClearSelected();
- // this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
- //}
- //catch (Exception ex)
- //{
- // throw ex;
- //}
- }
- /// <summary>
- /// 下拉列表关闭
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
- {
- }
- /// <summary>
- /// 文本框值改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKListBoxComboBox_TextChanged(object sender, EventArgs e)
- {
- try
- {
- this.SetDataSource(base.Text);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKListBoxComboBox_KeyUp(object sender, KeyEventArgs e)
- {
- try
- {
- int boxSelectedIndex = this.ListBox.SelectedIndex;
- int boxItemCount = this.ListBox.Items.Count;
- switch (e.KeyCode)
- {
- // 通过上下箭头在待选框中移动
- case Keys.Up:
- if (boxItemCount > 0)
- {
- boxSelectedIndex--;
- if (boxSelectedIndex < -1)
- {
- boxSelectedIndex = boxItemCount - 1;
- }
- this._isDropDownClose = false;
- this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
- if (boxSelectedIndex == -1)
- {
- this.ListBox.ClearSelected();
- }
- else
- {
- this.ListBox.SelectedIndex = boxSelectedIndex;
- }
- this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
- this._isDropDownClose = true;
- }
- break;
- case Keys.Down:
- if (boxItemCount > 0)
- {
- boxSelectedIndex++;
- if (boxSelectedIndex >= boxItemCount)
- {
- boxSelectedIndex = -1;
- }
- this._isDropDownClose = false;
- this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
- if (boxSelectedIndex == -1)
- {
- this._listBox.ClearSelected();
- this._listBox.SelectedItem = null;
- }
- else
- {
- this._listBox.SelectedIndex = boxSelectedIndex;
- }
- this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
- this._isDropDownClose = true;
- }
- break;
- case Keys.Escape:
- // ESC隐藏提示
- //this.ResetSelectedValue();
- break;
- case Keys.Return:
- // 回车选择 在输入法中按回车也会响应这个事件
- this.ResetSelectedValue();
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 输入文本框值
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKListBoxComboBox_KeyDown(object sender, KeyEventArgs e)
- {
- try
- {
- this.ShowCbDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// Form大小改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void form_SizeChanged(object sender, EventArgs e)
- {
- try
- {
- this.HideCbDropDown();
- Control control = sender as Control;
- if (control != null)
- {
- control.Refresh();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// Form位置改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void form_LocationChanged(object sender, EventArgs e)
- {
- try
- {
- this.HideCbDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// Form失去焦点时
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void form_Deactivate(object sender, EventArgs e)
- {
- try
- {
- // listbox不能改变大小,不能移动滚动条
- //this.HideCbDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// Form激活
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void form_Activated(object sender, EventArgs e)
- {
- try
- {
- this.HideCbDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 焦点离开
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKListBoxComboBox_Leave(object sender, EventArgs e)
- {
- try
- {
- this.ResetSelectedValue();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 选项改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- try
- {
- if (this._listBox.SelectedItem == null)
- {
- base.Text = string.Empty;
- }
- else
- {
- this.TextChanged -= this.DKListBoxComboBox_TextChanged;
- base.Text = this._listBox.GetItemText(this._listBox.SelectedItem);
- this.TextChanged += this.DKListBoxComboBox_TextChanged;
- }
- this.SelectionStart = base.Text.Length;
- this.HideDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 尺寸改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKListBoxComboBox_SizeChanged(object sender, EventArgs e)
- {
- try
- {
- this.HideCbDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 位置改变
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void DKListBoxComboBox_LocationChanged(object sender, EventArgs e)
- {
- try
- {
- this.HideCbDropDown();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 公有方法/函数
- /// <summary>
- /// 显示下拉列表
- /// </summary>
- public override void ShowDropDown()
- {
- if (this.Enabled && !this.dropDown.Visible)
- {
- if (!_isSetting)
- {
- _isSetting = true;
- Control control = this.Parent;
- while (control != null && !(control is Form))
- {
- control = control.Parent;
- }
- Form form = control as Form;
- if (form != null)
- {
- form.LocationChanged += form_LocationChanged;
- form.SizeChanged += form_SizeChanged;
- form.Deactivate += form_Deactivate;
- form.Activated += form_Activated;
- }
- }
- //int itemCount = this.ListBox.Items.Count;
- //this._containerControl.Height = itemCount * 20;
- this._isDropDownShowing = true;
- base.ShowDropDown();
- this._isDropDownShowing = false;
- int selectionStart = this.SelectionStart;
- int selectionLength = this.SelectionLength;
- this.Focus();
- this.SelectionStart = selectionStart;
- this.SelectionLength = selectionLength;
- }
- }
- /// <summary>
- /// 关闭下拉列表
- /// </summary>
- public override void HideDropDown()
- {
- if (this.dropDown.Visible && this._isDropDownClose)
- {
- base.HideDropDown();
- this.dropDown.Close();
- this.dropDown.Visible = false;
- }
- }
- #endregion
- #region 受保护方法/函数
- /// <summary>
- /// 改变大小
- /// </summary>
- /// <param name="e"></param>
- protected override void OnResize(EventArgs e)
- {
- // When the ComboBox is resized, the width of the dropdown
- // is also resized to match the width of the ComboBox. I think it looks better.
- Size Size = new Size(Width, DropDownControl.Height);
- dropDown.Size = Size;
- base.OnResize(e);
- }
- #endregion
- #region 私有方法/函数
- /// <summary>
- ///
- /// </summary>
- internal void ResetSelectedValue()
- {
- if (this._isDropDownShowing)
- {
- return;
- }
- this.HideCbDropDown();
- if (this._listBox.SelectedItem != null)
- {
- base.Text = this._listBox.GetItemText(this._listBox.SelectedItem);
- this.SelectionStart = base.Text.Length;
- return;
- }
- if (this._dropDownOnly && !string.IsNullOrEmpty(base.Text))
- {
- bool hasValue = false;
- foreach (object item in this._listBox.Items)
- {
- string itemText = this._listBox.GetItemText(item);
- if (itemText.Contains(this.Text))
- {
- base.Text = itemText;
- this._listBox.SelectedItem = item;
- hasValue = true;
- break;
- }
- }
- if (!hasValue)
- {
- base.Text = string.Empty;
- this._listBox.ClearSelected();
- }
- this.SelectionStart = base.Text.Length;
- }
- }
- /// <summary>
- /// 模糊过滤数据
- /// </summary>
- /// <param name="text"></param>
- private void SetDataSource(string text)
- {
- DataTable dtDataSource = this._listBox.DataSource as DataTable;
- if (dtDataSource == null)
- {
- return;
- }
- this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
- if (string.IsNullOrEmpty(text)
- || string.IsNullOrEmpty(this._listBox.DisplayMember)
- || string.IsNullOrEmpty(this._listBox.ValueMember))
- {
- dtDataSource.DefaultView.RowFilter = null;
- }
- else
- {
- dtDataSource.DefaultView.RowFilter = this._listBox.DisplayMember + " LIKE '%" + Utility.SelectFilterLike(text) + "%'";
- }
- if (string.IsNullOrEmpty(text))
- {
- this._listBox.ClearSelected();
- this._listBox.SelectedItem = null;
- this.TextChanged -= this.DKListBoxComboBox_TextChanged;
- base.Text = text;
- this.TextChanged += this.DKListBoxComboBox_TextChanged;
- }
- this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
- }
- #endregion
- }
- }
|