lg
2024-09-22 c115ac388af5a6928716f0146a75a2a42fb27d8e
DevApp/Gs.DevApp/ToolBox/UtilityHelper.cs
@@ -1,6 +1,5 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
@@ -10,11 +9,13 @@
using System.Resources;
using System.Drawing;
using Newtonsoft.Json;
using Gs.DevApp.Models;
using Gs.DevApp.Entity;
using System.Windows.Forms;
using static System.Windows.Forms.Control;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace Gs.DevApp.ToolBox
{
@@ -73,6 +74,17 @@
            }
            return responseStr;
        }
        /// <summary>
        /// 读取默认页大小
        /// </summary>
        /// <returns></returns>
        public static int GetPageSize()
        {
            return 50;
            // return int.Parse(System.Configuration.ConfigurationSettings.AppSettings.Get("PageSize").ToString());
        }
        /// <summary>
        /// 根据图片名读取资源文件,不带后缀名
        /// </summary>
@@ -112,11 +124,11 @@
            return token;
        }
        /// <summary>
        /// 标准json串返回ReturnModel-->table,
        /// 标准json返回ReturnModel-->包含TablePage分页,
        /// </summary>
        /// <param name="strReturn"></param>
        /// <returns></returns>
        public static ReturnModel<PageListModel> GetTableByJson(string strReturn)
        public static ReturnModel<PageListModel> ReturnToTablePage(string strReturn)
        {
            ReturnModel<PageListModel> rto = new ReturnModel<PageListModel>();
            JObject json = JObject.Parse(strReturn);
@@ -137,11 +149,11 @@
            return rto;
        }
        /// <summary>
        /// 标准json串返回ReturnModel->字符串,
        /// 标准json返回ReturnModel,
        /// </summary>
        /// <param name="strReturn"></param>
        /// <returns></returns>
        public static ReturnModel<dynamic> GetDataByJson(string strReturn)
        public static ReturnModel<dynamic> ReturnToDynamic(string strReturn)
        {
            ReturnModel<dynamic> rto = new ReturnModel<dynamic>();
            JObject json = JObject.Parse(strReturn);
@@ -150,89 +162,176 @@
            rto.rtnData = json["rtnData"];
            return rto;
        }
        /// <summary>
        /// 设置系统字体大小,目前并不通用
        /// </summary>
        public static float GetFontSize = 10;
        public static void SetFont(Control control)
        {
            float size = GetFontSize;
            foreach (Control childControl in control.Controls)
            {
                childControl.Font = new Font(childControl.Font.FontFamily, size, childControl.Font.Style);
                SetFont(childControl);
            }
        }
        /// <summary>
        /// 读取默认页大小
        /// 标准json串返回ReturnModel-->仅仅有list,不分页,
        /// </summary>
        /// <param name="strReturn"></param>
        /// <returns></returns>
        public static int GetPageSize()
        public static ReturnModel<DataTable> ReturnToList(string strReturn)
        {
            return int.Parse(System.Configuration.ConfigurationSettings.AppSettings.Get("PageSize").ToString());
            ReturnModel<DataTable> rto = new ReturnModel<DataTable>();
            JObject json = JObject.Parse(strReturn);
            rto.rtnCode = int.Parse(json["rtnCode"].ToString());
            rto.rtnMsg = json["rtnMsg"].ToString();
            rto.rtnData = new DataTable();
            JArray array = new JArray();
            var d = json["rtnData"];
            foreach (var a in d)
            {
                array.Add(a);
            }
            DataTable dt = JsonConvert.DeserializeObject<DataTable>(array.ToString());
            rto.rtnData = dt;
            return rto;
        }
        /// <summary>
        /// 根据对象批量设置文本值,
        /// 根据对象批量设置文本值
        /// </summary>
        /// <param name="controls"></param>
        /// <param name="dynamicObject"></param>
        public static void SetValueByObj(ControlCollection controls, dynamic dynamicObject, Boolean isEdt)
        /// <param name="controls">controls:为groupBox1.Controls/panel1.Controls</param>
        /// <param name="dynamicObject">对像</param>
        /// <param name="isEdt">是否可编辑</param>
        public static void SetValueByObj(ControlCollection controls, dynamic dynamicObject, Boolean isEdt, List<DevExpress.XtraGrid.Views.Grid.GridView> gridViews = null)
        {
            if (gridViews != null)
            {
                foreach (DevExpress.XtraGrid.Views.Grid.GridView gv in gridViews)
                {
                    gv.OptionsBehavior.Editable = isEdt;
                }
            }
            foreach (JProperty property in dynamicObject.Properties())
            {
                //Console.WriteLine("Name: {0}, Value: {1}", property.Name, property.Value);
                string strName = property.Name;
                string strVal = property.Value.ToString();
                //    // 如果value是一个对象,可以递归遍历
                //    if (property.Value is JObject)
                //    {
                //        JObject nestedObject = (JObject)property.Value;
                //        foreach (JProperty nestedProperty in nestedObject.Properties())
                //        {
                //            Console.WriteLine("\tName: {0}, Value: {1}", nestedProperty.Name, nestedProperty.Value);
                //        }
                //    }
                Control[] cols = controls.Find("txt_" + strName, true);
                if (cols.Length > 0)
                try
                {
                    Control colType = cols[0];
                    if (colType is ComboBoxEdit)
                    //    // 如果value是一个对象,可以递归遍历
                    //    if (property.Value is JObject)
                    //    {
                    //        JObject nestedObject = (JObject)property.Value;
                    //        foreach (JProperty nestedProperty in nestedObject.Properties())
                    //        {
                    //            Console.WriteLine("\tName: {0}, Value: {1}", nestedProperty.Name, nestedProperty.Value);
                    //        }
                    //    }
                    Control[] cols = controls.Find("txt_" + strName, true);
                    if (cols.Length > 0)
                    {
                        ComboBoxEdit txt = colType as ComboBoxEdit;
                        txt.SelectedIndex = int.Parse(strVal);
                        txt.Enabled = isEdt;
                        continue;
                    }
                    if (colType is TextEdit)
                    {
                        TextEdit txt = colType as TextEdit;
                        if (txt != null)
                        Control colType = cols[0];
                        //LookUpEdit
                        if (colType is LookUpEdit)
                        {
                            txt.Text = strVal;
                            LookUpEdit txt = colType as LookUpEdit;
                            if (txt != null)
                            {
                                txt.EditValue = strVal;
                                // txt.Text = strVal;
                            }
                            txt.Enabled = isEdt;
                            continue;
                        }
                        continue;
                        //下拉
                        if (colType is ComboBoxEdit)
                        {
                            ComboBoxEdit txt = colType as ComboBoxEdit;
                            if (txt.Properties.TextEditStyle == DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor)
                                txt.SelectedIndex = int.Parse(strVal);
                            else
                                txt.Text = strVal;
                            txt.Enabled = isEdt;
                            continue;
                        }
                        //文本
                        if (colType is TextEdit)
                        {
                            TextEdit txt = colType as TextEdit;
                            if (txt != null)
                                txt.Text = strVal;
                            txt.Enabled = isEdt;
                            continue;
                        }
                        //数字卡
                        if (colType is NumericUpDown)
                        {
                            NumericUpDown txt = colType as NumericUpDown;
                            if (txt != null)
                                txt.Text = strVal;
                            txt.Enabled = isEdt;
                            continue;
                        }
                        //单选
                        if (colType is CheckEdit)
                        {
                            CheckEdit txt = colType as CheckEdit;
                            if (txt != null)
                                txt.Checked = (strVal.ToString() == "1" ? true : false);
                            txt.Enabled = isEdt;
                            continue;
                        }
                        //时间
                        if (colType is DateTimePicker)
                        {
                            DateTimePicker txt = colType as DateTimePicker;
                            if (txt != null)
                                txt.Checked = (strVal.ToString() == "1" ? true : false);
                            txt.Enabled = isEdt;
                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(strName + ex.Message);
                }
            }
        }
        /// <summary>
        /// 清空容器里面的控件
        /// </summary>
        /// <param name="controls">容器</param>
        /// <param name="isEdt">清空后是否可编辑</param>
        public static void CleanValue(ControlCollection controls, Boolean isEdt)
        /// <param name="controls">controls:为groupBox1.Controls/panel1.Controls</param>
        /// <param name="isEdt">是否可编辑</param>
        public static void CleanValueByControl(ControlCollection controls, Boolean isEdt, List<DevExpress.XtraGrid.Views.Grid.GridView> gridViews = null)
        {
            foreach (Control ctrl in controls)//或为groupBox1.Controls/panel1.Controls
            if (gridViews != null)
            {
                foreach (DevExpress.XtraGrid.Views.Grid.GridView gv in gridViews)
                {
                    gv.OptionsBehavior.Editable = isEdt;
                }
            }
            foreach (Control ctrl in controls)
            {
                ctrl.Enabled = isEdt;
                //多行文本
                if (ctrl is MemoEdit)
                {
                    MemoEdit txt = ctrl as MemoEdit;
                    txt.Text = "";
                    txt.Enabled = isEdt;
                    continue;
                }
                //下拉
                if (ctrl is ComboBoxEdit)
                {
                    ComboBoxEdit txt = ctrl as ComboBoxEdit;
                    if (txt.Properties.TextEditStyle == DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor)
                        txt.SelectedIndex = 0;
                    else
                        txt.Text = "";
                    txt.Enabled = isEdt;
                    continue;
                }
                //文本
                if (ctrl is TextEdit)
                {
                    ctrl.Text = "";
                    ctrl.Enabled = isEdt;
                    continue;
                }
            }
        }
@@ -240,14 +339,39 @@
        /// <summary>
        /// 禁用或启用容器里面的控件
        /// </summary>
        /// <param name="controls">容器</param>
        /// <param name="controls">controls:为groupBox1.Controls/panel1.Controls</param>
        /// <param name="isEdt">是否可编辑</param>
        public static void ChangeEnable(ControlCollection controls, Boolean isEdt)
        public static void ChangeEnableByControl(ControlCollection controls, Boolean isEdt, List<DevExpress.XtraGrid.Views.Grid.GridView> gridViews = null)
        {
            foreach (Control ctrl in controls)//或为groupBox1.Controls/panel1.Controls
            if (gridViews != null)
            {
                foreach (DevExpress.XtraGrid.Views.Grid.GridView gv in gridViews)
                {
                    gv.OptionsBehavior.Editable = isEdt;
                }
            }
            foreach (Control ctrl in controls)
            {
                ctrl.Enabled = isEdt;
                //文本
                if (ctrl is TextEdit)
                {
                    ctrl.Enabled = isEdt;
                    continue;
                }
                //数字卡
                if (ctrl is NumericUpDown)
                {
                    ctrl.Enabled = isEdt;
                    continue;
                }
                //日期
                if (ctrl is DateTimePicker)
                {
                    ctrl.Enabled = isEdt;
                    continue;
                }
            }
        }
@@ -256,7 +380,7 @@
        /// </summary>
        /// <param name="tabControl">选项卡容器</param>
        /// <param name="idx">从0开始,如果是999,则全部可用</param>
        public static void ChangeTab(XtraTabControl tabControl, int idx)
        public static void JumpToTab(XtraTabControl tabControl, int idx)
        {
            if (idx == 999)
            {
@@ -274,7 +398,6 @@
            tabControl.TabPages[idx].PageEnabled = true;
            tabControl.SelectedTabPageIndex = idx;
        }
        public static void TreeViewCheck(TreeViewEventArgs e)
        {
@@ -323,5 +446,127 @@
            }
        }
        /// <summary>
        /// 转驼峰命名
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToCamelCase(string propertyName)
        {
            if (propertyName.Length > 0 && propertyName.All(char.IsUpper))
                propertyName = propertyName.ToLower();
            string dd = Regex.Replace(propertyName, @"_([a-z])", m => m.Groups[1].Value.ToUpper());
            return dd;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="xtraTabControl1"></param>
        /// <param name="lbGuid">编辑框中的主键名</param>
        /// <param name="txtName">编辑框中的文本框名</param>
        /// <param name="gridView1"></param>
        /// <param name="gridRowName">列表中的文本列名</param>
        /// <param name="SelectedTabPageIndex"></param>
        /// <returns></returns>
        public static (string, string) GetCurrentRow(XtraTabControl xtraTabControl1
            , System.Windows.Forms.Label lbGuid
            , DevExpress.XtraEditors.TextEdit txtName
            , DevExpress.XtraGrid.Views.Grid.GridView gridView1
            , int SelectedTabPageIndex = 1)
        {
            string _strGuid = "";
            string _strName = "";
            if (xtraTabControl1.SelectedTabPageIndex == SelectedTabPageIndex && lbGuid.Text.Length > 10)
            {
                _strGuid = lbGuid.Text.Trim();
                _strName = txtName.Text.Trim();
            }
            else
            {
                DataRow dr = gridView1.GetFocusedDataRow();
                if (dr == null || string.IsNullOrEmpty(dr["guid"].ToString()))
                {
                }
                else {
                    _strGuid = dr["guid"].ToString();
                    _strName = dr[1].ToString();
                }
            }
            return (_strGuid, _strName);
        }
        public static (string, string) GetCurrentRow(XtraTabControl xtraTabControl1
            , System.Windows.Forms.Label lbGuid
            , DevExpress.XtraEditors.TextEdit txtName
            , System.Windows.Forms.TreeView tlMenu
            , int SelectedTabPageIndex = 1)
        {
            string _strGuid = "";
            string _strName = "";
            if (xtraTabControl1.SelectedTabPageIndex == SelectedTabPageIndex && lbGuid.Text.Length > 10)
            {
                _strGuid = lbGuid.Text.Trim();
                _strName = txtName.Text.Trim();
            }
            else
            {
                TreeNode clickedNode = tlMenu.SelectedNode;
                if (clickedNode != null)
                {
                    _strGuid = clickedNode.Name.ToString();
                    _strName = clickedNode.Text.Trim();
                }
            }
            return (_strGuid, _strName);
        }
        public static (string, string) GetCurrentRow(XtraTabControl xtraTabControl1
            , System.Windows.Forms.Label lbGuid
             , DevExpress.XtraEditors.TextEdit txtName
            , DevExpress.XtraTreeList.TreeList tlMenu
            , int SelectedTabPageIndex = 1)
        {
            string _strGuid = "";
            string _strName = "";
            if (xtraTabControl1.SelectedTabPageIndex == SelectedTabPageIndex && lbGuid.Text.Length > 10)
            {
                _strGuid = lbGuid.Text.Trim();
                _strName = txtName.Text.Trim();
            }
            else
            {
                _strGuid = tlMenu.FocusedNode.GetValue("guid").ToString();
                _strName = tlMenu.FocusedNode.GetValue(0).ToString();
            }
            return (_strGuid, _strName);
        }
    }
    public class CboItemEntity
    {
        private object _text = 0;
        private object _Value = "";
        /// <summary>
        /// 显示值
        /// </summary>
        public object Text
        {
            get { return this._text; }
            set { this._text = value; }
        }
        /// <summary>
        /// 对象值
        /// </summary>
        public object Value
        {
            get { return this._Value; }
            set { this._Value = value; }
        }
        public override string ToString()
        {
            return this.Text.ToString();
        }
    }
}