CmbComboBox.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using Dongke.WinForm.Utilities;
  8. namespace Dongke.WinForm.Controls
  9. {
  10. /// <summary>
  11. /// 标准下拉框控件
  12. /// </summary>
  13. [ToolboxBitmap(typeof(ComboBox))]
  14. public class CmbComboBox : ComboBox, IDKControl, IDataVerifiable, IAsyncControl, IChildNativeWindow
  15. {
  16. #region 内部类
  17. #endregion
  18. #region 事件声明
  19. #region HasErrorChanged
  20. /// <summary>
  21. /// 当 HasError 属性的值更改时发生。
  22. /// </summary>
  23. private static readonly object EventHasErrorChanged = new object();
  24. /// <summary>
  25. /// 当 HasError 属性的值更改时发生。
  26. /// </summary>
  27. [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")]
  28. public event EventHandler HasErrorChanged
  29. {
  30. add
  31. {
  32. base.Events.AddHandler(EventHasErrorChanged, value);
  33. }
  34. remove
  35. {
  36. base.Events.RemoveHandler(EventHasErrorChanged, value);
  37. }
  38. }
  39. /// <summary>
  40. /// 引发 HasErrorChanged 事件
  41. /// </summary>
  42. /// <param name="e">包含事件数据的 EventArgs</param>
  43. protected virtual void OnHasErrorChanged(EventArgs e)
  44. {
  45. EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged];
  46. if (eventHandler != null)
  47. {
  48. eventHandler(this, e);
  49. }
  50. }
  51. #endregion
  52. #region ReadOnlyChanged
  53. /// <summary>
  54. /// 当 ReadOnly 属性的值更改时发生。
  55. /// </summary>
  56. private static readonly object EventReadOnlyChanged = new object();
  57. /// <summary>
  58. /// 当 ReadOnly 属性的值更改时发生
  59. /// </summary>
  60. [Description("当 ReadOnly 属性的值更改时发生。"), Category("CustomerEx")]
  61. public event EventHandler ReadOnlyChanged
  62. {
  63. add
  64. {
  65. base.Events.AddHandler(EventReadOnlyChanged, value);
  66. }
  67. remove
  68. {
  69. base.Events.RemoveHandler(EventReadOnlyChanged, value);
  70. }
  71. }
  72. /// <summary>
  73. /// 引发 ReadOnlyChanged 事件
  74. /// </summary>
  75. /// <param name="e"></param>
  76. protected virtual void OnReadOnlyChanged(EventArgs e)
  77. {
  78. EventHandler eventHandler = base.Events[EventReadOnlyChanged] as EventHandler;
  79. if (eventHandler != null)
  80. {
  81. eventHandler(this, e);
  82. }
  83. }
  84. #endregion
  85. #region EditReadOnlyChanged
  86. /// <summary>
  87. /// 当 EditReadOnly 属性的值更改时发生。
  88. /// </summary>
  89. private static readonly object EventEditReadOnlyChanged = new object();
  90. /// <summary>
  91. /// 当 EditReadOnly 属性的值更改时发生
  92. /// </summary>
  93. [Description("当 EditReadOnly 属性的值更改时发生。"), Category("CustomerEx")]
  94. public event EventHandler EditReadOnlyChanged
  95. {
  96. add
  97. {
  98. base.Events.AddHandler(EventEditReadOnlyChanged, value);
  99. }
  100. remove
  101. {
  102. base.Events.RemoveHandler(EventEditReadOnlyChanged, value);
  103. }
  104. }
  105. /// <summary>
  106. /// 引发 EditReadOnlyChanged 事件
  107. /// </summary>
  108. /// <param name="e"></param>
  109. protected virtual void OnEditReadOnlyChanged(EventArgs e)
  110. {
  111. EventHandler eventHandler = base.Events[EventEditReadOnlyChanged] as EventHandler;
  112. if (eventHandler != null)
  113. {
  114. eventHandler(this, e);
  115. }
  116. }
  117. #endregion
  118. #endregion
  119. #region 成员变量
  120. /// <summary>
  121. /// 焦点是否进入控件
  122. /// </summary>
  123. private bool _entered = false;
  124. /// <summary>
  125. /// 鼠标是否进入控件
  126. /// </summary>
  127. private bool _mouseOver = false;
  128. /// <summary>
  129. /// 边框颜色
  130. /// </summary>
  131. private Color? _borderColor = null;
  132. /// <summary>
  133. /// 边框样式
  134. /// </summary>
  135. private BorderStyle _borderStyle = BorderStyle.Fixed3D;
  136. /// <summary>
  137. /// 指示控件中的文本是否为只读
  138. /// </summary>
  139. private bool _editReadOnly = true;
  140. /// <summary>
  141. /// 指示控件中是否为只读
  142. /// </summary>
  143. private bool _readOnly = false;
  144. /// <summary>
  145. /// ComboBox内部的Edit
  146. /// </summary>
  147. private ChildNativeWindow _childEdit = null;
  148. private bool _hasNullData = false;
  149. #endregion
  150. #region 构造函数
  151. /// <summary>
  152. /// 标准复选框列表控件
  153. /// </summary>
  154. public CmbComboBox()
  155. {
  156. base.FormattingEnabled = false;
  157. CommonSetting.InitControls(this);
  158. }
  159. #endregion
  160. #region 属性
  161. /// <summary>
  162. /// 获取一个值,该值指示控件是否有输入焦点。
  163. /// </summary>
  164. [DefaultValue(false)]
  165. [Browsable(false)]
  166. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  167. [EditorBrowsable(EditorBrowsableState.Advanced)]
  168. public virtual bool Entered
  169. {
  170. get
  171. {
  172. return this._entered;
  173. }
  174. protected set
  175. {
  176. if (this._entered != value)
  177. {
  178. this._entered = value;
  179. this.InvalidateBorder();
  180. }
  181. }
  182. }
  183. /// <summary>
  184. /// 获取一个值,该值指示鼠标是否在控件上方。
  185. /// </summary>
  186. [DefaultValue(false)]
  187. [Browsable(false)]
  188. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  189. [EditorBrowsable(EditorBrowsableState.Advanced)]
  190. public virtual bool MouseOver
  191. {
  192. get
  193. {
  194. return this._mouseOver;
  195. }
  196. protected set
  197. {
  198. if (this._mouseOver != value)
  199. {
  200. this._mouseOver = value;
  201. this.InvalidateBorder();
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 获取或设置控件四周绘制的边框的类型
  207. /// </summary>
  208. [Description("获取或设置控件四周绘制的边框的类型。"), Category("CustomerEx")]
  209. [DefaultValue(BorderStyle.Fixed3D)]
  210. //[Browsable(false)]
  211. //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  212. //[EditorBrowsable(EditorBrowsableState.Advanced)]
  213. public virtual BorderStyle BorderStyle
  214. {
  215. get
  216. {
  217. return this._borderStyle;
  218. }
  219. set
  220. {
  221. if (!Enum.IsDefined(typeof(BorderStyle), value))
  222. {
  223. throw new InvalidEnumArgumentException("value", (int)value, typeof(BorderStyle));
  224. }
  225. if (this._borderStyle != value)
  226. {
  227. this._borderStyle = value;
  228. base.UpdateStyles();
  229. base.RecreateHandle();
  230. this.InvalidateBorder();
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// 获取或设置一个值,该值指示此控件是否只读。
  236. /// </summary>
  237. [Description("获取或设置一个值,该值指示此控件是否只读。"), Category("CustomerEx")]
  238. [DefaultValue(false)]
  239. public virtual bool ReadOnly
  240. {
  241. get
  242. {
  243. return this._readOnly;
  244. }
  245. set
  246. {
  247. if (this._readOnly != value)
  248. {
  249. this._readOnly = value;
  250. this.SetEditReadOnly();
  251. this.OnReadOnlyChanged(EventArgs.Empty);
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// 获取或设置一个值,该值指示文本框中的文本是否为只读。
  257. /// </summary>
  258. [Description("获取或设置一个值,该值指示文本框中的文本是否为只读。"), Category("CustomerEx")]
  259. [DefaultValue(true)]
  260. public virtual bool EditReadOnly
  261. {
  262. get
  263. {
  264. return this._editReadOnly;
  265. }
  266. set
  267. {
  268. if (this._editReadOnly != value)
  269. {
  270. this._editReadOnly = value;
  271. this.SetEditReadOnly();
  272. this.OnEditReadOnlyChanged(EventArgs.Empty);
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// 获取或设置一个值,该值指示是否向数据源中插入null选项(数据源为DataTable)。
  278. /// </summary>
  279. [Description("获取或设置一个值,该值指示是否向数据源中插入null选项(数据源为DataTable)。"), Category("CustomerEx")]
  280. [DefaultValue(false)]
  281. public virtual bool HasNullData
  282. {
  283. get
  284. {
  285. return this._hasNullData;
  286. }
  287. set
  288. {
  289. if (this._hasNullData != value)
  290. {
  291. this._hasNullData = value;
  292. this.SetNullData(this.DataSource, true);
  293. }
  294. }
  295. }
  296. #endregion
  297. #region 重写属性
  298. /// <summary>
  299. /// 获取或设置控件的数据源
  300. /// </summary>
  301. [Description("获取或设置控件的数据源。"), Category("ListControl")]
  302. [AttributeProvider(typeof(IListSource))]
  303. [DefaultValue(null)]
  304. [RefreshProperties(RefreshProperties.Repaint)]
  305. public new virtual object DataSource
  306. {
  307. get
  308. {
  309. return base.DataSource;
  310. }
  311. set
  312. {
  313. this.SetNullData(value, false);
  314. base.DataSource = value;
  315. }
  316. }
  317. #endregion
  318. #region 重写事件
  319. #region 属性改变
  320. /// <summary>
  321. /// 句柄创建时
  322. /// </summary>
  323. /// <param name="e"></param>
  324. protected override void OnHandleCreated(EventArgs e)
  325. {
  326. base.OnHandleCreated(e);
  327. if (this._childEdit != null)
  328. {
  329. return;
  330. }
  331. //if (this.DropDownStyle != ComboBoxStyle.DropDownList)
  332. //{
  333. // IntPtr window = WindowsAPI.GetWindow(new HandleRef(this, base.Handle), 5);
  334. // if (window != IntPtr.Zero)
  335. // {
  336. // if (this.DropDownStyle == ComboBoxStyle.Simple)
  337. // {
  338. // NativeWindow childListBox = new CmbComboBox.ComboBoxChildEditNativeWindow(this);
  339. // childListBox.AssignHandle(window);
  340. // window = WindowsAPI.GetWindow(new HandleRef(this, window), 2);
  341. // }
  342. // this._childEdit = new CmbComboBox.ComboBoxChildEditNativeWindow(this);
  343. // this._childEdit.AssignHandle(window);
  344. // }
  345. //}
  346. IntPtr window = WindowsAPI.FindWindowEx(base.Handle, IntPtr.Zero, "Edit", "");
  347. this._childEdit = new ChildNativeWindow(this, "Edit");
  348. this._childEdit.AssignHandle(window);
  349. this.SetEditReadOnly();
  350. }
  351. /// <summary>
  352. /// 选择项目改变
  353. /// </summary>
  354. /// <param name="e"></param>
  355. protected override void OnSelectedIndexChanged(EventArgs e)
  356. {
  357. this.SetDefaultOnCheck();
  358. base.OnSelectedIndexChanged(e);
  359. this.ValidateData();
  360. }
  361. /// <summary>
  362. /// 数据源改变事件
  363. /// </summary>
  364. /// <param name="e"></param>
  365. protected override void OnDataSourceChanged(EventArgs e)
  366. {
  367. base.OnDataSourceChanged(e);
  368. }
  369. #endregion
  370. #region 行为事件
  371. #endregion
  372. #region 键盘事件
  373. /// <summary>
  374. /// 键盘点击
  375. /// </summary>
  376. /// <param name="e"></param>
  377. protected override void OnKeyDown(KeyEventArgs e)
  378. {
  379. base.OnKeyDown(e);
  380. if (this._readOnly)
  381. {
  382. switch (e.KeyCode)
  383. {
  384. case Keys.Up:
  385. case Keys.Down:
  386. case Keys.PageUp:
  387. case Keys.PageDown:
  388. case Keys.Back:
  389. case Keys.Delete:
  390. e.Handled = true;
  391. break;
  392. default:
  393. break;
  394. }
  395. return;
  396. }
  397. if (this._editReadOnly)
  398. {
  399. if (e.KeyCode == Keys.Delete ||
  400. e.KeyCode == Keys.Back)
  401. {
  402. if (!this._mustInput)
  403. {
  404. this.ClearValue();
  405. }
  406. e.Handled = true;
  407. return;
  408. }
  409. }
  410. if (this.Items.Count > 0)
  411. {
  412. if (e.KeyCode == Keys.Up && this.SelectedIndex < 1)
  413. {
  414. this.SelectPrevItem();
  415. this.SelectAll();
  416. e.Handled = true;
  417. return;
  418. }
  419. if (e.KeyCode == Keys.Down &&
  420. (this.SelectedIndex < 1 || this.SelectedIndex == this.Items.Count - 1))
  421. {
  422. this.SelectNextItem();
  423. this.SelectAll();
  424. e.Handled = true;
  425. return;
  426. }
  427. }
  428. }
  429. #endregion
  430. #region 焦点事件
  431. /// <summary>
  432. /// 输入焦点进入控件
  433. /// </summary>
  434. /// <param name="e"></param>
  435. protected override void OnEnter(EventArgs e)
  436. {
  437. this.Entered = true;
  438. base.OnEnter(e);
  439. }
  440. /// <summary>
  441. /// 获得焦点
  442. /// </summary>
  443. /// <param name="e"></param>
  444. protected override void OnGotFocus(EventArgs e)
  445. {
  446. base.OnGotFocus(e);
  447. }
  448. /// <summary>
  449. /// 失去焦点
  450. /// </summary>
  451. /// <param name="e"></param>
  452. protected override void OnLostFocus(EventArgs e)
  453. {
  454. base.OnLostFocus(e);
  455. }
  456. /// <summary>
  457. /// 输入焦点离开控件
  458. /// </summary>
  459. /// <param name="e"></param>
  460. protected override void OnLeave(EventArgs e)
  461. {
  462. base.OnLeave(e);
  463. //this.Entered = false;
  464. }
  465. /// <summary>
  466. /// 控件正在验证
  467. /// </summary>
  468. /// <param name="e"></param>
  469. protected override void OnValidating(CancelEventArgs e)
  470. {
  471. base.OnValidating(e);
  472. if (this.HasError && !this._canLostFocusOnError)
  473. {
  474. e.Cancel = true;
  475. }
  476. }
  477. /// <summary>
  478. /// 控件完成验证
  479. /// </summary>
  480. /// <param name="e"></param>
  481. protected override void OnValidated(EventArgs e)
  482. {
  483. this.Entered = false;
  484. base.OnValidated(e);
  485. }
  486. #endregion
  487. #region 鼠标事件
  488. /// <summary>
  489. /// 鼠标进入控件
  490. /// </summary>
  491. /// <param name="e"></param>
  492. protected override void OnMouseEnter(EventArgs e)
  493. {
  494. this.MouseOver = true;
  495. base.OnMouseEnter(e);
  496. }
  497. /// <summary>
  498. /// 鼠标离开控件
  499. /// </summary>
  500. /// <param name="e"></param>
  501. protected override void OnMouseLeave(EventArgs e)
  502. {
  503. base.OnMouseLeave(e);
  504. this.MouseOver = false;
  505. }
  506. /// <summary>
  507. /// 鼠标移动
  508. /// </summary>
  509. /// <param name="e"></param>
  510. protected override void OnMouseMove(MouseEventArgs e)
  511. {
  512. base.OnMouseMove(e);
  513. }
  514. /// <summary>
  515. /// 鼠标按下
  516. /// </summary>
  517. /// <param name="e"></param>
  518. protected override void OnMouseDown(MouseEventArgs e)
  519. {
  520. base.OnMouseDown(e);
  521. }
  522. /// <summary>
  523. /// 鼠标抬起
  524. /// </summary>
  525. /// <param name="e"></param>
  526. protected override void OnMouseUp(MouseEventArgs e)
  527. {
  528. base.OnMouseUp(e);
  529. }
  530. /// <summary>
  531. /// 鼠标滚轮滚动
  532. /// </summary>
  533. /// <param name="e"></param>
  534. protected override void OnMouseWheel(MouseEventArgs e)
  535. {
  536. base.OnMouseWheel(e);
  537. if (this._readOnly)
  538. {
  539. return;
  540. }
  541. if (this.DropDownStyle == ComboBoxStyle.Simple &&
  542. this.Items.Count > 0)
  543. {
  544. if (e.Delta > 0)
  545. {
  546. this.SelectPrevItem();
  547. }
  548. else
  549. {
  550. this.SelectNextItem();
  551. }
  552. }
  553. }
  554. #endregion
  555. #endregion
  556. #region 重写方法
  557. /// <summary>
  558. /// 处理 Windows 消息
  559. /// </summary>
  560. /// <param name="m">要处理的 Windows System.Windows.Forms.Message</param>
  561. protected override void WndProc(ref Message m)
  562. {
  563. if (this._readOnly)
  564. {
  565. if (m.Msg == (int)WindowsMessage.WM_LBUTTONDBLCLK ||
  566. m.Msg == (int)WindowsMessage.WM_LBUTTONDOWN)
  567. {
  568. return;
  569. }
  570. }
  571. base.WndProc(ref m);
  572. if (m.Msg == (int)WindowsMessage.WM_NCDESTROY)
  573. {
  574. this.ReleaseChildWindow();
  575. return;
  576. }
  577. if (m.Msg == (int)WindowsMessage.WM_PAINT ||
  578. m.Msg == (int)WindowsMessage.WM_NCPAINT ||
  579. m.Msg == (int)WindowsMessage.WM_CTLCOLOREDIT)
  580. {
  581. BorderColorPaint.WmBorderPaint(this._borderColor, this.Width, this.Height, ref m);
  582. return;
  583. }
  584. }
  585. #endregion
  586. #region 公有方法
  587. /// <summary>
  588. /// 初始化显示的值
  589. /// </summary>
  590. /// <param name="text"></param>
  591. /// <param name="value"></param>
  592. public DataRow InitValue(string text, object value)
  593. {
  594. if (this.DataManager == null)
  595. {
  596. if (this.Items.Count > 0)
  597. {
  598. foreach (object item in this.Items)
  599. {
  600. string t = this.GetItemText(item);
  601. if (t == text)
  602. {
  603. this.SelectedItem = item;
  604. return null;
  605. }
  606. }
  607. }
  608. this.Items.Insert(0, value);
  609. this.SelectedIndex = 0;
  610. //this.Text = text;
  611. return null;
  612. }
  613. DataTable data = this.DataSource as DataTable;
  614. if (data == null)
  615. {
  616. return null;
  617. }
  618. if (value == null)
  619. {
  620. value = DBNull.Value;
  621. }
  622. foreach (DataRow item in data.Rows)
  623. {
  624. object v = item[this.ValueMember];
  625. if (value.Equals(v))
  626. {
  627. this.SelectedValue = v;
  628. return null;
  629. }
  630. }
  631. DataRow row = data.NewRow();
  632. row[this.ValueMember] = value;
  633. row[this.DisplayMember] = text + "【停用】";
  634. if (this.HasNullData)
  635. {
  636. data.Rows.InsertAt(row, 1);
  637. this.SelectedIndex = 1;
  638. }
  639. else
  640. {
  641. data.Rows.InsertAt(row, 0);
  642. this.SelectedIndex = 0;
  643. }
  644. return row;
  645. }
  646. /// <summary>
  647. /// 选择下一个Item
  648. /// </summary>
  649. /// <param name="clearSelect">是否能选择空</param>
  650. public void SelectNextItem(bool clearSelect = true)
  651. {
  652. int boxItemCount = this.Items.Count;
  653. if (boxItemCount < 1)
  654. {
  655. return;
  656. }
  657. int boxSelectedIndex = this.SelectedIndex;
  658. boxSelectedIndex++;
  659. if (boxSelectedIndex >= boxItemCount)
  660. {
  661. if (this._mustInput || !clearSelect || this._hasNullData)
  662. {
  663. boxSelectedIndex = 0;
  664. }
  665. else
  666. {
  667. boxSelectedIndex = -1;
  668. }
  669. }
  670. //if (boxSelectedIndex < 0)
  671. //{
  672. // //this.ClearSelected();
  673. //}
  674. //else
  675. {
  676. this.SelectedIndex = boxSelectedIndex;
  677. }
  678. }
  679. /// <summary>
  680. /// 选择上一个Item
  681. /// </summary>
  682. /// <param name="clearSelect">是否能选择空</param>
  683. public void SelectPrevItem(bool clearSelect = true)
  684. {
  685. int boxItemCount = this.Items.Count;
  686. if (boxItemCount < 1)
  687. {
  688. return;
  689. }
  690. int boxSelectedIndex = this.SelectedIndex;
  691. boxSelectedIndex--;
  692. if (boxSelectedIndex < -1 ||
  693. ((this._mustInput || !clearSelect || this._hasNullData) && boxSelectedIndex < 0))
  694. {
  695. boxSelectedIndex = boxItemCount - 1;
  696. }
  697. //if (boxSelectedIndex < 0)
  698. //{
  699. // this.ClearSelected();
  700. //}
  701. //else
  702. {
  703. this.SelectedIndex = boxSelectedIndex;
  704. }
  705. }
  706. #endregion
  707. #region 内部方法
  708. /// <summary>
  709. /// 处理ComboBox内部Edit的消息
  710. /// </summary>
  711. /// <param name="child">Edit</param>
  712. /// <param name="m">消息</param>
  713. /// <returns>消息是否已经处理过</returns>
  714. public virtual bool ChildWndProc(ChildNativeWindow child, ref Message m)
  715. {
  716. if (this._editReadOnly || this._readOnly)
  717. {
  718. if (m.Msg == (int)WindowsMessage.WM_PASTE)
  719. {
  720. WindowsAPI.MessageBeep(BeepType.MB_OK);
  721. return true;
  722. }
  723. if (m.Msg == (int)WindowsMessage.WM_CUT)
  724. {
  725. if (this.SelectionLength > 0)
  726. {
  727. Clipboard.SetText(this.SelectedText);
  728. }
  729. WindowsAPI.MessageBeep(BeepType.MB_OK);
  730. return true;
  731. }
  732. if (m.Msg == (int)WindowsMessage.WM_CHAR)
  733. {
  734. int wp = m.WParam.ToInt32();
  735. // = Cut(0x18)
  736. if (wp == 0x18)
  737. {
  738. if (this.SelectionLength > 0)
  739. {
  740. Clipboard.SetText(this.SelectedText);
  741. }
  742. WindowsAPI.MessageBeep(BeepType.MB_OK);
  743. return true;
  744. }
  745. // = Copy(0x3) esc(27) enter(13)
  746. if (wp == 0x3 || wp == 27 || wp == 13)
  747. {
  748. return false;
  749. }
  750. // back(8)
  751. else if (wp == 8)
  752. {
  753. return this._readOnly;
  754. }
  755. else
  756. {
  757. WindowsAPI.MessageBeep(BeepType.MB_OK);
  758. return true;
  759. }
  760. }
  761. }
  762. if (m.Msg == (int)WindowsMessage.WM_LBUTTONDBLCLK)
  763. {
  764. this.OnDoubleClick(EventArgs.Empty);
  765. }
  766. return false;
  767. }
  768. #endregion
  769. #region 保护方法
  770. /// <summary>
  771. /// 引发 System.Windows.Forms.Control.KeyDown 事件。
  772. /// </summary>
  773. /// <param name="e"></param>
  774. protected virtual void OnComBoxKeyDown(KeyEventArgs e)
  775. {
  776. base.OnKeyDown(e);
  777. }
  778. /// <summary>
  779. /// 输入验证时,设定默认值。
  780. /// </summary>
  781. protected virtual void SetDefaultOnCheck()
  782. {
  783. if (this._mustInput && this.SelectedIndex < 0 && this.Items.Count > 0)
  784. {
  785. this.SelectedIndex = 0;
  786. }
  787. }
  788. /// <summary>
  789. /// 设置文本框只读
  790. /// </summary>
  791. protected virtual void SetEditReadOnly()
  792. {
  793. this.SetEditReadOnly(this._readOnly || this._editReadOnly);
  794. }
  795. #endregion
  796. #region 私有方法
  797. /// <summary>
  798. /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。
  799. /// </summary>
  800. private void InvalidateBorder()
  801. {
  802. Color? borderColor = BorderColorPaint.GetBorderColor(this as IDataVerifiable, this._entered, this._mouseOver);
  803. if (borderColor != this._borderColor)
  804. {
  805. this._borderColor = borderColor;
  806. if (this.Parent == null)
  807. {
  808. this.Invalidate();
  809. }
  810. else
  811. {
  812. this.Parent.Invalidate(this.Bounds, true);
  813. }
  814. }
  815. }
  816. /// <summary>
  817. /// 释放与此窗口组合Edit关联的句柄
  818. /// </summary>
  819. private void ReleaseChildWindow()
  820. {
  821. if (this._childEdit != null)
  822. {
  823. this._childEdit.ReleaseHandle();
  824. this._childEdit = null;
  825. }
  826. //if (this.childListBox != null)
  827. //{
  828. // this.childListBox.ReleaseHandle();
  829. // this.childListBox = null;
  830. //}
  831. }
  832. private void SetNullData(object dataSource, bool canRemove)
  833. {
  834. System.Data.DataTable dt = dataSource as System.Data.DataTable;
  835. if (dt == null)
  836. {
  837. return;
  838. }
  839. if (this._hasNullData)
  840. {
  841. dt.Rows.InsertAt(dt.NewRow(), 0);
  842. }
  843. else if (canRemove)
  844. {
  845. dt.Rows.RemoveAt(0);
  846. }
  847. }
  848. /// <summary>
  849. /// 设置文本框只读
  850. /// </summary>
  851. /// <param name="readOnly"></param>
  852. protected void SetEditReadOnly(bool readOnly)
  853. {
  854. if (this._childEdit != null)
  855. {
  856. WindowsAPI.SendMessage(this._childEdit.Handle, (int)WindowsMessage.EM_SETREADONLY,
  857. readOnly ? -1 : 0, 0);
  858. }
  859. }
  860. #endregion
  861. #region IDataVerifiable 成员
  862. #region 成员变量
  863. /// <summary>
  864. /// 显示边框颜色
  865. /// </summary>
  866. private bool _showBorderColor = true;
  867. /// <summary>
  868. /// 控件的项目名
  869. /// </summary>
  870. private string _itemName = null;
  871. /// <summary>
  872. /// 控件是否是必须输入项目
  873. /// </summary>
  874. private bool _mustInput = false;
  875. /// <summary>
  876. /// 控件在验证输入错误时,如何提示
  877. /// </summary>
  878. private InputErrorAlert _errorAlert = InputErrorAlert.Validated;
  879. /// <summary>
  880. /// 是否显示必须输入项目的提示
  881. /// </summary>
  882. private bool _showMustInputAlert = true;
  883. /// <summary>
  884. /// 验证不通过时,焦点能否离开
  885. /// </summary>
  886. private bool _canLostFocusOnError = true;
  887. /// <summary>
  888. /// 验证是否有错误
  889. /// </summary>
  890. private bool _hasError = false;
  891. /// <summary>
  892. /// 是否自定义错误
  893. /// </summary>
  894. private bool _hasCustomerError = false;
  895. /// <summary>
  896. /// 错误编码
  897. /// </summary>
  898. private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000;
  899. /// <summary>
  900. /// 错误消息
  901. /// </summary>
  902. private string _errorMessage = null;
  903. /// <summary>
  904. /// 自定义错误消息
  905. /// </summary>
  906. private string _customerErrorMessage = null;
  907. #endregion
  908. #region 属性
  909. /// <summary>
  910. /// 获取或设置控件是否显示边框颜色。
  911. /// </summary>
  912. [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")]
  913. [DefaultValue(true)]
  914. public bool ShowBorderColor
  915. {
  916. get
  917. {
  918. return this._showBorderColor;
  919. }
  920. set
  921. {
  922. if (this._showBorderColor != value)
  923. {
  924. this._showBorderColor = value;
  925. this.InvalidateBorder();
  926. }
  927. }
  928. }
  929. /// <summary>
  930. /// 获取或设置控件的项目名
  931. /// </summary>
  932. [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")]
  933. [DefaultValue(null)]
  934. public string CDItemName
  935. {
  936. get
  937. {
  938. return this._itemName;
  939. }
  940. set
  941. {
  942. this._itemName = value;
  943. }
  944. }
  945. /// <summary>
  946. /// 获取或设置控件是否必须选中项目。
  947. /// </summary>
  948. [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")]
  949. [DefaultValue(false)]
  950. public virtual bool MustInput
  951. {
  952. get
  953. {
  954. return this._mustInput;
  955. }
  956. set
  957. {
  958. if (this._mustInput != value)
  959. {
  960. this._mustInput = value;
  961. this.ValidateData();
  962. if (value && this._showMustInputAlert)
  963. {
  964. this.InvalidateBorder();
  965. }
  966. }
  967. }
  968. }
  969. /// <summary>
  970. /// 获取或设置控件是否必须选中项目。
  971. /// </summary>
  972. [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")]
  973. [DefaultValue(typeof(InputErrorAlert), "Validated")]
  974. public InputErrorAlert InputErrorAlert
  975. {
  976. get
  977. {
  978. return this._errorAlert;
  979. }
  980. set
  981. {
  982. if (this._errorAlert != value)
  983. {
  984. this._errorAlert = value;
  985. this.InvalidateBorder();
  986. }
  987. }
  988. }
  989. /// <summary>
  990. /// 获取或设置控件是否显示必须输入项目提示
  991. /// </summary>
  992. [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")]
  993. [DefaultValue(true)]
  994. public bool ShowMustInputAlert
  995. {
  996. get
  997. {
  998. return this._showMustInputAlert;
  999. }
  1000. set
  1001. {
  1002. if (this._showMustInputAlert != value)
  1003. {
  1004. this._showMustInputAlert = value;
  1005. this.InvalidateBorder();
  1006. }
  1007. }
  1008. }
  1009. /// <summary>
  1010. /// 获取或设置当验证不通过时,控件是否可以失去焦点
  1011. /// </summary>
  1012. [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")]
  1013. [DefaultValue(true)]
  1014. public bool CanLostFocusOnError
  1015. {
  1016. get
  1017. {
  1018. return this._canLostFocusOnError;
  1019. }
  1020. set
  1021. {
  1022. this._canLostFocusOnError = value;
  1023. }
  1024. }
  1025. /// <summary>
  1026. /// 获取控件校验时是否有错误
  1027. /// </summary>
  1028. [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")]
  1029. [DefaultValue(false)]
  1030. public bool HasError
  1031. {
  1032. get
  1033. {
  1034. return this._hasCustomerError || this._hasError;
  1035. }
  1036. }
  1037. /// <summary>
  1038. /// 获取控件校验时的错误编码
  1039. /// </summary>
  1040. [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
  1041. [DefaultValue(typeof(ControlErrorCode), "DKC_0000")]
  1042. public ControlErrorCode ErrorCode
  1043. {
  1044. get
  1045. {
  1046. return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode;
  1047. }
  1048. }
  1049. /// <summary>
  1050. /// 获取控件校验时的错误消息
  1051. /// </summary>
  1052. [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
  1053. [DefaultValue(null)]
  1054. public string ErrorMessage
  1055. {
  1056. get
  1057. {
  1058. return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage;
  1059. }
  1060. }
  1061. #endregion
  1062. #region 公有方法
  1063. /// <summary>
  1064. /// 设置自定义错误
  1065. /// </summary>
  1066. /// <param name="hasError">输入是否有错误</param>
  1067. /// <param name="errorMessage">错误消息</param>
  1068. public virtual void SetCustomerError(bool hasError, string errorMessage)
  1069. {
  1070. if (this._hasCustomerError != hasError ||
  1071. this._customerErrorMessage != errorMessage)
  1072. {
  1073. this._hasCustomerError = hasError;
  1074. this._customerErrorMessage = errorMessage;
  1075. this.OnHasErrorChanged(EventArgs.Empty);
  1076. this.InvalidateBorder();
  1077. }
  1078. }
  1079. /// <summary>
  1080. /// 清除自定义错误
  1081. /// </summary>
  1082. public virtual void ClearCustomerError()
  1083. {
  1084. this.SetCustomerError(false, null);
  1085. }
  1086. /// <summary>
  1087. /// 验证输入内容
  1088. /// </summary>
  1089. /// <returns>验证结果</returns>
  1090. public virtual bool ValidateData()
  1091. {
  1092. if (this._mustInput && this.SelectedIndex < 0)
  1093. {
  1094. this.SetError(true, ControlErrorCode.DKC_0001, this._itemName);
  1095. return false;
  1096. }
  1097. this.ClearError();
  1098. return true;
  1099. }
  1100. /// <summary>
  1101. /// 清除输入项
  1102. /// </summary>
  1103. public virtual void ClearValue()
  1104. {
  1105. this.Text = null;
  1106. this.SelectedIndex = -1;
  1107. }
  1108. #endregion
  1109. #region 保护方法
  1110. /// <summary>
  1111. /// 设置验证不通过错误
  1112. /// </summary>
  1113. /// <param name="hasError">是否有错误</param>
  1114. /// <param name="code">错误编码</param>
  1115. /// <param name="args">设置格式的对象</param>
  1116. protected void SetError(bool hasError, ControlErrorCode code, params object[] args)
  1117. {
  1118. if (this._hasError != hasError ||
  1119. this._errorCode != code)
  1120. {
  1121. this._hasError = hasError;
  1122. this._errorCode = code;
  1123. if (args != null && args.Length > 0)
  1124. {
  1125. this._errorMessage = string.Format(code.GetDescription(), args);
  1126. }
  1127. else
  1128. {
  1129. this._errorMessage = code.GetDescription();
  1130. }
  1131. this.OnHasErrorChanged(EventArgs.Empty);
  1132. this.InvalidateBorder();
  1133. }
  1134. }
  1135. /// <summary>
  1136. /// 清除验证不通过错误
  1137. /// </summary>
  1138. protected void ClearError()
  1139. {
  1140. this.SetError(false, ControlErrorCode.DKC_0000);
  1141. }
  1142. #endregion
  1143. #endregion
  1144. #region IAsyncControl 成员
  1145. #region 成员变量
  1146. /// <summary>
  1147. /// 异步处理开始时,控件状态
  1148. /// </summary>
  1149. private bool _asyncBeginStatus = false;
  1150. private bool _asyncBeginFocused = false;
  1151. #endregion
  1152. #region 公有方法
  1153. /// <summary>
  1154. /// 开始异步处理
  1155. /// </summary>
  1156. /// <param name="doFocus">是否处理焦点</param>
  1157. public virtual void BeginAsync(ref bool doFocus)
  1158. {
  1159. this._asyncBeginFocused = false;
  1160. if (doFocus && this.Focused)
  1161. {
  1162. this._asyncBeginFocused = true;
  1163. doFocus = false;
  1164. }
  1165. this._asyncBeginStatus = this.ReadOnly;
  1166. this.ReadOnly = true;
  1167. }
  1168. /// <summary>
  1169. /// 结束异步处理
  1170. /// </summary>
  1171. public virtual void EndAsync()
  1172. {
  1173. this.ReadOnly = this._asyncBeginStatus;
  1174. if (this._asyncBeginFocused)
  1175. {
  1176. this.Focus();
  1177. }
  1178. }
  1179. #endregion
  1180. #endregion
  1181. }
  1182. }