BaseEditExtension.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 CheckSearch(this SearchLookUpEdit @this, DXErrorProvider dxErrorProvider, string msg)
  24. {
  25. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  26. {
  27. dxErrorProvider.SetError(@this, $"{msg}不能为空!");
  28. return false;
  29. }
  30. dxErrorProvider.SetError(@this, string.Empty);
  31. return true;
  32. }
  33. public static bool CheckLon(this TextEdit @this, DXErrorProvider dxErrorProvider, string msg)
  34. {
  35. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  36. {
  37. dxErrorProvider.SetError(@this, $"{msg}经度不能为空!");
  38. return false;
  39. }
  40. double lon;
  41. bool isDoubleLon = Double.TryParse(@this.Text, out lon);
  42. if (!isDoubleLon || lon > 180 || lon < -180)
  43. {
  44. dxErrorProvider.SetError(@this, $"{msg}经度范围[180,-180]!");
  45. return false;
  46. }
  47. dxErrorProvider.SetError(@this, string.Empty);
  48. return true;
  49. }
  50. public static bool CheckLat(this TextEdit @this, DXErrorProvider dxErrorProvider, string msg)
  51. {
  52. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  53. {
  54. dxErrorProvider.SetError(@this, $"{msg}纬度不能为空!");
  55. return false;
  56. }
  57. double lat;
  58. bool isDoubleLat = Double.TryParse(@this.Text, out lat);
  59. if (!isDoubleLat || lat > 90 || lat < -90)
  60. {
  61. dxErrorProvider.SetError(@this, $"{msg}纬度范围[90,-90]!");
  62. return false;
  63. }
  64. dxErrorProvider.SetError(@this, string.Empty);
  65. return true;
  66. }
  67. public static bool CheckLonLat(this TextEdit @this, DXErrorProvider dxErrorProvider, string msg)
  68. {
  69. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  70. {
  71. dxErrorProvider.SetError(@this, $"{msg}经纬度不能为空!");
  72. return false;
  73. }
  74. var context = @this.Text.Split(',');
  75. if (context.Length != 2)
  76. {
  77. dxErrorProvider.SetError(@this, $"{msg}经度纬度之间用英文逗号隔开!");
  78. return false;
  79. }
  80. double lon;
  81. bool isDoubleLon = Double.TryParse(context[0], out lon);
  82. if (!isDoubleLon || lon > 180 || lon < -180)
  83. {
  84. dxErrorProvider.SetError(@this, $"{msg}经度范围[180,-180]!");
  85. return false;
  86. }
  87. double lat;
  88. bool isDoubleLat = Double.TryParse(context[1], out lat);
  89. if (!isDoubleLat || lat > 90 || lat < -90)
  90. {
  91. dxErrorProvider.SetError(@this, $"{msg}纬度范围[90,-90]!");
  92. return false;
  93. }
  94. dxErrorProvider.SetError(@this, string.Empty);
  95. return true;
  96. }
  97. public static (bool, string) CheckLonLat(this TextEdit @this, string msg)
  98. {
  99. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  100. {
  101. return (false, $"{msg}经纬度不能为空!");
  102. }
  103. var context = @this.Text.Split(',');
  104. if (context.Length != 2)
  105. {
  106. return (false, $"{msg}经度纬度之间用英文逗号隔开!");
  107. }
  108. double lon;
  109. bool isDoubleLon = Double.TryParse(context[0], out lon);
  110. if (!isDoubleLon || lon > 180 || lon < -180)
  111. {
  112. return (false, $"{msg}经度范围[180,-180]!");
  113. }
  114. double lat;
  115. bool isDoubleLat = Double.TryParse(context[1], out lat);
  116. if (!isDoubleLat || lat > 90 || lat < -90)
  117. {
  118. return (false, $"{msg}纬度范围[90,-90]!");
  119. }
  120. return (true, "");
  121. }
  122. public static double[] GetLonLat(this TextEdit @this)
  123. {
  124. var context = @this.Text.Replace(",", ",").Split(',');
  125. double lon = Convert.ToDouble(context[0]);
  126. double lat = Convert.ToDouble(context[1]);
  127. return new double[3] { lon, lat, 0 };
  128. }
  129. }
  130. }