FrmCommonUtil.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using ComponentFactory.Krypton.Toolkit;
  2. using NXWMS.Client.Model.AppModels.Result.SysSettings;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace NXWMS.Commons
  10. {
  11. /// <summary>
  12. /// 窗体通用工具
  13. /// </summary>
  14. public static class FrmCommonUtil
  15. {
  16. /// <summary>
  17. /// 设置控件基础
  18. /// </summary>
  19. /// <param name="c"></param>
  20. /// <param name="maxLength"></param>
  21. /// <param name="defaultValue"></param>
  22. /// <returns></returns>
  23. public static bool SetControlBase(this Control c, int maxLength, string defaultValue = "")
  24. {
  25. switch (c.GetType().Name)
  26. {
  27. case "KryptonTextBox":
  28. (c as KryptonTextBox).MaxLength = maxLength;
  29. (c as KryptonTextBox).Text = defaultValue;
  30. return true;
  31. case "TextBox":
  32. (c as TextBox).MaxLength = maxLength;
  33. (c as TextBox).Text = defaultValue;
  34. return true;
  35. case "RichTextBox":
  36. (c as RichTextBox).MaxLength = maxLength;
  37. (c as RichTextBox).Text = defaultValue;
  38. return true;
  39. case "KryptonRichTextBox":
  40. (c as KryptonRichTextBox).MaxLength = maxLength;
  41. (c as KryptonRichTextBox).Text = defaultValue;
  42. return true;
  43. default:
  44. break;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// 设定控件组中所有字段值等于Row
  50. /// </summary>
  51. /// <param name="container"></param>
  52. /// <param name="dataView"></param>
  53. /// <param name="row"></param>
  54. public static void SetGroupControls(this TableLayoutPanel container, DataGridView dataView, DataGridViewRow row)
  55. {
  56. foreach (Control c in container.Controls)
  57. {
  58. if (c.Tag != null)
  59. {
  60. if (c.Tag.GetType().Name == "String")
  61. {
  62. if (dataView.Columns.Contains(c.Tag.ToString()))
  63. {
  64. switch (c.GetType().Name)
  65. {
  66. case "KryptonCheckBox":
  67. var kryptonCheckBox = (c as KryptonCheckBox);
  68. kryptonCheckBox.Checked = (row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString()) == "1" ? true : false;
  69. break;
  70. case "CheckBox":
  71. var checkbox = (c as KryptonCheckBox);
  72. checkbox.Checked = (row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString()) == "1" ? true : false;
  73. break;
  74. case "KryptonComboBox":
  75. var kryptonComboBox = (c as KryptonComboBox);
  76. kryptonComboBox.SelectedValue = row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim();
  77. break;
  78. case "ComboBox":
  79. var comboBox = (c as ComboBox);
  80. comboBox.SelectedValue = row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim();
  81. break;
  82. case "KryptonDateTimePicker":
  83. var datePicker = (c as KryptonDateTimePicker);
  84. datePicker.Checked = row.Cells[c.Tag.ToString()].Value == null ? false : true;
  85. if (DateTime.TryParse(row.Cells[c.Tag.ToString()].Value==null?"": row.Cells[c.Tag.ToString()].Value.ToString(), out var _time))
  86. {
  87. datePicker.Value = _time;
  88. }
  89. // datePicker.Value = Convert.ToDateTime(row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim());
  90. break;
  91. case "KryptonTextBox":
  92. case "TextBox":
  93. default:
  94. c.Text = row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim();
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// 设定控件组中所有字段值等于空
  104. /// </summary>
  105. /// <param name="container"></param>
  106. /// <param name="dataView"></param>
  107. public static void SetGroupControlsEmpty(this TableLayoutPanel container, DataGridView dataView)
  108. {
  109. foreach (Control c in container.Controls)
  110. {
  111. if (c.Tag != null)
  112. {
  113. if (c.Tag.GetType().Name == "String")
  114. {
  115. if (dataView.Columns.Contains(c.Tag.ToString()))
  116. {
  117. switch (c.GetType().Name)
  118. {
  119. case "KryptonCheckBox":
  120. var kryptonCheckBox = (c as KryptonCheckBox);
  121. kryptonCheckBox.Checked = false;
  122. break;
  123. case "CheckBox":
  124. var checkbox = (c as KryptonCheckBox);
  125. checkbox.Checked = false;
  126. break;
  127. case "KryptonComboBox":
  128. var kryptonComboBox = (c as KryptonComboBox);
  129. kryptonComboBox.SelectedIndex = -1;
  130. break;
  131. case "ComboBox":
  132. var comboBox = (c as ComboBox);
  133. comboBox.SelectedIndex = -1;
  134. break;
  135. case "KryptonTextBox":
  136. case "TextBox":
  137. default:
  138. c.Text = "";
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// 授权设置Panel按钮隐藏
  148. /// </summary>
  149. /// <param name="form"></param>
  150. /// <param name="menuLevelList"></param>
  151. /// <returns></returns>
  152. public static void SetFormPanelEnabled(this TableLayoutPanel container, List<MenuLevelResult> menuLevelList)
  153. {
  154. foreach (Control c in container.Controls)
  155. {
  156. if (c.Tag != null)
  157. {
  158. if (c.Tag.GetType().Name == "String")
  159. {
  160. if (c.Tag.ToString() == "Auth")
  161. {
  162. if (menuLevelList.Where(s => $"{c.FindForm().GetType().FullName}.{c.Name}" == s.FunctionURL).Any())
  163. {
  164. c.Enabled = true;
  165. }
  166. else
  167. {
  168. c.Enabled = false;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// 获取DataGridView符合条件行下对应拼接Value值
  177. /// </summary>
  178. /// <param name="dataView">对象</param>
  179. /// <param name="rowIndexList">选中行索引列表</param>
  180. /// <param name="columnsName">拼接列名称</param>
  181. /// <param name="splitChar">分割符</param>
  182. /// <returns></returns>
  183. public static string GetColumnJoinString(this DataGridView dataView, List<int> rowIndexList, string columnsName, string splitChar = "\r\n")
  184. {
  185. var strList = new List<string>();
  186. foreach (int i in rowIndexList)
  187. {
  188. strList.Add(dataView.Rows[i].Cells[columnsName].Value.GetObjectToString());
  189. }
  190. return string.Join(splitChar, strList);
  191. }
  192. /// <summary>
  193. /// 获取KryptonDateTimePicker Value
  194. /// </summary>
  195. /// <param name="dtpicker"></param>
  196. /// <returns></returns>
  197. public static DateTime? GetDateTimeValue(this KryptonDateTimePicker dtpicker)
  198. {
  199. if (!dtpicker.Checked)
  200. {
  201. return null;
  202. }
  203. else
  204. {
  205. return dtpicker.Value;
  206. }
  207. }
  208. /// <summary>
  209. /// 设置窗体大小和位置
  210. /// </summary>
  211. /// <param name="form"></param>
  212. /// <param name="proportionSize">大小比例</param>
  213. /// <param name="startPosition">窗体初始位置</param>
  214. public static void SetFormSizeLocation(this Form form, double proportionSize = 0.8, FormStartPosition startPosition = FormStartPosition.CenterScreen)
  215. {
  216. var screenArea = Screen.GetWorkingArea(form);
  217. form.Size = new Size(Convert.ToInt32(screenArea.Width * proportionSize), Convert.ToInt32(screenArea.Height * proportionSize));
  218. form.Location = new Point((screenArea.Width - form.Width) / 2,
  219. (screenArea.Height - form.Height) / 2);
  220. }
  221. }
  222. }