BaseEditExtension.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraEditors.DXErrorProvider;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ExtensionsDev
  11. {
  12. public static class BaseEditExtension
  13. {
  14. public static void UseDoubleClickToSelectAll(this BaseEdit ctrl)
  15. {
  16. ctrl.DoubleClick += Ctrl_DoubleClick;
  17. }
  18. private static void Ctrl_DoubleClick(object sender, EventArgs e)
  19. {
  20. var ctrl = (BaseEdit)sender;
  21. ctrl.SelectAll();
  22. }
  23. public static bool CheckDouble(this ButtonEdit @this, DXErrorProvider dxErrorProvider, string msg)
  24. {
  25. dxErrorProvider.ClearErrors();
  26. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  27. {
  28. dxErrorProvider.SetError(@this, $"{msg}值不能为空!");
  29. return false;
  30. }
  31. double val;
  32. bool isDoubleLon = Double.TryParse(@this.Text.Trim(), out val);
  33. if (!isDoubleLon)
  34. {
  35. dxErrorProvider.SetError(@this, $"{msg}值非有效数字!");
  36. return false;
  37. }
  38. dxErrorProvider.SetError(@this, string.Empty);
  39. return true;
  40. }
  41. public static bool CheckLonLat(this TextEdit @this, DXErrorProvider dxErrorProvider, string msg)
  42. {
  43. dxErrorProvider.ClearErrors();
  44. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  45. {
  46. dxErrorProvider.SetError(@this, $"{msg}经纬度不能为空!");
  47. return false;
  48. }
  49. var context = @this.Text.Split(',');
  50. if (context.Length != 2)
  51. {
  52. dxErrorProvider.SetError(@this, $"{msg}经度纬度之间用英文逗号隔开!");
  53. return false;
  54. }
  55. double lon;
  56. bool isDoubleLon = Double.TryParse(context[0], out lon);
  57. if (!isDoubleLon || lon > 180 || lon < -180)
  58. {
  59. dxErrorProvider.SetError(@this, $"{msg}经度范围[180,-180]!");
  60. return false;
  61. }
  62. double lat;
  63. bool isDoubleLat = Double.TryParse(context[1], out lat);
  64. if (!isDoubleLat || lat > 90 || lat < -90)
  65. {
  66. dxErrorProvider.SetError(@this, $"{msg}纬度范围[90,-90]!");
  67. return false;
  68. }
  69. dxErrorProvider.SetError(@this, string.Empty);
  70. return true;
  71. }
  72. public static (bool, string) CheckLonLat(this TextEdit @this, string msg)
  73. {
  74. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  75. {
  76. return (false, $"{msg}经纬度不能为空!");
  77. }
  78. var context = @this.Text.Split(',');
  79. if (context.Length != 2)
  80. {
  81. return (false, $"{msg}经度纬度之间用英文逗号隔开!");
  82. }
  83. double lon;
  84. bool isDoubleLon = Double.TryParse(context[0], out lon);
  85. if (!isDoubleLon || lon > 180 || lon < -180)
  86. {
  87. return (false, $"{msg}经度范围[180,-180]!");
  88. }
  89. double lat;
  90. bool isDoubleLat = Double.TryParse(context[1], out lat);
  91. if (!isDoubleLat || lat > 90 || lat < -90)
  92. {
  93. return (false, $"{msg}纬度范围[90,-90]!");
  94. }
  95. return (true, "");
  96. }
  97. public static double[] GetLonLat(this TextEdit @this)
  98. {
  99. var context = @this.Text.Replace(",", ",").Split(',');
  100. double lon = Convert.ToDouble(context[0]);
  101. double lat = Convert.ToDouble(context[1]);
  102. return new double[3] { lon, lat, 0 };
  103. }
  104. }
  105. }