TextBoxNumerical.cs 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Globalization;
  6. using System.Text.RegularExpressions;
  7. using System.Windows.Forms;
  8. using Dongke.WinForm.Utilities;
  9. namespace Dongke.WinForm.Controls
  10. {
  11. /// <summary>
  12. /// 数值输入文本框
  13. /// </summary>
  14. public abstract class TextBoxNumerical : TextBox, IDKControl,
  15. IDataVerifiable, IAsyncControl
  16. {
  17. #region 常量
  18. /// <summary>
  19. /// [^0-9]
  20. /// </summary>
  21. private const string REJECT_CHARS = "[^0-9]";
  22. /// <summary>
  23. /// [^-.0-9]
  24. /// </summary>
  25. private const string REJECT_CHARS_ALL = "[^-.0-9]";
  26. /// <summary>
  27. /// [-.]
  28. /// </summary>
  29. private const string REJECT_CHARS_NP = "-.";
  30. /// <summary>
  31. /// [#,##]
  32. /// </summary>
  33. private const string COMMA_HAS = "#,##";
  34. /// <summary>
  35. /// [####]
  36. /// </summary>
  37. private const string COMMA_NOT = "####";
  38. #endregion
  39. #region 事件声明
  40. /*
  41. #region TextPaste
  42. /// <summary>
  43. /// 当输入文本粘贴后发生
  44. /// </summary>
  45. private static readonly object EventTextPaste = new object();
  46. /// <summary>
  47. /// TextPaste 事件
  48. /// </summary>
  49. public event TextPasteEventHandler TextPaste
  50. {
  51. add
  52. {
  53. base.Events.AddHandler(EventTextPaste, value);
  54. }
  55. remove
  56. {
  57. base.Events.RemoveHandler(EventTextPaste, value);
  58. }
  59. }
  60. #endregion
  61. */
  62. #region HasErrorChanged
  63. /// <summary>
  64. /// 当 HasError 属性的值更改时发生。
  65. /// </summary>
  66. private static readonly object EventHasErrorChanged = new object();
  67. /// <summary>
  68. /// 当 HasError 属性的值更改时发生。
  69. /// </summary>
  70. [Description("当 HasError 属性的值更改时发生。"), Category("CustomerEx")]
  71. public event EventHandler HasErrorChanged
  72. {
  73. add
  74. {
  75. base.Events.AddHandler(EventHasErrorChanged, value);
  76. }
  77. remove
  78. {
  79. base.Events.RemoveHandler(EventHasErrorChanged, value);
  80. }
  81. }
  82. #endregion
  83. #endregion
  84. #region 成员变量
  85. /// <summary>
  86. /// 验证不通过,焦点不离开
  87. /// </summary>
  88. private bool _validatingCancel = false;
  89. /// <summary>
  90. /// 焦点是否进入控件
  91. /// </summary>
  92. private bool _entered = false;
  93. /// <summary>
  94. /// 鼠标是否进入控件
  95. /// </summary>
  96. private bool _mouseOver = false;
  97. /// <summary>
  98. /// 边框颜色
  99. /// </summary>
  100. private Color? _borderColor = null;
  101. /// <summary>
  102. /// 控件在获得焦点时,文本内容的选择方式。
  103. /// </summary>
  104. private EnteredSelectTextStyle _enteredSelectTextStyle = EnteredSelectTextStyle.None;
  105. /// <summary>
  106. /// 数值精度
  107. /// </summary>
  108. //private DNumber _number = DNumber.Empty;
  109. private Point _number = Point.Empty;
  110. /// <summary>
  111. /// 输入最大值
  112. /// </summary>
  113. private decimal _maxValue = 70000000000000000000000000000m;
  114. /// <summary>
  115. /// 输入最小值
  116. /// </summary>
  117. private decimal _minValue = -70000000000000000000000000000m;
  118. /// <summary>
  119. /// 数值精度最大值
  120. /// </summary>
  121. private decimal _maxDNumberValue = decimal.MaxValue;
  122. /// <summary>
  123. /// 数值精度最小值
  124. /// </summary>
  125. private decimal _minDNumberValue = decimal.MinValue;
  126. /// <summary>
  127. /// 最大值
  128. /// </summary>
  129. private decimal _maxNumberValue = decimal.MaxValue;
  130. /// <summary>
  131. /// 最小值
  132. /// </summary>
  133. private decimal _minNumberValue = decimal.MinValue;
  134. /// <summary>
  135. /// 数值
  136. /// </summary>
  137. protected decimal? _dataValue = null;
  138. /// <summary>
  139. /// 允许负数
  140. /// </summary>
  141. private bool _allowNegative = true;
  142. /// <summary>
  143. /// 是否显示千分隔符(逗号)
  144. /// </summary>
  145. private ShowDigitalKind _thousandsSeparator = ShowDigitalKind.Always;
  146. /// <summary>
  147. /// 补齐小数位数
  148. /// </summary>
  149. private ShowDigitalKind _showDecimalPlaces = ShowDigitalKind.Always;
  150. /// <summary>
  151. /// 限制数值输入方式
  152. /// </summary>
  153. private LimitInputKind _limitInputKind = LimitInputKind.None;
  154. /// <summary>
  155. /// 正在设置文本
  156. /// </summary>
  157. private bool _isSettingText = false;
  158. #endregion
  159. #region 构造函数
  160. /// <summary>
  161. /// 数值输入文本框
  162. /// </summary>
  163. public TextBoxNumerical()
  164. {
  165. base.TextAlign = HorizontalAlignment.Right;
  166. base.ImeMode = ImeMode.Off;
  167. CommonSetting.InitControls(this);
  168. }
  169. #endregion
  170. #region 属性
  171. /// <summary>
  172. /// 获取一个值,该值指示控件是否有输入焦点。
  173. /// </summary>
  174. [DefaultValue(false)]
  175. [Browsable(false)]
  176. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  177. [EditorBrowsable(EditorBrowsableState.Advanced)]
  178. public virtual bool Entered
  179. {
  180. get
  181. {
  182. return this._entered;
  183. }
  184. protected set
  185. {
  186. if (this._entered != value)
  187. {
  188. this._entered = value;
  189. this.InvalidateBorder();
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// 获取一个值,该值指示鼠标是否在控件上方。
  195. /// </summary>
  196. [DefaultValue(false)]
  197. [Browsable(false)]
  198. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  199. [EditorBrowsable(EditorBrowsableState.Advanced)]
  200. public virtual bool MouseOver
  201. {
  202. get
  203. {
  204. return this._mouseOver;
  205. }
  206. protected set
  207. {
  208. if (this._mouseOver != value)
  209. {
  210. this._mouseOver = value;
  211. this.InvalidateBorder();
  212. }
  213. }
  214. }
  215. /// <summary>
  216. /// 获取或设置数字的精度,DNumber.X为整数位的有效位数,DNumber.Y为小数位的有效位数
  217. /// </summary>
  218. [Description("获取或设置数字的精度,DNumber.X为整数位的有效位数,DNumber.Y为小数位的有效位数"), Category("CustomerEx")]
  219. [DefaultValue(typeof(Point), "0, 0")]
  220. public virtual Point Number
  221. {
  222. get
  223. {
  224. return this._number;
  225. }
  226. set
  227. {
  228. if (this._number != value)
  229. {
  230. this._number = value;
  231. this.SetDNumberValue();
  232. this.SetTextValue();
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 获取或设置允许输入的数字最大值
  238. /// </summary>
  239. [Description("获取或设置允许输入的数字最大值"), Category("CustomerEx")]
  240. //[DefaultValue(typeof(decimal), "79228162514264337593543950335")]
  241. [DefaultValue(typeof(decimal), "70000000000000000000000000000")]
  242. [Localizable(true)]
  243. public virtual decimal MaxValue
  244. {
  245. get
  246. {
  247. return this._maxValue;
  248. }
  249. set
  250. {
  251. if (value < this._minValue)
  252. {
  253. return;
  254. }
  255. if (this._maxValue != value)
  256. {
  257. this._maxValue = value;
  258. if (this.SetMaxOrMinValue())
  259. {
  260. this.SetTextValue();
  261. }
  262. }
  263. }
  264. }
  265. /// <summary>
  266. /// 获取或设置允许输入的数字最小值
  267. /// </summary>
  268. [Description("获取或设置允许输入的数字最小值"), Category("CustomerEx")]
  269. //[DefaultValue(typeof(decimal), "-79228162514264337593543950335")]
  270. [DefaultValue(typeof(decimal), "-70000000000000000000000000000")]
  271. [Localizable(true)]
  272. public virtual decimal MinValue
  273. {
  274. get
  275. {
  276. return this._minValue;
  277. }
  278. set
  279. {
  280. if (value > this._maxValue)
  281. {
  282. return;
  283. }
  284. if (this._minValue != value)
  285. {
  286. this._minValue = value;
  287. if (this.SetMaxOrMinValue())
  288. {
  289. this.SetTextValue();
  290. }
  291. }
  292. }
  293. }
  294. /// <summary>
  295. /// 获取或设置文本框的数值
  296. /// </summary>
  297. [Description("获取或设置文本框的数值"), Category("CustomerEx")]
  298. [DefaultValue(null)]
  299. public decimal? DataValue
  300. {
  301. get
  302. {
  303. return this._dataValue;
  304. }
  305. set
  306. {
  307. if (this._dataValue != value)
  308. {
  309. //this._dataValue = value;
  310. this.SetTextValue(value.HasValue ? value.ToString() : string.Empty);
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// 获取或设置是否允许输入负数
  316. /// </summary>
  317. [Description("获取或设置是否允许输入负数"), Category("CustomerEx")]
  318. [DefaultValue(true)]
  319. public virtual bool AllowNegative
  320. {
  321. get
  322. {
  323. return this._allowNegative;
  324. }
  325. set
  326. {
  327. if (this._allowNegative != value)
  328. {
  329. this._allowNegative = value;
  330. bool needReset = this.SetDNumberValue();
  331. //needReset = this.SetRejectedChars() || needReset;
  332. if (needReset)
  333. {
  334. this.SetTextValue();
  335. }
  336. }
  337. }
  338. }
  339. /// <summary>
  340. /// 获取或设置一个值,该值指示如何显示千位分隔符(逗号)。
  341. /// </summary>
  342. [Description("获取或设置一个值,该值指示如何显示千位分隔符(逗号)。"), Category("CustomerEx")]
  343. [DefaultValue(typeof(ShowDigitalKind), "Always")]
  344. public virtual ShowDigitalKind ThousandsSeparator
  345. {
  346. get
  347. {
  348. return this._thousandsSeparator;
  349. }
  350. set
  351. {
  352. if (!Enum.IsDefined(typeof(ShowDigitalKind), value))
  353. {
  354. return;
  355. }
  356. if (this._thousandsSeparator != value)
  357. {
  358. this._thousandsSeparator = value;
  359. this.SetTextValue();
  360. }
  361. }
  362. }
  363. /// <summary>
  364. /// 获取或设置一个值,该值指示如何显示小数位数。
  365. /// </summary>
  366. [Description("获取或设置一个值,该值指示如何显示小数位数。"), Category("CustomerEx")]
  367. [DefaultValue(typeof(ShowDigitalKind), "Always")]
  368. public virtual ShowDigitalKind ShowDecimalPlaces
  369. {
  370. get
  371. {
  372. return this._showDecimalPlaces;
  373. }
  374. set
  375. {
  376. if (!Enum.IsDefined(typeof(ShowDigitalKind), value))
  377. {
  378. return;
  379. }
  380. if (this._showDecimalPlaces != value)
  381. {
  382. this._showDecimalPlaces = value;
  383. this.SetTextValue();
  384. }
  385. }
  386. }
  387. /// <summary>
  388. /// 获取或设置输入限制种类
  389. /// </summary>
  390. [Description("获取或设置输入限制种类"), Category("CustomerEx")]
  391. [DefaultValue(typeof(LimitInputKind), "None")]
  392. public virtual LimitInputKind LimitInputKind
  393. {
  394. get
  395. {
  396. return this._limitInputKind;
  397. }
  398. set
  399. {
  400. if (!Enum.IsDefined(typeof(LimitInputKind), value))
  401. {
  402. return;
  403. }
  404. if (this._limitInputKind != value)
  405. {
  406. this._limitInputKind = value;
  407. this.SetTextValue();
  408. }
  409. }
  410. }
  411. #endregion
  412. #region 重写属性
  413. /// <summary>
  414. /// 获取或设置在 System.Windows.Forms.TextBox.AutoCompleteSource 属性设置为 CustomSource
  415. /// 时使用的自定义 System.Collections.Specialized.StringCollection。
  416. /// </summary>
  417. [Browsable(false)]
  418. [EditorBrowsable(EditorBrowsableState.Never)]
  419. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  420. public new AutoCompleteStringCollection AutoCompleteCustomSource
  421. {
  422. get
  423. {
  424. return base.AutoCompleteCustomSource;
  425. }
  426. set
  427. {
  428. base.AutoCompleteCustomSource = value;
  429. }
  430. }
  431. /// <summary>
  432. /// 获取或设置控制自动完成如何作用于 System.Windows.Forms.TextBox 的选项。
  433. /// </summary>
  434. [Browsable(false)]
  435. [EditorBrowsable(EditorBrowsableState.Never)]
  436. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  437. public new AutoCompleteMode AutoCompleteMode
  438. {
  439. get
  440. {
  441. return base.AutoCompleteMode;
  442. }
  443. set
  444. {
  445. base.AutoCompleteMode = value;
  446. }
  447. }
  448. /// <summary>
  449. /// 获取或设置一个值,该值指定用于自动完成的完整字符串源。
  450. /// </summary>
  451. [Browsable(false)]
  452. [EditorBrowsable(EditorBrowsableState.Never)]
  453. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  454. public new AutoCompleteSource AutoCompleteSource
  455. {
  456. get
  457. {
  458. return base.AutoCompleteSource;
  459. }
  460. set
  461. {
  462. base.AutoCompleteSource = value;
  463. }
  464. }
  465. /// <summary>
  466. /// 获取或设置 System.Windows.Forms.TextBox 控件是否在字符键入时修改其大小写格式。
  467. /// </summary>
  468. [Browsable(false)]
  469. [EditorBrowsable(EditorBrowsableState.Never)]
  470. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  471. public new CharacterCasing CharacterCasing
  472. {
  473. get
  474. {
  475. return base.CharacterCasing;
  476. }
  477. set
  478. {
  479. base.CharacterCasing = value;
  480. }
  481. }
  482. /// <summary>
  483. /// 获取或设置一个值,该值指示此控件是否为多行控件。
  484. /// </summary>
  485. [Browsable(false)]
  486. [EditorBrowsable(EditorBrowsableState.Never)]
  487. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  488. public override bool Multiline
  489. {
  490. get
  491. {
  492. return base.Multiline;
  493. }
  494. set
  495. {
  496. base.Multiline = value;
  497. }
  498. }
  499. /// <summary>
  500. /// 获取或设置控件中文本的对齐方式。
  501. /// </summary>
  502. [Browsable(false)]
  503. [EditorBrowsable(EditorBrowsableState.Never)]
  504. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  505. public new HorizontalAlignment TextAlign
  506. {
  507. get
  508. {
  509. return base.TextAlign;
  510. }
  511. set
  512. {
  513. base.TextAlign = value;
  514. }
  515. }
  516. /// <summary>
  517. /// 获取或设置控件的输入法编辑器 (IME) 模式。
  518. /// </summary>
  519. [Browsable(false)]
  520. [EditorBrowsable(EditorBrowsableState.Never)]
  521. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  522. public new ImeMode ImeMode
  523. {
  524. get
  525. {
  526. return base.ImeMode;
  527. }
  528. set
  529. {
  530. base.ImeMode = value;
  531. }
  532. }
  533. /// <summary>
  534. /// 获取一个用以指示是否可以将 System.Windows.Forms.Control.ImeMode 属性设置为活动值的值,以启用 IME 支持。
  535. /// </summary>
  536. protected override bool CanEnableIme
  537. {
  538. get
  539. {
  540. return false;
  541. }
  542. }
  543. /// <summary>
  544. /// 获取或设置用户可在文本框控件中键入或粘贴的最大字符数
  545. /// </summary>
  546. [Browsable(false)]
  547. [EditorBrowsable(EditorBrowsableState.Never)]
  548. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  549. public override int MaxLength
  550. {
  551. get
  552. {
  553. return base.MaxLength;
  554. }
  555. set
  556. {
  557. base.MaxLength = value;
  558. }
  559. }
  560. /// <summary>
  561. /// 获取或设置当前文本。
  562. /// </summary>
  563. [Browsable(false)]
  564. [EditorBrowsable(EditorBrowsableState.Never)]
  565. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  566. public override string Text
  567. {
  568. get
  569. {
  570. return base.Text;
  571. }
  572. set
  573. {
  574. this.SetTextValue(value);
  575. }
  576. }
  577. /// <summary>
  578. /// 获取或设置一个值,该值指示控件中当前选定的文本。
  579. /// </summary>
  580. public override string SelectedText
  581. {
  582. get
  583. {
  584. return base.SelectedText;
  585. }
  586. set
  587. {
  588. this.PasteChars(value);
  589. }
  590. }
  591. /// <summary>
  592. /// 获取或设置字符,该字符用于屏蔽单行控件中的密码字符。
  593. /// </summary>
  594. [Browsable(false)]
  595. [EditorBrowsable(EditorBrowsableState.Never)]
  596. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  597. public new char PasswordChar
  598. {
  599. get
  600. {
  601. return base.PasswordChar;
  602. }
  603. set
  604. {
  605. base.PasswordChar = value;
  606. }
  607. }
  608. /// <summary>
  609. /// 获取或设置哪些滚动条应出现在多行 System.Windows.Forms.TextBox 控件中。
  610. /// </summary>
  611. [Browsable(false)]
  612. [EditorBrowsable(EditorBrowsableState.Never)]
  613. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  614. public new ScrollBars ScrollBars
  615. {
  616. get
  617. {
  618. return base.ScrollBars;
  619. }
  620. set
  621. {
  622. base.ScrollBars = value;
  623. }
  624. }
  625. /// <summary>
  626. /// 获取或设置一个值,该值指示 System.Windows.Forms.TextBox 控件中的文本是否应该以默认的密码字符显示。
  627. /// </summary>
  628. [Browsable(false)]
  629. [EditorBrowsable(EditorBrowsableState.Never)]
  630. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  631. public new bool UseSystemPasswordChar
  632. {
  633. get
  634. {
  635. return base.UseSystemPasswordChar;
  636. }
  637. set
  638. {
  639. base.UseSystemPasswordChar = value;
  640. }
  641. }
  642. #endregion
  643. #region 重写事件
  644. #region 属性改变
  645. /// <summary>
  646. /// 引发 HasErrorChanged 事件
  647. /// </summary>
  648. /// <param name="e">包含事件数据的 EventArgs</param>
  649. protected virtual void OnHasErrorChanged(EventArgs e)
  650. {
  651. EventHandler eventHandler = (EventHandler)base.Events[EventHasErrorChanged];
  652. if (eventHandler != null)
  653. {
  654. eventHandler(this, e);
  655. }
  656. }
  657. /// <summary>
  658. /// 文本改变事件
  659. /// </summary>
  660. /// <param name="e"></param>
  661. protected override void OnTextChanged(EventArgs e)
  662. {
  663. if (!this._isSettingText)
  664. {
  665. this.SetTextValue();
  666. return;
  667. }
  668. this._isSettingText = false;
  669. base.OnTextChanged(EventArgs.Empty);
  670. // TODO 实时验证?
  671. if (true)
  672. {
  673. this.ValidateData();
  674. }
  675. }
  676. #endregion
  677. #region 行为事件
  678. /// <summary>
  679. /// 引发 TextPaste 事件
  680. /// </summary>
  681. /// <param name="e">包含事件数据的 TextPasteEventArgs</param>
  682. protected virtual void OnPaste(TextPasteEventArgs e)
  683. {
  684. //TextPasteEventHandler eventHandler = (TextPasteEventHandler)base.Events[EventTextPaste];
  685. //if (eventHandler != null)
  686. //{
  687. // eventHandler(this, e);
  688. //}
  689. }
  690. #endregion
  691. #region 键盘事件
  692. /// <summary>
  693. /// 键盘输入
  694. /// </summary>
  695. /// <param name="e"></param>
  696. protected override void OnKeyPress(KeyPressEventArgs e)
  697. {
  698. base.OnKeyPress(e);
  699. if (e.Handled)
  700. {
  701. return;
  702. }
  703. if (!this.Enabled || this.ReadOnly || char.IsControl(e.KeyChar))
  704. {
  705. // Back
  706. if (e.KeyChar == (char)8)
  707. {
  708. int ss = base.SelectionStart;
  709. int sl = base.SelectionLength;
  710. int tl = base.TextLength;
  711. if (ss == 0 || (ss == 1 && tl == 1) || sl == tl)
  712. {
  713. return;
  714. }
  715. e.Handled = true;
  716. }
  717. return;
  718. }
  719. if (!this.PressChar(e.KeyChar))
  720. {
  721. WindowsAPI.MessageBeep(BeepType.MB_OK);
  722. }
  723. e.Handled = true;
  724. return;
  725. }
  726. /// <summary>
  727. /// 键盘点击
  728. /// </summary>
  729. /// <param name="e"></param>
  730. protected override void OnKeyDown(KeyEventArgs e)
  731. {
  732. base.OnKeyDown(e);
  733. if (e.Handled)
  734. {
  735. return;
  736. }
  737. if (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete)
  738. {
  739. return;
  740. }
  741. int ss = base.SelectionStart;
  742. int sl = base.SelectionLength;
  743. int tl = base.TextLength;
  744. #region Back
  745. if (e.KeyCode == Keys.Back)
  746. {
  747. // 删除空白
  748. if (ss == 0 || (ss == 1 && tl == 1) || sl == tl)
  749. {
  750. return;
  751. }
  752. this.BackChars();
  753. e.Handled = true;
  754. return;
  755. }
  756. #endregion
  757. #region Delete
  758. if (e.KeyCode == Keys.Delete)
  759. {
  760. // 删除空白
  761. if (ss == tl || (ss == 0 && tl == 1) || sl == tl)
  762. {
  763. return;
  764. }
  765. this.DeleteChars();
  766. e.Handled = true;
  767. return;
  768. }
  769. #endregion
  770. }
  771. #endregion
  772. #region 焦点事件
  773. /// <summary>
  774. /// 输入焦点进入控件
  775. /// </summary>
  776. /// <param name="e"></param>
  777. protected override void OnEnter(EventArgs e)
  778. {
  779. this.Entered = true;
  780. base.OnEnter(e);
  781. }
  782. /// <summary>
  783. /// 获得焦点
  784. /// </summary>
  785. /// <param name="e"></param>
  786. protected override void OnGotFocus(EventArgs e)
  787. {
  788. base.OnGotFocus(e);
  789. if (this._validatingCancel)
  790. {
  791. this._validatingCancel = false;
  792. return;
  793. }
  794. // 格式化显示文本
  795. if (this._showDecimalPlaces != ShowDigitalKind.Always ||
  796. this._thousandsSeparator != ShowDigitalKind.Always)
  797. {
  798. this.SetText(this.FormatText(base.Text));
  799. }
  800. //this.SelectAll();
  801. int textLength = base.Text.Length;
  802. if (textLength > 0)
  803. {
  804. switch (this._enteredSelectTextStyle)
  805. {
  806. case EnteredSelectTextStyle.Left:
  807. this.Select(0, 0);
  808. break;
  809. case EnteredSelectTextStyle.Right:
  810. this.Select(textLength, 0);
  811. break;
  812. case EnteredSelectTextStyle.All:
  813. this.SelectAll();
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. }
  820. /// <summary>
  821. /// 失去焦点
  822. /// </summary>
  823. /// <param name="e"></param>
  824. protected override void OnLostFocus(EventArgs e)
  825. {
  826. base.OnLostFocus(e);
  827. }
  828. /// <summary>
  829. /// 输入焦点离开控件
  830. /// </summary>
  831. /// <param name="e"></param>
  832. protected override void OnLeave(EventArgs e)
  833. {
  834. base.OnLeave(e);
  835. //this.Entered = false;
  836. }
  837. /// <summary>
  838. /// 控件正在验证
  839. /// </summary>
  840. /// <param name="e"></param>
  841. protected override void OnValidating(CancelEventArgs e)
  842. {
  843. base.OnValidating(e);
  844. if (this.HasError && !this._canLostFocusOnError)
  845. {
  846. e.Cancel = true;
  847. }
  848. this._validatingCancel = e.Cancel;
  849. }
  850. /// <summary>
  851. /// 控件完成验证
  852. /// </summary>
  853. /// <param name="e"></param>
  854. protected override void OnValidated(EventArgs e)
  855. {
  856. this.Entered = false;
  857. this.SetText(this.FormatText(base.Text));
  858. this._validatingCancel = false;
  859. base.OnValidated(e);
  860. }
  861. #endregion
  862. #region 鼠标事件
  863. /// <summary>
  864. /// 鼠标进入控件
  865. /// </summary>
  866. /// <param name="e"></param>
  867. protected override void OnMouseEnter(EventArgs e)
  868. {
  869. this.MouseOver = true;
  870. base.OnMouseEnter(e);
  871. }
  872. /// <summary>
  873. /// 鼠标离开控件
  874. /// </summary>
  875. /// <param name="e"></param>
  876. protected override void OnMouseLeave(EventArgs e)
  877. {
  878. base.OnMouseLeave(e);
  879. this.MouseOver = false;
  880. }
  881. /// <summary>
  882. /// 鼠标移动
  883. /// </summary>
  884. /// <param name="e"></param>
  885. protected override void OnMouseMove(MouseEventArgs e)
  886. {
  887. base.OnMouseMove(e);
  888. }
  889. /// <summary>
  890. /// 鼠标按下
  891. /// </summary>
  892. /// <param name="e"></param>
  893. protected override void OnMouseDown(MouseEventArgs e)
  894. {
  895. base.OnMouseDown(e);
  896. }
  897. /// <summary>
  898. /// 鼠标抬起
  899. /// </summary>
  900. /// <param name="e"></param>
  901. protected override void OnMouseUp(MouseEventArgs e)
  902. {
  903. base.OnMouseUp(e);
  904. }
  905. #endregion
  906. #endregion
  907. #region 重写方法
  908. /// <summary>
  909. /// 处理 Windows 消息
  910. /// </summary>
  911. /// <param name="m">要处理的 Windows System.Windows.Forms.Message</param>
  912. protected override void WndProc(ref Message m)
  913. {
  914. switch (m.Msg)
  915. {
  916. case (int)WindowsMessage.WM_PASTE:
  917. // 粘贴
  918. this.PasteChars();
  919. return;
  920. case (int)WindowsMessage.WM_CUT:
  921. // 右键菜单 剪切
  922. this.CutChars();
  923. break;
  924. case (int)WindowsMessage.WM_CHAR:
  925. if (m.WParam.ToInt32() == 18)
  926. {
  927. // Ctrl + X 18
  928. this.CutChars();
  929. }
  930. break;
  931. default:
  932. break;
  933. }
  934. base.WndProc(ref m);
  935. if ((int)m.Result == 1)
  936. {
  937. // 剪切、back、delete 等操作没有改变Text时,设置 _isSettingText = false。
  938. switch (m.Msg)
  939. {
  940. //case (int)WindowsMessage.WM_CUT:
  941. //// 剪切空白
  942. case (int)WindowsMessage.WM_CHAR:
  943. // 输入达到最大长度,back 8
  944. this._isSettingText = false;
  945. break;
  946. case (int)WindowsMessage.WM_KEYDOWN:
  947. //if (m.WParam.ToInt32() == 46)
  948. //{
  949. // // delete 46
  950. // this._isSettingText = false;
  951. //}
  952. break;
  953. default:
  954. break;
  955. }
  956. }
  957. if (m.Msg == (int)WindowsMessage.WM_PAINT ||
  958. m.Msg == (int)WindowsMessage.WM_NCPAINT ||
  959. m.Msg == (int)WindowsMessage.WM_CTLCOLOREDIT)
  960. {
  961. BorderColorPaint.WmBorderPaint(this._borderColor, this.Width, this.Height, ref m);
  962. }
  963. }
  964. #endregion
  965. #region 公有方法
  966. #endregion
  967. #region 保护方法
  968. #endregion
  969. #region 私有方法
  970. /// <summary>
  971. /// 设置文本(过滤限制输入字符,并验证是否符合正则表达式)
  972. /// </summary>
  973. private void SetTextValue()
  974. {
  975. this.SetTextValue(base.Text);
  976. }
  977. /// <summary>
  978. /// 设置文本(过滤限制输入字符,并验证是否符合正则表达式)
  979. /// </summary>
  980. /// <param name="text">文本</param>
  981. private void SetTextValue(string text)
  982. {
  983. text = this.RejectChars(ref text, string.Empty);
  984. text = this.FormatText(text);
  985. this.SetText(text);
  986. }
  987. /// <summary>
  988. /// 设置文本(修改base.Text属性)
  989. /// </summary>
  990. /// <param name="text">文本</param>
  991. private void SetText(string text)
  992. {
  993. if (base.Text != text)
  994. {
  995. this._isSettingText = true;
  996. base.Text = text;
  997. this._isSettingText = false;
  998. }
  999. }
  1000. /// <summary>
  1001. /// Delete删除文本
  1002. /// </summary>
  1003. private void DeleteChars()
  1004. {
  1005. // 原文本
  1006. string baseText = base.Text;
  1007. // 选定的文本
  1008. string seletedText = base.SelectedText;
  1009. // 选定文本起始位置
  1010. int selectionStart = base.SelectionStart;
  1011. // 选定文本长度
  1012. int selectionLength = base.SelectionLength;
  1013. // 删除的文本
  1014. string removedText = seletedText;
  1015. // 输入后选定文本起始位置
  1016. int start = selectionStart + 1;
  1017. if (selectionLength == 0)
  1018. {
  1019. removedText = baseText.Substring(selectionStart, 1);
  1020. }
  1021. // 删除字符为千分符【,】时,文本不变,输入焦点后移一位。
  1022. if (Constant.S_COMMA == removedText)
  1023. {
  1024. this.Select(start, 0);
  1025. return;
  1026. }
  1027. // 删除字符为小数点【.】时,如果只有【.】或不补齐小数位数时,删除字符;其他情况不删除,输入焦点后移一位。
  1028. if (baseText.Length > 1 &&
  1029. this._showDecimalPlaces == ShowDigitalKind.Always &&
  1030. Constant.S_POINT == removedText)
  1031. {
  1032. this.Select(start, 0);
  1033. return;
  1034. }
  1035. // 新文本
  1036. string text = null;
  1037. // 去掉选定的字符
  1038. if (selectionLength > 0)
  1039. {
  1040. text = baseText.Remove(selectionStart, selectionLength);
  1041. }
  1042. else
  1043. {
  1044. text = baseText.Remove(selectionStart, 1);
  1045. }
  1046. text = text.Replace(Constant.S_COMMA, string.Empty);
  1047. // 验证输入后字符
  1048. if (this.CheckInputedText(text))
  1049. {
  1050. // 格式化输入后字符
  1051. text = this.FormatText(text);
  1052. // 输入后光标位置
  1053. start = this.GetDeleteSelectionStart(baseText, selectionStart, selectionLength, removedText, text);
  1054. // 设置文本
  1055. if (baseText != text)
  1056. {
  1057. this._isSettingText = true;
  1058. base.Text = text;
  1059. this._isSettingText = false;
  1060. }
  1061. }
  1062. // 设置输入光标
  1063. if (base.SelectionStart != start || selectionLength > 0)
  1064. {
  1065. this.Select(start, 0);
  1066. }
  1067. }
  1068. /// <summary>
  1069. /// Back删除文本
  1070. /// </summary>
  1071. private void BackChars()
  1072. {
  1073. // 原文本
  1074. string baseText = base.Text;
  1075. // 选定的文本
  1076. string seletedText = base.SelectedText;
  1077. // 选定文本起始位置
  1078. int selectionStart = base.SelectionStart;
  1079. // 选定文本长度
  1080. int selectionLength = base.SelectionLength;
  1081. // 删除的文本
  1082. string removedText = seletedText;
  1083. // 输入后选定文本起始位置
  1084. int start = selectionStart;
  1085. if (selectionLength == 0)
  1086. {
  1087. start = selectionStart - 1;
  1088. removedText = baseText.Substring(start, 1);
  1089. }
  1090. // 删除字符为千分符【,】时,文本不变,输入焦点前移一位。
  1091. if (Constant.S_COMMA == removedText)
  1092. {
  1093. this.Select(start, 0);
  1094. return;
  1095. }
  1096. // 删除字符为小数点【.】时,如果只有【.】或不补齐小数位数时,删除字符;其他情况不删除,输入焦点前移一位。
  1097. if (baseText.Length > 1 &&
  1098. this._showDecimalPlaces == ShowDigitalKind.Always &&
  1099. Constant.S_POINT == removedText)
  1100. {
  1101. this.Select(start, 0);
  1102. return;
  1103. }
  1104. // 新文本
  1105. string text = null;
  1106. // 去掉选定的字符
  1107. if (selectionLength > 0)
  1108. {
  1109. text = baseText.Remove(selectionStart, selectionLength);
  1110. }
  1111. else
  1112. {
  1113. text = baseText.Remove(start, 1);
  1114. }
  1115. text = text.Replace(Constant.S_COMMA, string.Empty);
  1116. // 验证输入后字符
  1117. if (this.CheckInputedText(text))
  1118. {
  1119. // 格式化输入后字符
  1120. text = this.FormatText(text);
  1121. // 输入后光标位置
  1122. start = this.GetBackSelectionStart(baseText, selectionStart, selectionLength, removedText, text);
  1123. // 设置文本
  1124. if (baseText != text)
  1125. {
  1126. this._isSettingText = true;
  1127. base.Text = text;
  1128. this._isSettingText = false;
  1129. }
  1130. }
  1131. // 设置输入光标
  1132. if (base.SelectionStart != start || selectionLength > 0)
  1133. {
  1134. this.Select(start, 0);
  1135. }
  1136. }
  1137. /// <summary>
  1138. /// 剪切文本
  1139. /// </summary>
  1140. private void CutChars()
  1141. {
  1142. if (!this.Enabled || this.ReadOnly)
  1143. {
  1144. return;
  1145. }
  1146. if (base.SelectionLength == 0)
  1147. {
  1148. return;
  1149. }
  1150. // 复制到剪切板
  1151. Clipboard.SetText(base.SelectedText);
  1152. this.DeleteChars();
  1153. }
  1154. /// <summary>
  1155. /// 输入字符串
  1156. /// </summary>
  1157. /// <param name="key"></param>
  1158. /// <returns></returns>
  1159. private bool PressChar(char key)
  1160. {
  1161. // 原文本
  1162. string baseText = base.Text;
  1163. // 选定的文本
  1164. string seletedText = base.SelectedText;
  1165. // 选定文本起始位置
  1166. int selectionStart = base.SelectionStart;
  1167. // 选定文本长度
  1168. int selectionLength = base.SelectionLength;
  1169. // 输入后选定文本起始位置
  1170. int start = selectionStart + selectionLength;
  1171. if (selectionLength == 1 && seletedText[0] == key)
  1172. {
  1173. this.Select(start, 0);
  1174. return true;
  1175. }
  1176. if (selectionLength == 2 &&
  1177. ((seletedText[0] == Constant.C_COMMA && seletedText[1] == key) ||
  1178. (seletedText[0] == key && seletedText[1] == Constant.C_COMMA)))
  1179. {
  1180. this.Select(start, 0);
  1181. return true;
  1182. }
  1183. if (key == Constant.C_POINT)
  1184. {
  1185. if (this._number.Y > 0)
  1186. {
  1187. int indexPoint = baseText.IndexOf(Constant.C_POINT);
  1188. if (selectionLength == 0 && indexPoint == selectionStart)
  1189. {
  1190. this.Select(start + 1, 0);
  1191. return true;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. return false;
  1197. }
  1198. }
  1199. string chars = key.ToString();
  1200. string text = this.RejectChars(ref chars, baseText, selectionStart, selectionLength);
  1201. if (this.CheckInputedText(text))
  1202. {
  1203. text = this.FormatText(text);
  1204. // 输入后光标位置
  1205. start = this.GetKeySelectionStart(baseText, selectionStart,
  1206. selectionLength, seletedText, text, chars);
  1207. // 设置文本
  1208. if (baseText != text)
  1209. {
  1210. this._isSettingText = true;
  1211. base.Text = text;
  1212. this._isSettingText = false;
  1213. }
  1214. // 设置输入光标
  1215. if (base.SelectionStart != start || selectionLength > 0)
  1216. {
  1217. this.Select(start, 0);
  1218. }
  1219. return true;
  1220. }
  1221. return false;
  1222. }
  1223. /// <summary>
  1224. /// 粘贴文本
  1225. /// </summary>
  1226. private void PasteChars()
  1227. {
  1228. if (!this.Enabled || this.ReadOnly)
  1229. {
  1230. return;
  1231. }
  1232. object clipboardChars = Clipboard.GetDataObject().GetData(DataFormats.UnicodeText);
  1233. if (clipboardChars == null)
  1234. {
  1235. clipboardChars = Clipboard.GetDataObject().GetData(DataFormats.Text);
  1236. if (clipboardChars == null)
  1237. {
  1238. return;
  1239. }
  1240. }
  1241. if (!this.PasteChars(clipboardChars.ToString()))
  1242. {
  1243. WindowsAPI.MessageBeep(BeepType.MB_OK);
  1244. }
  1245. }
  1246. /// <summary>
  1247. /// 粘贴文本
  1248. /// </summary>
  1249. /// <param name="chars"></param>
  1250. private bool PasteChars(string chars)
  1251. {
  1252. if (string.IsNullOrEmpty(chars))
  1253. {
  1254. return false;
  1255. }
  1256. // 原文本
  1257. string baseText = base.Text;
  1258. // 选定的文本
  1259. string seletedText = base.SelectedText;
  1260. // 选定文本起始位置
  1261. int selectionStart = base.SelectionStart;
  1262. // 选定文本长度
  1263. int selectionLength = base.SelectionLength;
  1264. // 输入后选定文本起始位置
  1265. int start = selectionStart + selectionLength;
  1266. if (selectionLength > 0 &&
  1267. (seletedText == chars ||
  1268. seletedText.Replace(Constant.S_COMMA, string.Empty) == chars.Replace(Constant.S_COMMA, string.Empty)))
  1269. {
  1270. this.Select(start, 0);
  1271. return true;
  1272. }
  1273. string text = this.RejectChars(ref chars, baseText, selectionStart, selectionLength);
  1274. if (this.CheckInputedText(text))
  1275. {
  1276. text = this.FormatText(text);
  1277. TextPasteEventArgs pasteEventArgs = new TextPasteEventArgs(chars);
  1278. this.OnPaste(pasteEventArgs);
  1279. if (pasteEventArgs.Cancel)
  1280. {
  1281. return false;
  1282. }
  1283. // 输入后光标位置
  1284. start = this.GetKeySelectionStart(baseText, selectionStart,
  1285. selectionLength, seletedText, text, chars);
  1286. // 设置文本
  1287. if (baseText != text)
  1288. {
  1289. this._isSettingText = true;
  1290. base.Text = text;
  1291. this._isSettingText = false;
  1292. }
  1293. // 设置输入光标
  1294. if (base.SelectionStart != start || selectionLength > 0)
  1295. {
  1296. this.Select(start, 0);
  1297. }
  1298. return true;
  1299. }
  1300. return false;
  1301. }
  1302. /// <summary>
  1303. /// 取得Delete删除文本后光标位置
  1304. /// </summary>
  1305. /// <param name="baseText"></param>
  1306. /// <param name="selectionStart"></param>
  1307. /// <param name="selectionLength"></param>
  1308. /// <param name="removedText"></param>
  1309. /// <param name="text"></param>
  1310. /// <returns></returns>
  1311. private int GetDeleteSelectionStart(string baseText, int selectionStart,
  1312. int selectionLength, string removedText, string text)
  1313. {
  1314. int start = selectionStart;
  1315. if (Constant.S_NEGATIVE == removedText || // 删除负号
  1316. Constant.S_NEGATIVE == text ||
  1317. Constant.S_POINT == text ||
  1318. Constant.S_POINT == baseText)
  1319. {
  1320. return start;
  1321. }
  1322. int baseTextLength = baseText.Length;
  1323. int textLength = text.Length;
  1324. int baseTextPointIndex = baseText.IndexOf(Constant.C_POINT);
  1325. int textPointIndex = text.IndexOf(Constant.C_POINT);
  1326. #region 无选定文本
  1327. if (selectionLength == 0)
  1328. {
  1329. // 无效操作
  1330. if (selectionStart == baseTextLength)
  1331. {
  1332. return selectionStart;
  1333. }
  1334. int selectionEnd = selectionStart + 1;
  1335. #region 删除整数部分(无小数点 或 光标在小数点前)
  1336. if (baseTextPointIndex < 0 || selectionStart < baseTextPointIndex)
  1337. {
  1338. // 输入焦点距离字符右侧间隔 + 1
  1339. start = textLength - (baseTextLength - selectionEnd);
  1340. // 光标后移一位,在【,】后面。
  1341. int startIndex = baseTextPointIndex < 0 ? baseTextLength - selectionEnd : baseTextPointIndex - selectionEnd;
  1342. if (startIndex > 0 && startIndex % 4 == 0)
  1343. {
  1344. start++;
  1345. }
  1346. }
  1347. #endregion
  1348. #region 删除小数点
  1349. else if (selectionStart == baseTextPointIndex)
  1350. {
  1351. // 小数变为整数
  1352. int lastLength = baseTextLength - selectionEnd;
  1353. if (lastLength > 0 &&
  1354. (this._thousandsSeparator == ShowDigitalKind.Always ||
  1355. (this._thousandsSeparator == ShowDigitalKind.NoFocus && !this._entered)))
  1356. {
  1357. lastLength = lastLength + lastLength / 3;
  1358. }
  1359. // 右侧字符长度不变
  1360. start = textLength - lastLength;
  1361. // 光标后移一位,在【,】后面。
  1362. if (lastLength % 4 == 0)
  1363. {
  1364. start++;
  1365. }
  1366. }
  1367. #endregion
  1368. #region 删除小数
  1369. else
  1370. {
  1371. if (baseText != text)
  1372. {
  1373. // 光标位置不变
  1374. start = selectionStart;
  1375. }
  1376. else
  1377. {
  1378. // 小数位全为【0】时,光标后移一位。
  1379. start++;
  1380. }
  1381. }
  1382. #endregion
  1383. }
  1384. #endregion
  1385. #region 选定文本
  1386. else
  1387. {
  1388. int selectionEnd = selectionStart + selectionLength;
  1389. #region 删除整数部分(无小数点 或 选定文本在小数点前)
  1390. if (baseTextPointIndex < 0 || selectionEnd <= baseTextPointIndex)
  1391. {
  1392. // 输入焦点(末尾)距离字符右侧间隔不变。
  1393. start = textLength - (baseTextLength - selectionEnd);
  1394. // 光标后移一位,在【,】后面。
  1395. int startIndex = baseTextPointIndex < 0 ? baseTextLength - selectionEnd : baseTextPointIndex - selectionEnd;
  1396. if (startIndex > 0 && startIndex % 4 == 0)
  1397. {
  1398. start++;
  1399. }
  1400. }
  1401. #endregion
  1402. #region 选定文本包括小数点
  1403. else if (selectionStart <= baseTextPointIndex && baseTextPointIndex < selectionEnd)
  1404. {
  1405. // 小数变为整数
  1406. int lastLength = baseTextLength - selectionEnd;
  1407. if (this._thousandsSeparator == ShowDigitalKind.Always ||
  1408. (this._thousandsSeparator == ShowDigitalKind.NoFocus && !this._entered))
  1409. {
  1410. lastLength = lastLength + lastLength / 3;
  1411. }
  1412. // 补齐小数位
  1413. if (textPointIndex > -1)
  1414. {
  1415. // 右侧字符长度加上补齐的小数部分
  1416. start = textLength - lastLength - (textLength - textPointIndex);
  1417. }
  1418. // 变为整数
  1419. else
  1420. {
  1421. // 右侧字符长度不变
  1422. start = textLength - lastLength;
  1423. }
  1424. // 光标后移一位,在【,】后面。
  1425. if (lastLength % 4 == 0)
  1426. {
  1427. start++;
  1428. }
  1429. }
  1430. #endregion
  1431. #region 删除小数
  1432. else
  1433. {
  1434. if (baseText != text)
  1435. {
  1436. // 光标位置不变
  1437. start = selectionStart;
  1438. }
  1439. else
  1440. {
  1441. // 小数位全为【0】时,光标后移。
  1442. start = selectionStart + selectionLength;
  1443. }
  1444. }
  1445. #endregion
  1446. }
  1447. #endregion
  1448. return start;
  1449. }
  1450. /// <summary>
  1451. /// 取得Back删除文本后光标位置
  1452. /// </summary>
  1453. /// <param name="baseText"></param>
  1454. /// <param name="selectionStart"></param>
  1455. /// <param name="selectionLength"></param>
  1456. /// <param name="removedText"></param>
  1457. /// <param name="text"></param>
  1458. /// <returns></returns>
  1459. private int GetBackSelectionStart(string baseText, int selectionStart,
  1460. int selectionLength, string removedText, string text)
  1461. {
  1462. int start = selectionStart - 1;
  1463. if (Constant.S_NEGATIVE == removedText || // 删除负号
  1464. Constant.S_NEGATIVE == text ||
  1465. Constant.S_POINT == text ||
  1466. Constant.S_POINT == baseText)
  1467. {
  1468. return (selectionLength > 0 ? selectionStart : start);
  1469. }
  1470. int baseTextLength = baseText.Length;
  1471. int textLength = text.Length;
  1472. int baseTextPointIndex = baseText.IndexOf(Constant.C_POINT);
  1473. int textPointIndex = text.IndexOf(Constant.C_POINT);
  1474. #region 无选定文本
  1475. if (selectionLength == 0)
  1476. {
  1477. // 无效操作
  1478. if (selectionStart == 0)
  1479. {
  1480. return 0;
  1481. }
  1482. #region 删除整数部分(无小数点 或 光标在小数点前)
  1483. else if (baseTextPointIndex < 0 || selectionStart <= baseTextPointIndex)
  1484. {
  1485. // 输入焦点距离字符右侧间隔不变。
  1486. start = textLength - (baseTextLength - selectionStart);
  1487. int startIndex = 1;
  1488. if (text[0] == Constant.C_NEGATIVE)
  1489. {
  1490. startIndex++;
  1491. }
  1492. // 光标在除负号外的第二位(1),并且光标后字符是【,】,光标后移一位,在【-】后面或第一位(0)。
  1493. if (selectionStart == startIndex)
  1494. {
  1495. startIndex = baseTextPointIndex < 0 ? baseTextLength - selectionStart : baseTextPointIndex - selectionStart;
  1496. if (startIndex > 0 && startIndex % 4 == 0)
  1497. {
  1498. start++;
  1499. }
  1500. }
  1501. }
  1502. #endregion
  1503. #region 删除小数点
  1504. else if (selectionStart == baseTextPointIndex + 1)
  1505. {
  1506. // 小数变为整数
  1507. int lastLength = baseTextLength - selectionStart;
  1508. if (lastLength > 0 &&
  1509. (this._thousandsSeparator == ShowDigitalKind.Always ||
  1510. (this._thousandsSeparator == ShowDigitalKind.NoFocus && !this._entered)))
  1511. {
  1512. lastLength = lastLength + lastLength / 3;
  1513. }
  1514. // 右侧字符长度不变
  1515. start = textLength - lastLength;
  1516. }
  1517. #endregion
  1518. #region 删除小数
  1519. else
  1520. {
  1521. // 光标前移一位
  1522. start = selectionStart - 1;
  1523. }
  1524. #endregion
  1525. }
  1526. #endregion
  1527. #region 选定文本
  1528. else
  1529. {
  1530. int selectionEnd = selectionStart + selectionLength;
  1531. #region 删除整数部分(无小数点 或 选定文本在小数点前)
  1532. if (baseTextPointIndex < 0 || selectionEnd <= baseTextPointIndex)
  1533. {
  1534. // 输入焦点(末尾)距离字符右侧间隔不变。
  1535. start = textLength - (baseTextLength - selectionEnd);
  1536. int startIndex = 0;
  1537. if (text[0] == Constant.C_NEGATIVE)
  1538. {
  1539. startIndex++;
  1540. }
  1541. // 光标在除负号外的第二位(1),并且光标后字符是【,】,光标后移一位,在【-】后面或第一位(0)。
  1542. if (selectionStart == startIndex)
  1543. {
  1544. startIndex = baseTextPointIndex < 0 ? baseTextLength - selectionEnd : baseTextPointIndex - selectionEnd;
  1545. if (startIndex > 0 && startIndex % 4 == 0)
  1546. {
  1547. start++;
  1548. }
  1549. }
  1550. else
  1551. {
  1552. if (removedText.EndsWith(Constant.S_COMMA))
  1553. {
  1554. start--;
  1555. }
  1556. }
  1557. }
  1558. #endregion
  1559. #region 选定文本包括小数点
  1560. else if (selectionStart <= baseTextPointIndex && baseTextPointIndex < selectionEnd)
  1561. {
  1562. // 小数变为整数
  1563. int lastLength = baseTextLength - selectionEnd;
  1564. if (lastLength > 0 &&
  1565. (this._thousandsSeparator == ShowDigitalKind.Always ||
  1566. (this._thousandsSeparator == ShowDigitalKind.NoFocus && !this._entered)))
  1567. {
  1568. lastLength = lastLength + lastLength / 3;
  1569. }
  1570. // 补齐小数位
  1571. if (textPointIndex > -1)
  1572. {
  1573. // 右侧字符长度加上补齐的小数部分
  1574. start = textLength - lastLength - (textLength - textPointIndex);
  1575. }
  1576. // 变为整数
  1577. else
  1578. {
  1579. // 右侧字符长度不变
  1580. start = textLength - lastLength;
  1581. }
  1582. }
  1583. #endregion
  1584. #region 删除小数
  1585. else
  1586. {
  1587. // 输入焦点不变。
  1588. start = selectionStart;
  1589. }
  1590. #endregion
  1591. }
  1592. #endregion
  1593. return start;
  1594. }
  1595. /// <summary>
  1596. /// 取得输入文本后光标位置
  1597. /// </summary>
  1598. /// <param name="baseText"></param>
  1599. /// <param name="selectionStart"></param>
  1600. /// <param name="selectionLength"></param>
  1601. /// <param name="removedText"></param>
  1602. /// <param name="text"></param>
  1603. /// <param name="chars"></param>
  1604. /// <returns></returns>
  1605. private int GetKeySelectionStart(string baseText, int selectionStart,
  1606. int selectionLength, string removedText, string text, string chars)
  1607. {
  1608. int start = selectionStart + 1;
  1609. if (Constant.S_NEGATIVE == chars ||
  1610. Constant.S_POINT == text)
  1611. {
  1612. return start;
  1613. }
  1614. if (REJECT_CHARS_NP == chars)
  1615. {
  1616. return start + 1;
  1617. }
  1618. int baseTextLength = baseText.Length;
  1619. int textLength = text.Length;
  1620. int charsLength = chars.Length;
  1621. int baseTextPointIndex = baseText.IndexOf(Constant.C_POINT);
  1622. int textPointIndex = text.IndexOf(Constant.C_POINT);
  1623. int charsPointIndex = chars.IndexOf(Constant.C_POINT);
  1624. // 无文本或全选时
  1625. if (baseTextLength == 0 || baseTextLength == selectionLength)
  1626. {
  1627. // 新文本没有小数部分,光标在新文本末尾
  1628. if (textPointIndex < 0)
  1629. {
  1630. return textLength;
  1631. }
  1632. // 输入字符没有小数部分,光标在小数点前
  1633. if (charsPointIndex < 0)
  1634. {
  1635. return textPointIndex;
  1636. }
  1637. // 输入字符有小数部分,光标在补齐位数前
  1638. if (charsPointIndex > -1)
  1639. {
  1640. return textPointIndex + charsLength - charsPointIndex;
  1641. }
  1642. }
  1643. #region 无选定文本(插入)
  1644. if (selectionLength == 0)
  1645. {
  1646. #region 输入字符不带小数点
  1647. if (charsPointIndex < 0)
  1648. {
  1649. #region 整数部分(无小数点 或 光标在小数点前)
  1650. if (baseTextPointIndex < 0 || selectionStart <= baseTextPointIndex)
  1651. {
  1652. // 输入前无小数,输入后有小数
  1653. if (baseTextPointIndex < 0 && textPointIndex >= 0)
  1654. {
  1655. start = textPointIndex;
  1656. }
  1657. else
  1658. {
  1659. // 输入焦点距离字符右侧间隔不变。
  1660. start = textLength - (baseTextLength - selectionStart);
  1661. if (baseTextPointIndex == baseTextLength - 1 &&
  1662. textPointIndex != textLength - 1)
  1663. {
  1664. start = start - textLength + textPointIndex + 1;
  1665. }
  1666. else
  1667. {
  1668. // 光标后移一位,在【,】后面。
  1669. int startIndex = baseTextPointIndex < 0 ? baseTextLength - selectionStart : baseTextPointIndex - selectionStart;
  1670. if (startIndex > 0 && startIndex % 4 == 0)
  1671. {
  1672. start++;
  1673. }
  1674. }
  1675. }
  1676. }
  1677. #endregion
  1678. #region 小数部分
  1679. else
  1680. {
  1681. // 光标后移输入字符串长度
  1682. start = selectionStart + charsLength;
  1683. }
  1684. #endregion
  1685. }
  1686. #endregion
  1687. #region 输入字符带小数点
  1688. else
  1689. {
  1690. // 光标在输入字符串末尾
  1691. start = textPointIndex + charsLength - charsPointIndex;
  1692. }
  1693. #endregion
  1694. }
  1695. #endregion
  1696. #region 选定文本(替换)
  1697. else
  1698. {
  1699. int selectionEnd = selectionStart + selectionLength;
  1700. #region 输入字符不带小数点
  1701. if (charsPointIndex < 0)
  1702. {
  1703. #region 整数部分(无小数点 或 光标在小数点前)
  1704. if (baseTextPointIndex < 0 || selectionEnd <= baseTextPointIndex)
  1705. {
  1706. // 输入焦点距离字符右侧间隔不变。
  1707. start = textLength - (baseTextLength - selectionEnd);
  1708. if (baseTextPointIndex == baseTextLength - 1 &&
  1709. textPointIndex != textLength - 1)
  1710. {
  1711. start = start - textLength + textPointIndex + 1;
  1712. }
  1713. else
  1714. {
  1715. // 光标后移一位,在【,】后面。
  1716. int startIndex = baseTextPointIndex < 0 ? baseTextLength - selectionStart : baseTextPointIndex - selectionStart;
  1717. if (startIndex > 0 && startIndex % 4 == 0)
  1718. {
  1719. start++;
  1720. }
  1721. }
  1722. }
  1723. #endregion
  1724. #region 小数部分
  1725. else if (selectionStart > baseTextPointIndex)
  1726. {
  1727. // 光标后移输入字符串长度。
  1728. start = selectionStart + charsLength;
  1729. }
  1730. #endregion
  1731. #region 选定小数点
  1732. else
  1733. {
  1734. // 小数变为整数
  1735. int lastLength = baseTextLength - selectionEnd;
  1736. if (this._thousandsSeparator == ShowDigitalKind.Always ||
  1737. (this._thousandsSeparator == ShowDigitalKind.NoFocus && !this._entered))
  1738. {
  1739. lastLength = lastLength + lastLength / 3;
  1740. }
  1741. // 补齐小数位
  1742. if (textPointIndex > -1)
  1743. {
  1744. // 右侧字符长度加上补齐的小数部分
  1745. start = textLength - lastLength - (textLength - textPointIndex);
  1746. }
  1747. // 变为整数
  1748. else
  1749. {
  1750. // 右侧字符长度不变
  1751. start = textLength - lastLength;
  1752. }
  1753. // 右侧字符长度不变
  1754. //start = textLength - lastLength;
  1755. // 光标后移一位,在【,】后面。
  1756. if (lastLength % 4 == 0)
  1757. {
  1758. start++;
  1759. }
  1760. }
  1761. #endregion
  1762. }
  1763. #endregion
  1764. #region 输入字符带小数点
  1765. else
  1766. {
  1767. // 光标在输入字符串末尾
  1768. start = textPointIndex + charsLength - charsPointIndex;
  1769. }
  1770. #endregion
  1771. }
  1772. #endregion
  1773. return start;
  1774. }
  1775. /// <summary>
  1776. /// 将数值转换为字符串表示形式
  1777. /// </summary>
  1778. /// <param name="value"></param>
  1779. /// <param name="hasNegatve"></param>
  1780. /// <param name="hasPoint"></param>
  1781. /// <param name="pointLength"></param>
  1782. /// <returns></returns>
  1783. private string ToText(decimal value, bool hasNegatve, int hasPoint, int pointLength)
  1784. {
  1785. string dec = null;
  1786. if (this._number.Y > 0)
  1787. {
  1788. if (this._showDecimalPlaces == ShowDigitalKind.Always ||
  1789. (this._showDecimalPlaces == ShowDigitalKind.NoFocus && !this._entered))
  1790. {
  1791. dec = Constant.S_POINT.PadRight(this._number.Y + 1, Constant.C_0);
  1792. }
  1793. else
  1794. {
  1795. if (this._entered)
  1796. {
  1797. if (this._limitInputKind.HasFlag(LimitInputKind.Decimal))
  1798. {
  1799. pointLength = Math.Min(pointLength, this._number.Y);
  1800. }
  1801. dec = Constant.S_POINT.PadRight(pointLength + 1, Constant.C_0);
  1802. }
  1803. else
  1804. {
  1805. dec = Constant.S_POINT.PadRight(this._number.Y + 1, Constant.C_WELL);
  1806. }
  1807. }
  1808. }
  1809. string comma = null;
  1810. if (this._thousandsSeparator == ShowDigitalKind.Always ||
  1811. (this._thousandsSeparator == ShowDigitalKind.NoFocus && !this._entered))
  1812. {
  1813. comma = COMMA_HAS + ((this._entered && hasPoint == -1) ? Constant.S_WELL : Constant.S_0) + dec;
  1814. }
  1815. else
  1816. {
  1817. comma = COMMA_NOT + ((this._entered && hasPoint == -1) ? Constant.S_WELL : Constant.S_0) + dec;
  1818. }
  1819. string text = value.ToString(comma, CultureInfo.CurrentCulture);
  1820. if (hasPoint == -2 && text.IndexOf(Constant.C_POINT) < 0)
  1821. {
  1822. text += Constant.S_POINT;
  1823. }
  1824. return (value < 0 || !hasNegatve) ? text : Constant.S_NEGATIVE + text;
  1825. }
  1826. /// <summary>
  1827. /// 验证输入后的新文本(OnKeyDown delete or back)
  1828. /// </summary>
  1829. /// <param name="text">输入后的新文本</param>
  1830. /// <returns>是否通过验证,true 通过,false 不通过</returns>
  1831. private bool CheckInputedText(string text)
  1832. {
  1833. if (text.Length == 0)
  1834. {
  1835. return true;
  1836. }
  1837. if (text.Length == 1)
  1838. {
  1839. char t1 = text[0];
  1840. // 是否可以输入小数点
  1841. if (Constant.C_POINT == t1)
  1842. {
  1843. return this._number.Y > 0;
  1844. }
  1845. // 是否可以输入负号
  1846. if (Constant.C_NEGATIVE == t1)
  1847. {
  1848. return this._minNumberValue < 0;
  1849. }
  1850. }
  1851. // 【-.】
  1852. if (text == TextBoxNumerical.REJECT_CHARS_NP)
  1853. {
  1854. return true;
  1855. }
  1856. decimal d = 0;
  1857. if (!decimal.TryParse(text, out d))
  1858. {
  1859. // 不能转为数值
  1860. return false;
  1861. }
  1862. int pointIndex = text.IndexOf(Constant.S_POINT);
  1863. // 有小数点
  1864. if (pointIndex > -1)
  1865. {
  1866. // 不能输入小数
  1867. if (this._number.Y == 0)
  1868. {
  1869. return false;
  1870. }
  1871. // todo ?
  1872. text = d.ToString();
  1873. pointIndex = text.IndexOf(Constant.S_POINT);
  1874. // 去掉小数部分多余的【0】
  1875. if (pointIndex > -1)
  1876. {
  1877. text = text.TrimEnd(Constant.C_0);
  1878. }
  1879. // 验证整数部分长度
  1880. if (this._number.X > 0 && this._limitInputKind.HasFlag(LimitInputKind.Integer))
  1881. {
  1882. int textStart = (text[0] == Constant.C_NEGATIVE ? 1 : 0);
  1883. if (pointIndex - textStart > this._number.X)
  1884. {
  1885. return false;
  1886. }
  1887. }
  1888. // 验证小数部分长度
  1889. if (this._number.Y > 0 && this._limitInputKind.HasFlag(LimitInputKind.Decimal))
  1890. {
  1891. if (text.Length - pointIndex - 1 > this._number.Y)
  1892. {
  1893. return false;
  1894. }
  1895. }
  1896. }
  1897. else
  1898. {
  1899. // 无小数点时,验证整数部分长度
  1900. if (this._number.X > 0 && this._limitInputKind.HasFlag(LimitInputKind.Integer))
  1901. {
  1902. int textStart = (text[0] == Constant.C_NEGATIVE ? 1 : 0);
  1903. if (text.Length - textStart > this._number.X)
  1904. {
  1905. return false;
  1906. }
  1907. }
  1908. }
  1909. // 验证数据范围
  1910. if (this._limitInputKind.HasFlag(LimitInputKind.Range))
  1911. {
  1912. if (d > this._maxNumberValue)
  1913. {
  1914. return false;
  1915. }
  1916. if (d < this._minNumberValue)
  1917. {
  1918. return false;
  1919. }
  1920. //if (!this._allowZero && d == 0)
  1921. //{
  1922. // return false;
  1923. //}
  1924. if (!this._allowNegative && d < 0)
  1925. {
  1926. return false;
  1927. }
  1928. }
  1929. return true;
  1930. }
  1931. /// <summary>
  1932. /// 验证数值
  1933. /// </summary>
  1934. /// <param name="value"></param>
  1935. private decimal CheckDataValue(decimal value)
  1936. {
  1937. if (value > this._maxNumberValue)
  1938. {
  1939. value = this._maxNumberValue;
  1940. }
  1941. if (value < this._minNumberValue)
  1942. {
  1943. value = this._minNumberValue;
  1944. }
  1945. return value;
  1946. }
  1947. /// <summary>
  1948. /// 验证输入的字符(paste or check all text)
  1949. /// </summary>
  1950. /// <param name="chars">输入的字符</param>
  1951. /// <param name="text">现有文本</param>
  1952. /// <param name="selectionStart">选定文本的起始点</param>
  1953. /// <param name="selectionLength">选定的字符数</param>
  1954. /// <returns>是否通过验证的文本</returns>
  1955. private string RejectChars(ref string chars, string text,
  1956. int selectionStart = 0, int selectionLength = 0)
  1957. {
  1958. // 去掉选定的字符
  1959. if (selectionLength > 0)
  1960. {
  1961. text = text.Remove(selectionStart, selectionLength);
  1962. }
  1963. if (string.IsNullOrEmpty(chars))
  1964. {
  1965. chars = string.Empty;
  1966. }
  1967. else
  1968. {
  1969. //chars = chars.Replace(REJECT_CHARS_ALL, string.Empty);
  1970. chars = Regex.Replace(chars, REJECT_CHARS_ALL, string.Empty);
  1971. }
  1972. // 过滤输入字符
  1973. if (chars.Length > 0)
  1974. {
  1975. // 过滤负号
  1976. bool noNegatve = selectionStart > 0 || !this._allowNegative;
  1977. // 过滤小数点
  1978. bool noPoint = this._number.Y == 0;
  1979. if (text.Length > 0)
  1980. {
  1981. // 文本中已经有负号
  1982. noNegatve = noNegatve || text[0] == Constant.C_NEGATIVE;
  1983. // 文本中已经有小数点
  1984. noPoint = noPoint || text.Contains(Constant.S_POINT);
  1985. }
  1986. //string rejected = TextBoxDigital.REJECT_CHARS_ALL;
  1987. // 输入字符中是否有负号
  1988. bool hasNegatve = (chars[0] == Constant.C_NEGATIVE);
  1989. // 过滤掉无用字符
  1990. chars = chars.Replace(Constant.S_NEGATIVE, string.Empty);
  1991. if (noPoint)
  1992. {
  1993. //chars = chars.Replace(Constant.S_POINT, string.Empty);
  1994. int indexPoint = chars.IndexOf(Constant.S_POINT);
  1995. if (indexPoint > -1)
  1996. {
  1997. chars = chars.Remove(indexPoint);
  1998. }
  1999. }
  2000. // 允许为负数并且输入负号
  2001. if (!noNegatve && hasNegatve)
  2002. {
  2003. chars = chars + Constant.C_NEGATIVE;
  2004. }
  2005. // 允许为小数并且输入小数点
  2006. if (!noPoint)
  2007. {
  2008. string[] p = chars.Split(Constant.C_POINT);
  2009. if (p.Length > 2)
  2010. {
  2011. chars = string.Join(Constant.S_POINT, p, 0, 2) + string.Join(string.Empty, p, 2, p.Length);
  2012. }
  2013. }
  2014. }
  2015. if (text.Length > 0 && chars.Length > 0)
  2016. {
  2017. text = text.Insert(selectionStart, chars);
  2018. }
  2019. else if (text.Length == 0)
  2020. {
  2021. text = chars;
  2022. }
  2023. return text.Replace(Constant.S_COMMA, string.Empty);
  2024. }
  2025. /// <summary>
  2026. /// 格式化显示文本
  2027. /// </summary>
  2028. /// <param name="text">文本</param>
  2029. /// <returns>格式化后的文本</returns>
  2030. private string FormatText(string text)
  2031. {
  2032. if (string.IsNullOrEmpty(text))
  2033. {
  2034. this._dataValue = null;
  2035. return string.Empty;
  2036. }
  2037. if (Constant.S_NEGATIVE == text ||
  2038. Constant.S_POINT == text ||
  2039. TextBoxNumerical.REJECT_CHARS_NP == text)
  2040. {
  2041. this._dataValue = 0;
  2042. if (this._entered)
  2043. {
  2044. return text;
  2045. }
  2046. else
  2047. {
  2048. if (this._showDecimalPlaces == ShowDigitalKind.Always ||
  2049. this._showDecimalPlaces == ShowDigitalKind.NoFocus)
  2050. {
  2051. return Constant.S_0 + Constant.S_POINT.PadRight(Number.Y + 1, Constant.C_0);
  2052. }
  2053. else
  2054. {
  2055. return Constant.S_0;
  2056. }
  2057. }
  2058. }
  2059. decimal d = 0;
  2060. if (!decimal.TryParse(text, out d))
  2061. {
  2062. this._dataValue = null;
  2063. return text;
  2064. }
  2065. // 负号
  2066. bool hasNegative = (text[0] == Constant.C_NEGATIVE);
  2067. // 小数点(0:无特殊处理;-1:开始位置;-2:结束位置)
  2068. int pointIndex = text.IndexOf(Constant.C_POINT);
  2069. int hasPoint = 0;
  2070. int pointLength = 0;
  2071. if (pointIndex > -1)
  2072. {
  2073. pointLength = text.Length - pointIndex - 1;
  2074. if (pointIndex == 0 || (hasNegative && pointIndex == 1))
  2075. {
  2076. hasPoint = -1;
  2077. }
  2078. else if (pointLength == 0)
  2079. {
  2080. hasPoint = -2;
  2081. }
  2082. }
  2083. this._dataValue = this.CheckDataValue(d);
  2084. return this.ToText(this._dataValue.Value, hasNegative, hasPoint, pointLength);
  2085. }
  2086. /// <summary>
  2087. /// 确定最大最小值
  2088. /// </summary>
  2089. /// <returns></returns>
  2090. private bool SetDNumberValue()
  2091. {
  2092. if (this._number.X > 0)
  2093. {
  2094. this._maxDNumberValue = DNumber.ToDecimal(this._number);
  2095. this._minDNumberValue = (this._allowNegative ? 0 - this._maxDNumberValue : 0);
  2096. }
  2097. else
  2098. {
  2099. this._maxDNumberValue = decimal.MaxValue;
  2100. this._minDNumberValue = (this._allowNegative ? decimal.MinValue : 0);
  2101. }
  2102. return this.SetMaxOrMinValue();
  2103. }
  2104. /// <summary>
  2105. /// 确定最大最小值
  2106. /// </summary>
  2107. /// <returns></returns>
  2108. private bool SetMaxOrMinValue()
  2109. {
  2110. bool isChanged = false;
  2111. decimal value = Math.Min(this._maxValue, this._maxDNumberValue);
  2112. if (this._maxNumberValue != value)
  2113. {
  2114. isChanged = true;
  2115. this._maxNumberValue = value;
  2116. }
  2117. value = Math.Max(this._minValue, this._minDNumberValue);
  2118. if (this._minNumberValue != value)
  2119. {
  2120. isChanged = true;
  2121. this._minNumberValue = value;
  2122. }
  2123. return isChanged;
  2124. }
  2125. /// <summary>
  2126. /// 使父控件的指定区域无效(将其添加到控件的更新区域,下次绘制操作时将重新绘制更新区域),并向父控件发送绘制消息。
  2127. /// </summary>
  2128. private void InvalidateBorder()
  2129. {
  2130. Color? borderColor = BorderColorPaint.GetBorderColor(this as IDataVerifiable, this._entered, this._mouseOver);
  2131. if (borderColor != this._borderColor)
  2132. {
  2133. this._borderColor = borderColor;
  2134. if (this.Parent == null)
  2135. {
  2136. this.Invalidate();
  2137. }
  2138. else
  2139. {
  2140. this.Parent.Invalidate(this.Bounds, true);
  2141. }
  2142. }
  2143. }
  2144. #endregion
  2145. #region IDataVerifiable 成员
  2146. #region 成员变量
  2147. /// <summary>
  2148. /// 显示边框颜色
  2149. /// </summary>
  2150. private bool _showBorderColor = true;
  2151. /// <summary>
  2152. /// 控件的项目名
  2153. /// </summary>
  2154. private string _itemName = null;
  2155. /// <summary>
  2156. /// 控件是否是必须输入项目
  2157. /// </summary>
  2158. private bool _mustInput = false;
  2159. /// <summary>
  2160. /// 控件在验证输入错误时,如何提示
  2161. /// </summary>
  2162. private InputErrorAlert _errorAlert = InputErrorAlert.Validated;
  2163. /// <summary>
  2164. /// 是否显示必须输入项目的提示
  2165. /// </summary>
  2166. private bool _showMustInputAlert = true;
  2167. /// <summary>
  2168. /// 验证不通过时,焦点能否离开
  2169. /// </summary>
  2170. private bool _canLostFocusOnError = true;
  2171. /// <summary>
  2172. /// 验证是否有错误
  2173. /// </summary>
  2174. private bool _hasError = false;
  2175. /// <summary>
  2176. /// 是否自定义错误
  2177. /// </summary>
  2178. private bool _hasCustomerError = false;
  2179. /// <summary>
  2180. /// 错误编码
  2181. /// </summary>
  2182. private ControlErrorCode _errorCode = ControlErrorCode.DKC_0000;
  2183. /// <summary>
  2184. /// 错误消息
  2185. /// </summary>
  2186. private string _errorMessage = null;
  2187. /// <summary>
  2188. /// 自定义错误消息
  2189. /// </summary>
  2190. private string _customerErrorMessage = null;
  2191. #endregion
  2192. #region 属性
  2193. /// <summary>
  2194. /// 获取或设置控件是否显示边框颜色。
  2195. /// </summary>
  2196. [Description("获取或设置控件是否显示边框颜色。"), Category("IDataVerifiable")]
  2197. [DefaultValue(true)]
  2198. public bool ShowBorderColor
  2199. {
  2200. get
  2201. {
  2202. return this._showBorderColor;
  2203. }
  2204. set
  2205. {
  2206. if (this._showBorderColor != value)
  2207. {
  2208. this._showBorderColor = value;
  2209. this.InvalidateBorder();
  2210. }
  2211. }
  2212. }
  2213. /// <summary>
  2214. /// 获取或设置控件的项目名
  2215. /// </summary>
  2216. [Description("获取或设置控件的项目名。"), Category("IDataVerifiable")]
  2217. [DefaultValue(null)]
  2218. public string CDItemName
  2219. {
  2220. get
  2221. {
  2222. return this._itemName;
  2223. }
  2224. set
  2225. {
  2226. this._itemName = value;
  2227. }
  2228. }
  2229. /// <summary>
  2230. /// 获取或设置控件是否必须选中项目。
  2231. /// </summary>
  2232. [Description("获取或设置控件是否必须选中项目。"), Category("IDataVerifiable")]
  2233. [DefaultValue(false)]
  2234. public bool MustInput
  2235. {
  2236. get
  2237. {
  2238. return this._mustInput;
  2239. }
  2240. set
  2241. {
  2242. if (this._mustInput != value)
  2243. {
  2244. if (!this._mustInput && value)
  2245. {
  2246. this._mustInput = value;
  2247. this.SetTextValue();
  2248. }
  2249. else
  2250. {
  2251. this._mustInput = value;
  2252. }
  2253. this.ValidateData();
  2254. if (value && this._showMustInputAlert)
  2255. {
  2256. this.InvalidateBorder();
  2257. }
  2258. }
  2259. }
  2260. }
  2261. /// <summary>
  2262. /// 获取或设置控件是否必须选中项目。
  2263. /// </summary>
  2264. [Description("获取或设置控件在验证输入错误时,如何提示。"), Category("IDataVerifiable")]
  2265. [DefaultValue(typeof(InputErrorAlert), "Validated")]
  2266. public InputErrorAlert InputErrorAlert
  2267. {
  2268. get
  2269. {
  2270. return this._errorAlert;
  2271. }
  2272. set
  2273. {
  2274. if (this._errorAlert != value)
  2275. {
  2276. this._errorAlert = value;
  2277. this.InvalidateBorder();
  2278. }
  2279. }
  2280. }
  2281. /// <summary>
  2282. /// 获取或设置控件是否显示必须输入项目提示
  2283. /// </summary>
  2284. [Description("获取或设置控件是否显示必须输入项目提示。"), Category("IDataVerifiable")]
  2285. [DefaultValue(true)]
  2286. public bool ShowMustInputAlert
  2287. {
  2288. get
  2289. {
  2290. return this._showMustInputAlert;
  2291. }
  2292. set
  2293. {
  2294. if (this._showMustInputAlert != value)
  2295. {
  2296. this._showMustInputAlert = value;
  2297. this.InvalidateBorder();
  2298. }
  2299. }
  2300. }
  2301. /// <summary>
  2302. /// 获取或设置当验证不通过时,控件是否可以失去焦点
  2303. /// </summary>
  2304. [Description("获取或设置当验证不通过时,控件是否可以失去焦点。"), Category("IDataVerifiable")]
  2305. [DefaultValue(true)]
  2306. public bool CanLostFocusOnError
  2307. {
  2308. get
  2309. {
  2310. return this._canLostFocusOnError;
  2311. }
  2312. set
  2313. {
  2314. this._canLostFocusOnError = value;
  2315. }
  2316. }
  2317. /// <summary>
  2318. /// 获取控件校验时是否有错误
  2319. /// </summary>
  2320. [Description("获取控件校验时是否有错误。"), Category("IDataVerifiable")]
  2321. [DefaultValue(false)]
  2322. public bool HasError
  2323. {
  2324. get
  2325. {
  2326. return this._hasCustomerError || this._hasError;
  2327. }
  2328. }
  2329. /// <summary>
  2330. /// 获取控件校验时的错误编码
  2331. /// </summary>
  2332. [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
  2333. [DefaultValue(typeof(ControlErrorCode), "DKC_0000")]
  2334. public ControlErrorCode ErrorCode
  2335. {
  2336. get
  2337. {
  2338. return this._hasCustomerError ? ControlErrorCode.DKC_C001 : this._errorCode;
  2339. }
  2340. }
  2341. /// <summary>
  2342. /// 获取控件校验时的错误消息
  2343. /// </summary>
  2344. [Description("获取控件校验时的错误编码。"), Category("IDataVerifiable")]
  2345. [DefaultValue(null)]
  2346. public string ErrorMessage
  2347. {
  2348. get
  2349. {
  2350. return this._hasCustomerError ? this._customerErrorMessage : this._errorMessage;
  2351. }
  2352. }
  2353. #endregion
  2354. #region 公有方法
  2355. /// <summary>
  2356. /// 设置自定义错误
  2357. /// </summary>
  2358. /// <param name="hasError">输入是否有错误</param>
  2359. /// <param name="errorMessage">错误消息</param>
  2360. public virtual void SetCustomerError(bool hasError, string errorMessage)
  2361. {
  2362. if (this._hasCustomerError != hasError ||
  2363. this._customerErrorMessage != errorMessage)
  2364. {
  2365. this._hasCustomerError = hasError;
  2366. this._customerErrorMessage = errorMessage;
  2367. this.OnHasErrorChanged(EventArgs.Empty);
  2368. this.InvalidateBorder();
  2369. }
  2370. }
  2371. /// <summary>
  2372. /// 清除自定义错误
  2373. /// </summary>
  2374. public virtual void ClearCustomerError()
  2375. {
  2376. this.SetCustomerError(false, null);
  2377. }
  2378. /// <summary>
  2379. /// 验证输入内容
  2380. /// </summary>
  2381. /// <returns>验证结果</returns>
  2382. public virtual bool ValidateData()
  2383. {
  2384. string text = base.Text;
  2385. if (this._mustInput && text.Length == 0)
  2386. {
  2387. this.SetError(true, ControlErrorCode.DKC_0001, this._itemName);
  2388. return false;
  2389. }
  2390. this.ClearError();
  2391. return true;
  2392. }
  2393. /// <summary>
  2394. /// 清除输入项
  2395. /// </summary>
  2396. public virtual void ClearValue()
  2397. {
  2398. this.Clear();
  2399. }
  2400. #endregion
  2401. #region 保护方法
  2402. /// <summary>
  2403. /// 设置验证不通过错误
  2404. /// </summary>
  2405. /// <param name="hasError">是否有错误</param>
  2406. /// <param name="code">错误编码</param>
  2407. /// <param name="args">设置格式的对象</param>
  2408. protected void SetError(bool hasError, ControlErrorCode code, params object[] args)
  2409. {
  2410. if (this._hasError != hasError ||
  2411. this._errorCode != code)
  2412. {
  2413. this._hasError = hasError;
  2414. this._errorCode = code;
  2415. if (args != null && args.Length > 0)
  2416. {
  2417. this._errorMessage = string.Format(code.GetDescription(), args);
  2418. }
  2419. else
  2420. {
  2421. this._errorMessage = code.GetDescription();
  2422. }
  2423. this.OnHasErrorChanged(EventArgs.Empty);
  2424. this.InvalidateBorder();
  2425. }
  2426. }
  2427. /// <summary>
  2428. /// 清除验证不通过错误
  2429. /// </summary>
  2430. protected void ClearError()
  2431. {
  2432. this.SetError(false, ControlErrorCode.DKC_0000);
  2433. }
  2434. #endregion
  2435. #endregion
  2436. #region IAsyncControl 成员
  2437. #region 成员变量
  2438. /// <summary>
  2439. /// 异步处理开始时,控件状态
  2440. /// </summary>
  2441. private bool _asyncBeginStatus = false;
  2442. private bool _asyncBeginFocused = false;
  2443. #endregion
  2444. #region 公有方法
  2445. /// <summary>
  2446. /// 开始异步处理
  2447. /// </summary>
  2448. /// <param name="doFocus">是否处理焦点</param>
  2449. public virtual void BeginAsync(ref bool doFocus)
  2450. {
  2451. this._asyncBeginFocused = false;
  2452. if (doFocus && this.Focused)
  2453. {
  2454. this._asyncBeginFocused = true;
  2455. doFocus = false;
  2456. }
  2457. this._asyncBeginStatus = this.ReadOnly;
  2458. this.ReadOnly = true;
  2459. }
  2460. /// <summary>
  2461. /// 结束异步处理
  2462. /// </summary>
  2463. public virtual void EndAsync()
  2464. {
  2465. this.ReadOnly = this._asyncBeginStatus;
  2466. if (this._asyncBeginFocused)
  2467. {
  2468. this.Focus();
  2469. }
  2470. }
  2471. #endregion
  2472. #endregion
  2473. }
  2474. }