lg
2024-09-06 3d6533f1381e3e513ffb3d65bf34fb254f967088
DevApp/Gs.DevApp/ToolBox/UtilityHelper.cs
@@ -12,6 +12,9 @@
using Newtonsoft.Json;
using Gs.DevApp.Models;
using System.Windows.Forms;
using static System.Windows.Forms.Control;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
namespace Gs.DevApp.ToolBox
{
@@ -160,5 +163,165 @@
                SetFont(childControl);
            }
        }
        /// <summary>
        /// 读取默认页大小
        /// </summary>
        /// <returns></returns>
        public static int GetPageSize()
        {
            return int.Parse(System.Configuration.ConfigurationSettings.AppSettings.Get("PageSize").ToString());
        }
        /// <summary>
        /// 根据对象批量设置文本值,
        /// </summary>
        /// <param name="controls"></param>
        /// <param name="dynamicObject"></param>
        public static void SetValueByObj(ControlCollection controls, dynamic dynamicObject, Boolean 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)
                {
                    Control colType = cols[0];
                    if (colType is ComboBoxEdit)
                    {
                        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)
                        {
                            txt.Text = strVal;
                            txt.Enabled = isEdt;
                        }
                        continue;
                    }
                }
            }
        }
        /// <summary>
        /// 清空容器里面的控件
        /// </summary>
        /// <param name="controls">容器</param>
        /// <param name="isEdt">清空后是否可编辑</param>
        public static void CleanValue(ControlCollection controls, Boolean isEdt)
        {
            foreach (Control ctrl in controls)//或为groupBox1.Controls/panel1.Controls
            {
                if (ctrl is TextEdit)
                {
                    ctrl.Text = "";
                    ctrl.Enabled = isEdt;
                }
            }
        }
        /// <summary>
        /// 禁用或启用容器里面的控件
        /// </summary>
        /// <param name="controls">容器</param>
        /// <param name="isEdt">是否可编辑</param>
        public static void ChangeEnable(ControlCollection controls, Boolean isEdt)
        {
            foreach (Control ctrl in controls)//或为groupBox1.Controls/panel1.Controls
            {
                if (ctrl is TextEdit)
                    ctrl.Enabled = isEdt;
            }
        }
        /// <summary>
        ///切换选项卡
        /// </summary>
        /// <param name="tabControl">选项卡容器</param>
        /// <param name="idx">从0开始,如果是999,则全部可用</param>
        public static void ChangeTab(XtraTabControl tabControl, int idx)
        {
            if (idx == 999)
            {
                for (int i = 0; i < tabControl.TabPages.Count; i++)
                {
                    tabControl.TabPages[i].PageEnabled = true;
                }
                tabControl.SelectedTabPageIndex = tabControl.TabPages.Count - 1;
                return;
            }
            for (int i = 0; i < tabControl.TabPages.Count; i++)
            {
                tabControl.TabPages[i].PageEnabled = false;
            }
            tabControl.TabPages[idx].PageEnabled = true;
            tabControl.SelectedTabPageIndex = idx;
        }
        public static void TreeViewCheck(TreeViewEventArgs e)
        {
            try
            {
                if (e.Node.Nodes.Count > 0)
                {
                    bool NoFalse = true;
                    foreach (TreeNode tn in e.Node.Nodes)
                    {
                        if (tn.Checked == false)
                        {
                            NoFalse = false;
                        }
                    }
                    if (e.Node.Checked == true || NoFalse)
                    {
                        foreach (TreeNode tn in e.Node.Nodes)
                        {
                            if (tn.Checked != e.Node.Checked)
                            {
                                tn.Checked = e.Node.Checked;
                            }
                        }
                    }
                }
                if (e.Node.Parent != null && e.Node.Parent is TreeNode)
                {
                    bool ParentNode = true;
                    foreach (TreeNode tn in e.Node.Parent.Nodes)
                    {
                        if (tn.Checked == false)
                        {
                            ParentNode = false;
                        }
                    }
                    if (e.Node.Parent.Checked != ParentNode && (e.Node.Checked == false || e.Node.Checked == true && e.Node.Parent.Checked == false))
                    {
                        e.Node.Parent.Checked = ParentNode;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}