BaseEditExtension.cs 3.1 KB

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