PopupComboBox.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. using System;
  2. using System.ComponentModel;
  3. using System.Security.Permissions;
  4. using System.Windows.Forms;
  5. using Dongke.WinForm.Utilities;
  6. namespace Dongke.WinForm.Controls
  7. {
  8. /// <summary>
  9. /// 自定义弹出下拉框控件
  10. /// </summary>
  11. public abstract class PopupComboBox : CmbComboBox
  12. {
  13. #region 事件声明
  14. #region EditReadOnlyChanged
  15. /// <summary>
  16. /// 当 EditReadOnly 属性的值更改时发生
  17. /// </summary>
  18. [Description("当 EditReadOnly 属性的值更改时发生。"), Category("CustomerEx")]
  19. [Browsable(false)]
  20. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  21. [EditorBrowsable(EditorBrowsableState.Never)]
  22. public new event EventHandler EditReadOnlyChanged
  23. {
  24. add
  25. {
  26. base.EditReadOnlyChanged += value;
  27. }
  28. remove
  29. {
  30. base.EditReadOnlyChanged -= value;
  31. }
  32. }
  33. #endregion
  34. #endregion
  35. #region 成员变量
  36. /// <summary>
  37. /// 弹出控件
  38. /// </summary>
  39. private PopupControlHost _popupDropDown = null;
  40. /// <summary>
  41. /// 弹出控件
  42. /// </summary>
  43. private Control _dropDownControl = null;
  44. /// <summary>
  45. /// 是否显示原有的ListBox
  46. /// </summary>
  47. private bool _showComboBoxDropDown = false;
  48. private int _dropDownHeight = 0;
  49. private int _dropDownWidth = 0;
  50. private bool _integralHeight = false;
  51. #endregion
  52. #region 构造函数
  53. /// <summary>
  54. /// 自定义弹出下拉框控件
  55. /// </summary>
  56. public PopupComboBox()
  57. {
  58. base.FormattingEnabled = false;
  59. base.EditReadOnly = true;
  60. this._popupDropDown = new PopupControlHost(this);
  61. this._popupDropDown.SelectedOK += PopupDropDown_SelectedOK;
  62. this._popupDropDown.SelectedCancel += PopupDropDown_SelectedCancel;
  63. this.SetShowComboBoxDropDown();
  64. }
  65. #endregion
  66. #region 属性
  67. /// <summary>
  68. /// 自定义的下拉项目
  69. /// </summary>
  70. protected PopupControlHost PopupDropDown
  71. {
  72. get
  73. {
  74. return this._popupDropDown;
  75. }
  76. }
  77. /// <summary>
  78. /// Gets or sets the drop down control.
  79. /// </summary>
  80. /// <value>The drop down control.</value>
  81. protected Control DropDownControl
  82. {
  83. get
  84. {
  85. return this._dropDownControl;
  86. }
  87. set
  88. {
  89. if (this._dropDownControl != value)
  90. {
  91. this._dropDownControl = value;
  92. this._popupDropDown.Content = this._dropDownControl;
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 获取或设置一个值,该值指示是否显示原有的ListBox。
  98. /// </summary>
  99. protected virtual bool ShowComboBoxDropDown
  100. {
  101. get
  102. {
  103. return this._showComboBoxDropDown;
  104. }
  105. set
  106. {
  107. if (this._showComboBoxDropDown != value)
  108. {
  109. this._showComboBoxDropDown = value;
  110. this.SetShowComboBoxDropDown();
  111. }
  112. }
  113. }
  114. #endregion
  115. #region 重写属性
  116. /// <summary>
  117. /// 获取或设置与此控件关联的文本。
  118. /// </summary>
  119. public override string Text
  120. {
  121. get
  122. {
  123. return base.Text;
  124. }
  125. set
  126. {
  127. //base.Text = value;
  128. }
  129. }
  130. /// <summary>
  131. /// 获取或设置一个值,该值指示文本框中的文本是否为只读。
  132. /// </summary>
  133. [Browsable(false)]
  134. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  135. [EditorBrowsable(EditorBrowsableState.Never)]
  136. public override bool EditReadOnly
  137. {
  138. get
  139. {
  140. return base.EditReadOnly;
  141. }
  142. set
  143. {
  144. base.EditReadOnly = value;
  145. }
  146. }
  147. /// <summary>
  148. /// 获取或设置组合框下拉部分的宽度。
  149. /// </summary>
  150. [Browsable(false)]
  151. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  152. [EditorBrowsable(EditorBrowsableState.Never)]
  153. public new virtual int DropDownWidth
  154. {
  155. get
  156. {
  157. if (this._showComboBoxDropDown)
  158. {
  159. return base.DropDownWidth;
  160. }
  161. return this._popupDropDown.Width;
  162. }
  163. set
  164. {
  165. if (value > 0)
  166. {
  167. if (this._showComboBoxDropDown)
  168. {
  169. base.DropDownWidth = value;
  170. }
  171. else
  172. {
  173. this._popupDropDown.Width = value;
  174. }
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// 获取或设置 System.Windows.Forms.ComboBox 下拉部分的高度(以像素为单位)。
  180. /// </summary>
  181. [Browsable(false)]
  182. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  183. [EditorBrowsable(EditorBrowsableState.Never)]
  184. public new virtual int DropDownHeight
  185. {
  186. get
  187. {
  188. if (this._showComboBoxDropDown)
  189. {
  190. return base.DropDownHeight;
  191. }
  192. return this._popupDropDown.Height;
  193. }
  194. set
  195. {
  196. if (value > 0)
  197. {
  198. if (this._showComboBoxDropDown)
  199. {
  200. base.DropDownHeight = value;
  201. }
  202. else
  203. {
  204. this._popupDropDown.Height = value;
  205. }
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 获取或设置一个值,该值指示控件是否应调整大小以避免只显示项的局部。
  211. /// </summary>
  212. [Browsable(false)]
  213. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  214. [EditorBrowsable(EditorBrowsableState.Never)]
  215. public new virtual bool IntegralHeight
  216. {
  217. get
  218. {
  219. return base.IntegralHeight;
  220. }
  221. set
  222. {
  223. if (this._showComboBoxDropDown)
  224. {
  225. base.IntegralHeight = value;
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 获取一个值,该值指示控件是否具有焦点。
  231. /// </summary>
  232. public override bool Focused
  233. {
  234. get
  235. {
  236. if (base.Focused)
  237. {
  238. return true;
  239. }
  240. return this._popupDropDown.Focused || this._dropDownControl.Focused;
  241. }
  242. }
  243. /// <summary>
  244. /// 获取或设置在 System.Windows.Forms.ComboBox.AutoCompleteSource 属性设置为 CustomSource
  245. /// 时要使用的自定义 System.Collections.Specialized.StringCollection。
  246. /// </summary>
  247. [Browsable(false)]
  248. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  249. [EditorBrowsable(EditorBrowsableState.Never)]
  250. public new AutoCompleteStringCollection AutoCompleteCustomSource
  251. {
  252. get
  253. {
  254. return base.AutoCompleteCustomSource;
  255. }
  256. set
  257. {
  258. base.AutoCompleteCustomSource = value;
  259. }
  260. }
  261. /// <summary>
  262. /// 获取或设置控制自动完成如何作用于 System.Windows.Forms.ComboBox 的选项。
  263. /// </summary>
  264. [Browsable(false)]
  265. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  266. [EditorBrowsable(EditorBrowsableState.Never)]
  267. public new AutoCompleteMode AutoCompleteMode
  268. {
  269. get
  270. {
  271. return base.AutoCompleteMode;
  272. }
  273. set
  274. {
  275. base.AutoCompleteMode = value;
  276. }
  277. }
  278. /// <summary>
  279. /// 获取或设置一个值,该值指定用于自动完成的完整字符串源。
  280. /// </summary>
  281. [Browsable(false)]
  282. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  283. [EditorBrowsable(EditorBrowsableState.Never)]
  284. public new AutoCompleteSource AutoCompleteSource
  285. {
  286. get
  287. {
  288. return base.AutoCompleteSource;
  289. }
  290. set
  291. {
  292. base.AutoCompleteSource = value;
  293. }
  294. }
  295. #endregion
  296. #region 重写事件
  297. #region 属性改变
  298. /// <summary>
  299. /// 字体改变事件
  300. /// </summary>
  301. /// <param name="e"></param>
  302. protected override void OnFontChanged(EventArgs e)
  303. {
  304. base.OnAutoSizeChanged(e);
  305. this._dropDownControl.Font = this.Font;
  306. }
  307. /// <summary>
  308. /// 背景色改变事件
  309. /// </summary>
  310. /// <param name="e"></param>
  311. protected override void OnBackColorChanged(EventArgs e)
  312. {
  313. base.OnBackColorChanged(e);
  314. this._dropDownControl.BackColor = this.BackColor;
  315. }
  316. #endregion
  317. #region 行为事件
  318. /// <summary>
  319. /// 当显示下拉部分时
  320. /// </summary>
  321. /// <param name="e"></param>
  322. protected override void OnDropDown(EventArgs e)
  323. {
  324. base.OnDropDown(e);
  325. }
  326. /// <summary>
  327. /// 当下拉部分不再可见时
  328. /// </summary>
  329. /// <param name="e"></param>
  330. protected override void OnDropDownClosed(EventArgs e)
  331. {
  332. base.OnDropDownClosed(e);
  333. }
  334. #endregion
  335. #region 键盘事件
  336. #endregion
  337. #region 焦点事件
  338. #endregion
  339. #region 鼠标事件
  340. #endregion
  341. #endregion
  342. #region 重写方法
  343. /// <summary>
  344. /// 清理所有正在使用的资源。
  345. /// </summary>
  346. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  347. protected override void Dispose(bool disposing)
  348. {
  349. if (disposing)
  350. {
  351. if (this._popupDropDown != null)
  352. {
  353. this._popupDropDown.Dispose();
  354. }
  355. if (this._dropDownControl != null)
  356. {
  357. this._dropDownControl.Dispose();
  358. }
  359. }
  360. base.Dispose(disposing);
  361. }
  362. /// <summary>
  363. /// Processes Windows messages.
  364. /// </summary>
  365. /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param>
  366. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  367. protected override void WndProc(ref Message m)
  368. {
  369. if (!this.ReadOnly)
  370. {
  371. if (m.Msg == (int)WindowsMessage.WM_LBUTTONDBLCLK ||
  372. m.Msg == (int)WindowsMessage.WM_LBUTTONDOWN)
  373. {
  374. if (!this._showComboBoxDropDown)
  375. {
  376. if (this._popupDropDown.IsOpen)
  377. {
  378. this.CloseDropDown();
  379. }
  380. else
  381. {
  382. this.ShowDropDown();
  383. }
  384. return;
  385. }
  386. }
  387. }
  388. base.WndProc(ref m);
  389. }
  390. #endregion
  391. #region 事件处理
  392. /// <summary>
  393. /// 确定
  394. /// </summary>
  395. /// <param name="sender"></param>
  396. /// <param name="e"></param>
  397. private void PopupDropDown_SelectedOK(object sender, EventArgs e)
  398. {
  399. this.PopupSelectedOK();
  400. }
  401. /// <summary>
  402. /// 取消
  403. /// </summary>
  404. /// <param name="sender"></param>
  405. /// <param name="e"></param>
  406. private void PopupDropDown_SelectedCancel(object sender, EventArgs e)
  407. {
  408. this.PopupSelectedCancel();
  409. }
  410. #endregion
  411. #region 公有方法
  412. /// <summary>
  413. /// 显示下拉列表。
  414. /// </summary>
  415. public virtual void ShowDropDown()
  416. {
  417. if (this.ReadOnly)
  418. {
  419. return;
  420. }
  421. if (this._popupDropDown != null &&
  422. !this._popupDropDown.IsOpen)
  423. {
  424. this.Focus();
  425. this._popupDropDown.Show();
  426. }
  427. }
  428. /// <summary>
  429. /// 关闭下拉列表。
  430. /// </summary>
  431. public virtual void CloseDropDown()
  432. {
  433. if (this._popupDropDown != null &&
  434. this._popupDropDown.IsOpen)
  435. {
  436. this._popupDropDown.Close();
  437. }
  438. }
  439. #endregion
  440. #region 内部方法
  441. #endregion
  442. #region 保护方法
  443. /// <summary>
  444. /// 确定
  445. /// </summary>
  446. protected virtual void PopupSelectedOK()
  447. {
  448. }
  449. /// <summary>
  450. /// 取消
  451. /// </summary>
  452. protected virtual void PopupSelectedCancel()
  453. {
  454. }
  455. /// <summary>
  456. /// 设置文本
  457. /// </summary>
  458. /// <param name="text">文本</param>
  459. protected virtual void SetText(string text)
  460. {
  461. base.Text = text;
  462. }
  463. #endregion
  464. #region 私有方法
  465. /// <summary>
  466. /// 设置下拉列表
  467. /// </summary>
  468. private void SetShowComboBoxDropDown()
  469. {
  470. if (this._showComboBoxDropDown)
  471. {
  472. base.DropDownHeight = this._dropDownHeight;
  473. base.DropDownWidth = this._dropDownWidth;
  474. base.IntegralHeight = this._integralHeight;
  475. }
  476. else
  477. {
  478. this._dropDownHeight = base.DropDownHeight;
  479. this._dropDownWidth = base.DropDownWidth;
  480. this._integralHeight = base.IntegralHeight;
  481. base.DropDownHeight = 1;
  482. base.DropDownWidth = 1;
  483. base.IntegralHeight = false;
  484. }
  485. }
  486. #endregion
  487. }
  488. }