DKListBoxComboBox.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DKListBoxComboBox.cs
  5. * 2.功能描述:下拉列表控件:根据输入内容进行模糊查询
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/04 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Windows.Forms;
  15. using Dongke.IBOSS.PRD.Basics.Library;
  16. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  17. {
  18. /// <summary>
  19. /// 下拉列表控件:根据输入内容进行模糊查询
  20. /// </summary>
  21. public partial class DKListBoxComboBox : PopupComboBox
  22. {
  23. #region 委托声明
  24. #endregion
  25. #region 事件声明
  26. #endregion
  27. #region 常量
  28. #endregion
  29. #region 成员变量
  30. ListControlContainer _containerControl = null;
  31. /// <summary>
  32. /// 多选控件
  33. /// </summary>
  34. private C_ListBox _listBox;
  35. /// <summary>
  36. /// 只能从下拉选项中选择值
  37. /// </summary>
  38. private bool _dropDownOnly = true;
  39. /// <summary>
  40. /// 下拉列表显示中
  41. /// </summary>
  42. private bool _isDropDownShowing = false;
  43. /// <summary>
  44. /// 下拉列表显示状态
  45. /// </summary>
  46. private bool _isDropDownClose = true;
  47. /// <summary>
  48. /// Form事件设定
  49. /// </summary>
  50. private bool _isSetting = false;
  51. #endregion
  52. #region 构造函数
  53. /// <summary>
  54. /// 构造函数
  55. /// </summary>
  56. public DKListBoxComboBox()
  57. : base()
  58. {
  59. InitializeComponent();
  60. this._listBox = new C_ListBox();
  61. this._containerControl = new ListControlContainer();
  62. this._containerControl.Controls.Add(this._listBox);
  63. this._containerControl.Padding = new Padding(0, 0, 0, 12);
  64. this._listBox.Dock = DockStyle.Fill;
  65. this._listBox.IntegralHeight = false;
  66. this.DropDownControl = this._containerControl;
  67. this.dropDown.Resizable = true;
  68. this.dropDown.FocusOnOpen = false;
  69. this.dropDown.AutoClose = false;
  70. this.dropDown.Opening += this.dropDown_Opening;
  71. this.dropDown.Closed += this.dropDown_Closed;
  72. this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
  73. this.Leave += this.DKListBoxComboBox_Leave;
  74. this.KeyDown += this.DKListBoxComboBox_KeyDown;
  75. this.TextChanged += this.DKListBoxComboBox_TextChanged;
  76. this.KeyUp += this.DKListBoxComboBox_KeyUp;
  77. this.LocationChanged += this.DKListBoxComboBox_LocationChanged;
  78. this.SizeChanged += this.DKListBoxComboBox_SizeChanged;
  79. //this.DropDown += DKListBoxComboBox_DropDown;
  80. //this.DropDownClosed += DKListBoxComboBox_DropDownClosed;
  81. }
  82. void ShowCbDropDown()
  83. {
  84. //this.OnDropDown(null);
  85. this.ShowDropDown();
  86. }
  87. void HideCbDropDown()
  88. {
  89. //this.OnDropDownClosed(null);
  90. this.HideDropDown();
  91. }
  92. //void DKListBoxComboBox_DropDownClosed(object sender, EventArgs e)
  93. //{
  94. // try
  95. // {
  96. // this.HideDropDown();
  97. // }
  98. // catch (Exception ex)
  99. // {
  100. // throw ex;
  101. // }
  102. //}
  103. //void DKListBoxComboBox_DropDown(object sender, EventArgs e)
  104. //{
  105. // try
  106. // {
  107. // this.ShowDropDown();
  108. // }
  109. // catch (Exception ex)
  110. // {
  111. // throw ex;
  112. // }
  113. //}
  114. #endregion
  115. #region 单例模式
  116. #endregion
  117. #region 属性
  118. /// <summary>
  119. /// 获取或设置控件的数据源
  120. /// </summary>
  121. public new DataTable DataSource
  122. {
  123. get
  124. {
  125. return this._listBox.DataSource as DataTable;
  126. }
  127. set
  128. {
  129. this._listBox.DataSource = value;
  130. }
  131. }
  132. /// <summary>
  133. /// 获取或设置一个字符串,该字符串指定要显示其内容的列表框中所含对象的属性
  134. /// </summary>
  135. public new string DisplayMember
  136. {
  137. get
  138. {
  139. return this._listBox.DisplayMember;
  140. }
  141. set
  142. {
  143. this._listBox.DisplayMember = value;
  144. }
  145. }
  146. /// <summary>
  147. /// 获取或设置一个字符串,该字符串指定要从中取值的数据源的属性
  148. /// </summary>
  149. public new string ValueMember
  150. {
  151. get
  152. {
  153. return this._listBox.ValueMember;
  154. }
  155. set
  156. {
  157. this._listBox.ValueMember = value;
  158. }
  159. }
  160. /// <summary>
  161. /// 设定或者获取控件是否只能从下拉选项中选择值
  162. /// </summary>
  163. [Description("设定或者获取控件是否只能从下拉选项中选择值")]
  164. [DefaultValue(true)]
  165. public bool DropDownOnly
  166. {
  167. get
  168. {
  169. return this._dropDownOnly;
  170. }
  171. set
  172. {
  173. this._dropDownOnly = value;
  174. }
  175. }
  176. /// <summary>
  177. /// Items
  178. /// </summary>
  179. [Browsable(true)]
  180. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  181. public new ListBox.ObjectCollection Items
  182. {
  183. get
  184. {
  185. return this._listBox.Items;
  186. }
  187. }
  188. /// <summary>
  189. /// 选项
  190. /// </summary>
  191. [Browsable(true)]
  192. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  193. public C_ListBox ListBox
  194. {
  195. get
  196. {
  197. return this._listBox;
  198. }
  199. }
  200. public new object SelectedValue
  201. {
  202. get
  203. {
  204. return this._listBox.SelectedValue;
  205. }
  206. set
  207. {
  208. this.SetDataSource(null);
  209. if (value != null)
  210. {
  211. this._listBox.SelectedValue = value;
  212. }
  213. }
  214. }
  215. public new string Text
  216. {
  217. get
  218. {
  219. return base.Text;
  220. }
  221. set
  222. {
  223. base.Text = value;
  224. //this.SetDataSource(value);
  225. this.ListBox_SelectedIndexChanged(this, null);
  226. }
  227. }
  228. #endregion
  229. #region 事件处理
  230. /// <summary>
  231. /// 下拉列表打开
  232. /// </summary>
  233. /// <param name="sender"></param>
  234. /// <param name="e"></param>
  235. private void dropDown_Opening(object sender, CancelEventArgs e)
  236. {
  237. //try
  238. //{
  239. // this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
  240. // this._listBox.ClearSelected();
  241. // this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
  242. //}
  243. //catch (Exception ex)
  244. //{
  245. // throw ex;
  246. //}
  247. }
  248. /// <summary>
  249. /// 下拉列表关闭
  250. /// </summary>
  251. /// <param name="sender"></param>
  252. /// <param name="e"></param>
  253. private void dropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  254. {
  255. }
  256. /// <summary>
  257. /// 文本框值改变
  258. /// </summary>
  259. /// <param name="sender"></param>
  260. /// <param name="e"></param>
  261. private void DKListBoxComboBox_TextChanged(object sender, EventArgs e)
  262. {
  263. try
  264. {
  265. this.SetDataSource(base.Text);
  266. }
  267. catch (Exception ex)
  268. {
  269. throw ex;
  270. }
  271. }
  272. /// <summary>
  273. ///
  274. /// </summary>
  275. /// <param name="sender"></param>
  276. /// <param name="e"></param>
  277. private void DKListBoxComboBox_KeyUp(object sender, KeyEventArgs e)
  278. {
  279. try
  280. {
  281. int boxSelectedIndex = this.ListBox.SelectedIndex;
  282. int boxItemCount = this.ListBox.Items.Count;
  283. switch (e.KeyCode)
  284. {
  285. // 通过上下箭头在待选框中移动
  286. case Keys.Up:
  287. if (boxItemCount > 0)
  288. {
  289. boxSelectedIndex--;
  290. if (boxSelectedIndex < -1)
  291. {
  292. boxSelectedIndex = boxItemCount - 1;
  293. }
  294. this._isDropDownClose = false;
  295. this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
  296. if (boxSelectedIndex == -1)
  297. {
  298. this.ListBox.ClearSelected();
  299. }
  300. else
  301. {
  302. this.ListBox.SelectedIndex = boxSelectedIndex;
  303. }
  304. this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
  305. this._isDropDownClose = true;
  306. }
  307. break;
  308. case Keys.Down:
  309. if (boxItemCount > 0)
  310. {
  311. boxSelectedIndex++;
  312. if (boxSelectedIndex >= boxItemCount)
  313. {
  314. boxSelectedIndex = -1;
  315. }
  316. this._isDropDownClose = false;
  317. this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
  318. if (boxSelectedIndex == -1)
  319. {
  320. this._listBox.ClearSelected();
  321. this._listBox.SelectedItem = null;
  322. }
  323. else
  324. {
  325. this._listBox.SelectedIndex = boxSelectedIndex;
  326. }
  327. this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
  328. this._isDropDownClose = true;
  329. }
  330. break;
  331. case Keys.Escape:
  332. // ESC隐藏提示
  333. //this.ResetSelectedValue();
  334. break;
  335. case Keys.Return:
  336. // 回车选择 在输入法中按回车也会响应这个事件
  337. this.ResetSelectedValue();
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. catch (Exception ex)
  344. {
  345. throw ex;
  346. }
  347. }
  348. /// <summary>
  349. /// 输入文本框值
  350. /// </summary>
  351. /// <param name="sender"></param>
  352. /// <param name="e"></param>
  353. private void DKListBoxComboBox_KeyDown(object sender, KeyEventArgs e)
  354. {
  355. try
  356. {
  357. this.ShowCbDropDown();
  358. }
  359. catch (Exception ex)
  360. {
  361. throw ex;
  362. }
  363. }
  364. /// <summary>
  365. /// Form大小改变
  366. /// </summary>
  367. /// <param name="sender"></param>
  368. /// <param name="e"></param>
  369. private void form_SizeChanged(object sender, EventArgs e)
  370. {
  371. try
  372. {
  373. this.HideCbDropDown();
  374. Control control = sender as Control;
  375. if (control != null)
  376. {
  377. control.Refresh();
  378. }
  379. }
  380. catch (Exception ex)
  381. {
  382. throw ex;
  383. }
  384. }
  385. /// <summary>
  386. /// Form位置改变
  387. /// </summary>
  388. /// <param name="sender"></param>
  389. /// <param name="e"></param>
  390. private void form_LocationChanged(object sender, EventArgs e)
  391. {
  392. try
  393. {
  394. this.HideCbDropDown();
  395. }
  396. catch (Exception ex)
  397. {
  398. throw ex;
  399. }
  400. }
  401. /// <summary>
  402. /// Form失去焦点时
  403. /// </summary>
  404. /// <param name="sender"></param>
  405. /// <param name="e"></param>
  406. private void form_Deactivate(object sender, EventArgs e)
  407. {
  408. try
  409. {
  410. // listbox不能改变大小,不能移动滚动条
  411. //this.HideCbDropDown();
  412. }
  413. catch (Exception ex)
  414. {
  415. throw ex;
  416. }
  417. }
  418. /// <summary>
  419. /// Form激活
  420. /// </summary>
  421. /// <param name="sender"></param>
  422. /// <param name="e"></param>
  423. void form_Activated(object sender, EventArgs e)
  424. {
  425. try
  426. {
  427. this.HideCbDropDown();
  428. }
  429. catch (Exception ex)
  430. {
  431. throw ex;
  432. }
  433. }
  434. /// <summary>
  435. /// 焦点离开
  436. /// </summary>
  437. /// <param name="sender"></param>
  438. /// <param name="e"></param>
  439. private void DKListBoxComboBox_Leave(object sender, EventArgs e)
  440. {
  441. try
  442. {
  443. this.ResetSelectedValue();
  444. }
  445. catch (Exception ex)
  446. {
  447. throw ex;
  448. }
  449. }
  450. /// <summary>
  451. /// 选项改变
  452. /// </summary>
  453. /// <param name="sender"></param>
  454. /// <param name="e"></param>
  455. private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
  456. {
  457. try
  458. {
  459. if (this._listBox.SelectedItem == null)
  460. {
  461. base.Text = string.Empty;
  462. }
  463. else
  464. {
  465. this.TextChanged -= this.DKListBoxComboBox_TextChanged;
  466. base.Text = this._listBox.GetItemText(this._listBox.SelectedItem);
  467. this.TextChanged += this.DKListBoxComboBox_TextChanged;
  468. }
  469. this.SelectionStart = base.Text.Length;
  470. this.HideDropDown();
  471. }
  472. catch (Exception ex)
  473. {
  474. throw ex;
  475. }
  476. }
  477. /// <summary>
  478. /// 尺寸改变
  479. /// </summary>
  480. /// <param name="sender"></param>
  481. /// <param name="e"></param>
  482. private void DKListBoxComboBox_SizeChanged(object sender, EventArgs e)
  483. {
  484. try
  485. {
  486. this.HideCbDropDown();
  487. }
  488. catch (Exception ex)
  489. {
  490. throw ex;
  491. }
  492. }
  493. /// <summary>
  494. /// 位置改变
  495. /// </summary>
  496. /// <param name="sender"></param>
  497. /// <param name="e"></param>
  498. private void DKListBoxComboBox_LocationChanged(object sender, EventArgs e)
  499. {
  500. try
  501. {
  502. this.HideCbDropDown();
  503. }
  504. catch (Exception ex)
  505. {
  506. throw ex;
  507. }
  508. }
  509. #endregion
  510. #region 公有方法/函数
  511. /// <summary>
  512. /// 显示下拉列表
  513. /// </summary>
  514. public override void ShowDropDown()
  515. {
  516. if (this.Enabled && !this.dropDown.Visible)
  517. {
  518. if (!_isSetting)
  519. {
  520. _isSetting = true;
  521. Control control = this.Parent;
  522. while (control != null && !(control is Form))
  523. {
  524. control = control.Parent;
  525. }
  526. Form form = control as Form;
  527. if (form != null)
  528. {
  529. form.LocationChanged += form_LocationChanged;
  530. form.SizeChanged += form_SizeChanged;
  531. form.Deactivate += form_Deactivate;
  532. form.Activated += form_Activated;
  533. }
  534. }
  535. //int itemCount = this.ListBox.Items.Count;
  536. //this._containerControl.Height = itemCount * 20;
  537. this._isDropDownShowing = true;
  538. base.ShowDropDown();
  539. this._isDropDownShowing = false;
  540. int selectionStart = this.SelectionStart;
  541. int selectionLength = this.SelectionLength;
  542. this.Focus();
  543. this.SelectionStart = selectionStart;
  544. this.SelectionLength = selectionLength;
  545. }
  546. }
  547. /// <summary>
  548. /// 关闭下拉列表
  549. /// </summary>
  550. public override void HideDropDown()
  551. {
  552. if (this.dropDown.Visible && this._isDropDownClose)
  553. {
  554. base.HideDropDown();
  555. this.dropDown.Close();
  556. this.dropDown.Visible = false;
  557. }
  558. }
  559. #endregion
  560. #region 受保护方法/函数
  561. /// <summary>
  562. /// 改变大小
  563. /// </summary>
  564. /// <param name="e"></param>
  565. protected override void OnResize(EventArgs e)
  566. {
  567. // When the ComboBox is resized, the width of the dropdown
  568. // is also resized to match the width of the ComboBox. I think it looks better.
  569. Size Size = new Size(Width, DropDownControl.Height);
  570. dropDown.Size = Size;
  571. base.OnResize(e);
  572. }
  573. #endregion
  574. #region 私有方法/函数
  575. /// <summary>
  576. ///
  577. /// </summary>
  578. internal void ResetSelectedValue()
  579. {
  580. if (this._isDropDownShowing)
  581. {
  582. return;
  583. }
  584. this.HideCbDropDown();
  585. if (this._listBox.SelectedItem != null)
  586. {
  587. base.Text = this._listBox.GetItemText(this._listBox.SelectedItem);
  588. this.SelectionStart = base.Text.Length;
  589. return;
  590. }
  591. if (this._dropDownOnly && !string.IsNullOrEmpty(base.Text))
  592. {
  593. bool hasValue = false;
  594. foreach (object item in this._listBox.Items)
  595. {
  596. string itemText = this._listBox.GetItemText(item);
  597. if (itemText.Contains(this.Text))
  598. {
  599. base.Text = itemText;
  600. this._listBox.SelectedItem = item;
  601. hasValue = true;
  602. break;
  603. }
  604. }
  605. if (!hasValue)
  606. {
  607. base.Text = string.Empty;
  608. this._listBox.ClearSelected();
  609. }
  610. this.SelectionStart = base.Text.Length;
  611. }
  612. }
  613. /// <summary>
  614. /// 模糊过滤数据
  615. /// </summary>
  616. /// <param name="text"></param>
  617. private void SetDataSource(string text)
  618. {
  619. DataTable dtDataSource = this._listBox.DataSource as DataTable;
  620. if (dtDataSource == null)
  621. {
  622. return;
  623. }
  624. this._listBox.SelectedIndexChanged -= this.ListBox_SelectedIndexChanged;
  625. if (string.IsNullOrEmpty(text)
  626. || string.IsNullOrEmpty(this._listBox.DisplayMember)
  627. || string.IsNullOrEmpty(this._listBox.ValueMember))
  628. {
  629. dtDataSource.DefaultView.RowFilter = null;
  630. }
  631. else
  632. {
  633. dtDataSource.DefaultView.RowFilter = this._listBox.DisplayMember + " LIKE '%" + Utility.SelectFilterLike(text) + "%'";
  634. }
  635. if (string.IsNullOrEmpty(text))
  636. {
  637. this._listBox.ClearSelected();
  638. this._listBox.SelectedItem = null;
  639. this.TextChanged -= this.DKListBoxComboBox_TextChanged;
  640. base.Text = text;
  641. this.TextChanged += this.DKListBoxComboBox_TextChanged;
  642. }
  643. this._listBox.SelectedIndexChanged += this.ListBox_SelectedIndexChanged;
  644. }
  645. #endregion
  646. }
  647. }