ParamCheckExtension.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DevExpress.XtraEditors.DXErrorProvider;
  7. using DevExpress.XtraEditors;
  8. using DevExpress.Emf;
  9. using System.Text.RegularExpressions;
  10. using System.IO;
  11. namespace ExtensionsDev
  12. {
  13. public static class ParamCheckExtension
  14. {
  15. public static bool CheckLonLat(this TextEdit @this, DXErrorProvider dxErrorProvider, string msg)
  16. {
  17. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  18. {
  19. dxErrorProvider.SetError(@this, $"{msg}经纬度不能为空!");
  20. return false;
  21. }
  22. var context = @this.Text.Split(',');
  23. if (context.Length != 2)
  24. {
  25. dxErrorProvider.SetError(@this, $"{msg}经度纬度之间用英文逗号隔开!");
  26. return false;
  27. }
  28. double lon;
  29. bool isDoubleLon = Double.TryParse(context[0], out lon);
  30. if (!isDoubleLon || lon > 180 || lon < -180)
  31. {
  32. dxErrorProvider.SetError(@this, $"{msg}经度范围[180,-180]!");
  33. return false;
  34. }
  35. double lat;
  36. bool isDoubleLat = Double.TryParse(context[1], out lat);
  37. if (!isDoubleLat || lat > 90 || lat < -90)
  38. {
  39. dxErrorProvider.SetError(@this, $"{msg}纬度范围[90,-90]!");
  40. return false;
  41. }
  42. dxErrorProvider.SetError(@this, string.Empty);
  43. return true;
  44. }
  45. public static bool CheckNumber(this ButtonEdit @this, DXErrorProvider dxErrorProvider, string msg)
  46. {
  47. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  48. {
  49. dxErrorProvider.SetError(@this, $"{msg}不能为空!");
  50. return false;
  51. }
  52. double number;
  53. bool isDoubleNum = Double.TryParse(@this.Text, out number);
  54. if (!isDoubleNum)
  55. {
  56. dxErrorProvider.SetError(@this, $"{msg}必须是数字!");
  57. return false;
  58. }
  59. dxErrorProvider.SetError(@this, string.Empty);
  60. return true;
  61. }
  62. public static bool CheckDateEdit(this DateEdit @this, DXErrorProvider dxErrorProvider, string msg)
  63. {
  64. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  65. {
  66. dxErrorProvider.SetError(@this, msg);
  67. return false;
  68. }
  69. else
  70. {
  71. dxErrorProvider.SetError(@this, string.Empty);
  72. return true;
  73. }
  74. }
  75. public static bool CheckFile(this ButtonEdit @this, DXErrorProvider dxErrorProvider, string msg)
  76. {
  77. if (string.IsNullOrWhiteSpace(@this.Text.Trim()))
  78. {
  79. dxErrorProvider.SetError(@this, $"{msg}不能为空!");
  80. return false;
  81. }
  82. if (!File.Exists(@this.Text.Trim()))
  83. {
  84. dxErrorProvider.SetError(@this, $"{msg}不存在!");
  85. return false;
  86. }
  87. dxErrorProvider.SetError(@this, string.Empty);
  88. return true;
  89. }
  90. }
  91. }