ButtonEditExtensions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using DevExpress.CodeParser;
  2. using DevExpress.Utils;
  3. using DevExpress.XtraEditors;
  4. using DevExpress.XtraEditors.Controls;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Ips.Library.Basic;
  11. using DevExpress.Utils.Url;
  12. namespace Ips.Library.DxpLib
  13. {
  14. public static class ButtonEditExtensions
  15. {
  16. public static ButtonEdit SetUnitText(this ButtonEdit source, string unitText, bool isDecimal = true, bool clearOther = true, string mask = "")
  17. {
  18. if (clearOther)
  19. {
  20. source.Properties.Buttons.Clear();
  21. }
  22. var unitBtn = new EditorButton()
  23. {
  24. Caption = unitText,
  25. Kind = ButtonPredefines.Glyph,
  26. Enabled = false,
  27. };
  28. var props = source.Properties;
  29. props.Appearance.Options.UseTextOptions = true;
  30. props.Appearance.TextOptions.HAlignment = HorzAlignment.Near;
  31. props.MaskSettings.UseMaskAsDisplayFormat = true;
  32. if (isDecimal)
  33. {
  34. props.MaskSettings.MaskExpression = mask.IfNullOrWhitespace("############0.0#####");
  35. }
  36. else
  37. {
  38. props.MaskSettings.MaskExpression = mask.IfNullOrWhitespace("d");
  39. }
  40. props.Buttons.Add(unitBtn);
  41. return source;
  42. }
  43. public static ButtonEdit SetFileOpen(this ButtonEdit source, string fileName = "", string title = "", string initPath = "", string filter = "", bool mutiSelect = false, bool clearOther = true)
  44. {
  45. var props = source.Properties;
  46. if (clearOther) props.Buttons.Clear();
  47. title = title.IfNullOrWhitespace("选择文件");
  48. var btnOpenFile = new EditorButton()
  49. {
  50. Caption = title,
  51. Kind = ButtonPredefines.Glyph
  52. };
  53. btnOpenFile.ImageOptions.ImageUri.Uri = "actions/open2";
  54. btnOpenFile.ImageOptions.SvgImageSize = new Size(12, 12);
  55. props.Buttons.Add(btnOpenFile);
  56. source.ButtonClick += (sender, args) =>
  57. {
  58. if (args.Button.Caption == title)
  59. {
  60. string selectFile = ToolDialog.OpenFile(fileName, title, initPath, filter, mutiSelect);
  61. source.EditValue = selectFile;
  62. }
  63. };
  64. return source;
  65. }
  66. public static ButtonEdit SetFolderOpen(this ButtonEdit source, string title = "", bool clearOther = true)
  67. {
  68. var props = source.Properties;
  69. if (clearOther) props.Buttons.Clear();
  70. title = title.IfNullOrWhitespace("选择文件");
  71. var btnOpenFile = new EditorButton()
  72. {
  73. Caption = title,
  74. Kind = ButtonPredefines.Glyph
  75. };
  76. btnOpenFile.ImageOptions.ImageUri.Uri = "actions/open";
  77. btnOpenFile.ImageOptions.SvgImageSize = new Size(12, 12);
  78. props.Buttons.Add(btnOpenFile);
  79. source.ButtonClick += (sender, args) =>
  80. {
  81. if (args.Button.Caption == title)
  82. {
  83. string selectFile = ToolDialog.OpenFolder(title);
  84. source.EditValue = selectFile;
  85. }
  86. };
  87. return source;
  88. }
  89. public static ButtonEdit SetFileSave(this ButtonEdit source, string fileName = "", string title = "", string initPath = "", string filter = "", bool clearOther = true)
  90. {
  91. var props = source.Properties;
  92. if (clearOther) props.Buttons.Clear();
  93. title = title.IfNullOrWhitespace("选择文件");
  94. var btnOpenFile = new EditorButton()
  95. {
  96. Caption = title,
  97. Kind = ButtonPredefines.Glyph
  98. };
  99. btnOpenFile.ImageOptions.ImageUri.Uri = "actions/open2";
  100. btnOpenFile.ImageOptions.SvgImageSize = new Size(12, 12);
  101. props.Buttons.Add(btnOpenFile);
  102. source.ButtonClick += (sender, args) =>
  103. {
  104. if (args.Button.Caption == title)
  105. {
  106. string selectFile = ToolDialog.SaveFile(fileName, title, initPath, filter);
  107. source.EditValue = selectFile;
  108. }
  109. };
  110. return source;
  111. }
  112. public static ButtonEdit EnableDragDropFile(this ButtonEdit source)
  113. {
  114. ((TextEdit)source).EnableDragDropFile();
  115. return source;
  116. }
  117. }
  118. }