BaseEditExtension.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 CheckLonLat(this TextEdit @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. var context = @this.Text.Split(',');
  32. if (context.Length != 2)
  33. {
  34. dxErrorProvider.SetError(@this, $"{msg}经度纬度之间用英文逗号隔开!");
  35. return false;
  36. }
  37. double lon;
  38. bool isDoubleLon = Double.TryParse(context[0], out lon);
  39. if (!isDoubleLon || lon > 180 || lon < -180)
  40. {
  41. dxErrorProvider.SetError(@this, $"{msg}经度范围[180,-180]!");
  42. return false;
  43. }
  44. double lat;
  45. bool isDoubleLat = Double.TryParse(context[1], out lat);
  46. if (!isDoubleLat || lat > 90 || lat < -90)
  47. {
  48. dxErrorProvider.SetError(@this, $"{msg}纬度范围[90,-90]!");
  49. return false;
  50. }
  51. dxErrorProvider.SetError(@this, string.Empty);
  52. return true;
  53. }
  54. public static (bool, string) CheckLonLat(this TextEdit @this, string msg)
  55. {
  56. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  57. {
  58. return (false, $"{msg}经纬度不能为空!");
  59. }
  60. var context = @this.Text.Split(',');
  61. if (context.Length != 2)
  62. {
  63. return (false, $"{msg}经度纬度之间用英文逗号隔开!");
  64. }
  65. double lon;
  66. bool isDoubleLon = Double.TryParse(context[0], out lon);
  67. if (!isDoubleLon || lon > 180 || lon < -180)
  68. {
  69. return (false, $"{msg}经度范围[180,-180]!");
  70. }
  71. double lat;
  72. bool isDoubleLat = Double.TryParse(context[1], out lat);
  73. if (!isDoubleLat || lat > 90 || lat < -90)
  74. {
  75. return (false, $"{msg}纬度范围[90,-90]!");
  76. }
  77. return (true, "");
  78. }
  79. public static double[] GetLonLat(this TextEdit @this)
  80. {
  81. var context = @this.Text.Replace(",", ",").Split(',');
  82. double lon = Convert.ToDouble(context[0]);
  83. double lat = Convert.ToDouble(context[1]);
  84. return new double[3] { lon, lat, 0 };
  85. }
  86. }
  87. }