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 { /// /// 窗体通用工具 /// public static class FrmCommonUtil { /// /// 设置控件基础 /// /// /// /// /// 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; } /// /// 设定控件组中所有字段值等于Row /// /// /// /// 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; } } } } } } /// /// 设定控件组中所有字段值等于空 /// /// /// 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; } } } } } } /// /// 授权设置Panel按钮隐藏 /// /// /// /// public static void SetFormPanelEnabled(this TableLayoutPanel container, List 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; } } } } } } /// /// 获取DataGridView符合条件行下对应拼接Value值 /// /// 对象 /// 选中行索引列表 /// 拼接列名称 /// 分割符 /// public static string GetColumnJoinString(this DataGridView dataView, List rowIndexList, string columnsName, string splitChar = "\r\n") { var strList = new List(); foreach (int i in rowIndexList) { strList.Add(dataView.Rows[i].Cells[columnsName].Value.GetObjectToString()); } return string.Join(splitChar, strList); } /// /// 获取KryptonDateTimePicker Value /// /// /// public static DateTime? GetDateTimeValue(this KryptonDateTimePicker dtpicker) { if (!dtpicker.Checked) { return null; } else { return dtpicker.Value; } } /// /// 设置窗体大小和位置 /// /// /// 大小比例 /// 窗体初始位置 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); } } }