123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using ComponentFactory.Krypton.Toolkit;
- using NXWMS.Client.Model.AppModels.Result.SysSettings;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace NXWMS.Commons
- {
- /// <summary>
- /// 窗体通用工具
- /// </summary>
- public static class FrmCommonUtil
- {
- /// <summary>
- /// 设置控件基础
- /// </summary>
- /// <param name="c"></param>
- /// <param name="maxLength"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static bool SetControlBase(this Control c, int maxLength, string defaultValue = "")
- {
- switch (c.GetType().Name)
- {
- case "KryptonTextBox":
- (c as KryptonTextBox).MaxLength = maxLength;
- (c as KryptonTextBox).Text = defaultValue;
- return true;
- case "TextBox":
- (c as TextBox).MaxLength = maxLength;
- (c as TextBox).Text = defaultValue;
- return true;
- case "RichTextBox":
- (c as RichTextBox).MaxLength = maxLength;
- (c as RichTextBox).Text = defaultValue;
- return true;
- case "KryptonRichTextBox":
- (c as KryptonRichTextBox).MaxLength = maxLength;
- (c as KryptonRichTextBox).Text = defaultValue;
- return true;
- default:
- break;
- }
- return false;
- }
- /// <summary>
- /// 设定控件组中所有字段值等于Row
- /// </summary>
- /// <param name="container"></param>
- /// <param name="dataView"></param>
- /// <param name="row"></param>
- public static void SetGroupControls(this TableLayoutPanel container, DataGridView dataView, DataGridViewRow row)
- {
- foreach (Control c in container.Controls)
- {
- if (c.Tag != null)
- {
- if (c.Tag.GetType().Name == "String")
- {
- if (dataView.Columns.Contains(c.Tag.ToString()))
- {
- switch (c.GetType().Name)
- {
- case "KryptonCheckBox":
- var kryptonCheckBox = (c as KryptonCheckBox);
- kryptonCheckBox.Checked = (row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString()) == "1" ? true : false;
- break;
- case "CheckBox":
- var checkbox = (c as KryptonCheckBox);
- checkbox.Checked = (row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString()) == "1" ? true : false;
- break;
- case "KryptonComboBox":
- var kryptonComboBox = (c as KryptonComboBox);
- kryptonComboBox.SelectedValue = row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim();
- break;
- case "ComboBox":
- var comboBox = (c as ComboBox);
- comboBox.SelectedValue = row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim();
- break;
- case "KryptonDateTimePicker":
- var datePicker = (c as KryptonDateTimePicker);
- datePicker.Checked = row.Cells[c.Tag.ToString()].Value == null ? false : true;
- if (DateTime.TryParse(row.Cells[c.Tag.ToString()].Value==null?"": row.Cells[c.Tag.ToString()].Value.ToString(), out var _time))
- {
- datePicker.Value = _time;
- }
- // datePicker.Value = Convert.ToDateTime(row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim());
- break;
- case "KryptonTextBox":
- case "TextBox":
- default:
- c.Text = row.Cells[c.Tag.ToString()].Value == null ? "" : row.Cells[c.Tag.ToString()].Value.ToString().Trim();
- break;
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 设定控件组中所有字段值等于空
- /// </summary>
- /// <param name="container"></param>
- /// <param name="dataView"></param>
- public static void SetGroupControlsEmpty(this TableLayoutPanel container, DataGridView dataView)
- {
- foreach (Control c in container.Controls)
- {
- if (c.Tag != null)
- {
- if (c.Tag.GetType().Name == "String")
- {
- if (dataView.Columns.Contains(c.Tag.ToString()))
- {
- switch (c.GetType().Name)
- {
- case "KryptonCheckBox":
- var kryptonCheckBox = (c as KryptonCheckBox);
- kryptonCheckBox.Checked = false;
- break;
- case "CheckBox":
- var checkbox = (c as KryptonCheckBox);
- checkbox.Checked = false;
- break;
- case "KryptonComboBox":
- var kryptonComboBox = (c as KryptonComboBox);
- kryptonComboBox.SelectedIndex = -1;
- break;
- case "ComboBox":
- var comboBox = (c as ComboBox);
- comboBox.SelectedIndex = -1;
- break;
- case "KryptonTextBox":
- case "TextBox":
- default:
- c.Text = "";
- break;
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 授权设置Panel按钮隐藏
- /// </summary>
- /// <param name="form"></param>
- /// <param name="menuLevelList"></param>
- /// <returns></returns>
- public static void SetFormPanelEnabled(this TableLayoutPanel container, List<MenuLevelResult> menuLevelList)
- {
- foreach (Control c in container.Controls)
- {
- if (c.Tag != null)
- {
- if (c.Tag.GetType().Name == "String")
- {
- if (c.Tag.ToString() == "Auth")
- {
- if (menuLevelList.Where(s => $"{c.FindForm().GetType().FullName}.{c.Name}" == s.FunctionURL).Any())
- {
- c.Enabled = true;
- }
- else
- {
- c.Enabled = false;
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 获取DataGridView符合条件行下对应拼接Value值
- /// </summary>
- /// <param name="dataView">对象</param>
- /// <param name="rowIndexList">选中行索引列表</param>
- /// <param name="columnsName">拼接列名称</param>
- /// <param name="splitChar">分割符</param>
- /// <returns></returns>
- public static string GetColumnJoinString(this DataGridView dataView, List<int> rowIndexList, string columnsName, string splitChar = "\r\n")
- {
- var strList = new List<string>();
- foreach (int i in rowIndexList)
- {
- strList.Add(dataView.Rows[i].Cells[columnsName].Value.GetObjectToString());
- }
- return string.Join(splitChar, strList);
- }
- /// <summary>
- /// 获取KryptonDateTimePicker Value
- /// </summary>
- /// <param name="dtpicker"></param>
- /// <returns></returns>
- public static DateTime? GetDateTimeValue(this KryptonDateTimePicker dtpicker)
- {
- if (!dtpicker.Checked)
- {
- return null;
- }
- else
- {
- return dtpicker.Value;
- }
- }
- /// <summary>
- /// 设置窗体大小和位置
- /// </summary>
- /// <param name="form"></param>
- /// <param name="proportionSize">大小比例</param>
- /// <param name="startPosition">窗体初始位置</param>
- public static void SetFormSizeLocation(this Form form, double proportionSize = 0.8, FormStartPosition startPosition = FormStartPosition.CenterScreen)
- {
- var screenArea = Screen.GetWorkingArea(form);
- form.Size = new Size(Convert.ToInt32(screenArea.Width * proportionSize), Convert.ToInt32(screenArea.Height * proportionSize));
- form.Location = new Point((screenArea.Width - form.Width) / 2,
- (screenArea.Height - form.Height) / 2);
- }
- }
- }
|