• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

!!无须定义配置文件中的每个变量的读写操作,以下代码遍历界面中各个c#控件,自动记 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


namespace PluginLib
{
    /// <summary>
    /// 遍历控件所有子控件并初始化或保存其值
    /// </summary>
    public class FormHelper
    {
        private static string Path
        {
            get
            {
                return Plugin.Path + "\\BufferFile.ini";
            }
        }
        static IniFile iniFile = null;

        /// <summary>
        /// 保存控件值
        /// </summary>
        /// <param name="control"></param>
        public static void SaveControl(Control control)
        {
            FileStream fs;
            if (File.Exists(Path))
            {
                File.Delete(Path);
            }
            fs = File.Create(Path);
            fs.Close();
            iniFile = new IniFile(Path);
            GetControlValue(control);
            iniFile = null;
        }
        private static void GetControlValue(Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                if (!ctl.HasChildren)
                {
                    if (ctl.Parent is System.Windows.Forms.NumericUpDown)
                    {
                        NumericUpDown tempNumberic = ctl.Parent as NumericUpDown;
                        iniFile.IniWriteValue("NumericUpDown", tempNumberic.Name, tempNumberic.Value.ToString());
                    }
                    else if (ctl is CheckBox)
                    {
                        iniFile.IniWriteValue("CheckBox", ctl.Name, ((CheckBox)ctl).Checked.ToString());
                    }
                    else if (ctl is RadioButton)
                    {
                        iniFile.IniWriteValue("RadioButton", ctl.Name, ((RadioButton)ctl).Checked.ToString());
                    }
                    else if (ctl is TextBox)
                    {
                        iniFile.IniWriteValue("TextBox", ctl.Name, ((TextBox)ctl).Text);
                    }
                    else if (ctl is ComboBox)
                    {
                        iniFile.IniWriteValue("ComboBox", ctl.Name, ((ComboBox)ctl).Text);
                    }
                    else if (ctl is TrackBar)
                    {
                        iniFile.IniWriteValue("TrackBar", ctl.Name, ((TrackBar)ctl).Value.ToString());
                    }
                    else if (ctl is CheckBoxX)
                    {
                        iniFile.IniWriteValue("CheckBoxX", ctl.Name, ((CheckBoxX)ctl).Checked.ToString());
                    }
                    else if (ctl is TextBoxX)
                    {
                        iniFile.IniWriteValue("TextBoxX", ctl.Name, ((TextBoxX)ctl).Text.ToString());
                    }
                    else if (ctl is RichTextBox)
                    {
                     
                        string[] strs = Regex.Split(((RichTextBox)ctl).Text.Trim(),"\n");
                        int i = 0;
                        foreach (string str in strs)
                        {
                            iniFile.IniWriteValue("RichTextBox="+ctl.Name, "row" + i, str);
                            i++;
                        }
                      
                    }

                }
                else
                {
                    GetControlValue(ctl);
                }

            }
        }
        /// <summary>
        /// 加载控件值
        /// </summary>
        /// <param name="control"></param>
        public static void LoadControl(Control control)
        {
            iniFile = new IniFile(Path);
            ForeachControl(control);
            iniFile = null;
        }
        private static void ForeachControl(Control control)
        {
            foreach (Control ctl in control.Controls)
            {
                if (!ctl.HasChildren)
                {
                    if (ctl.Parent is System.Windows.Forms.NumericUpDown)
                    {
                        NumericUpDown tempNumberic = ctl.Parent as NumericUpDown;
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("NumericUpDown", tempNumberic.Name)))
                        {
                            tempNumberic.Value = Convert.ToDecimal(iniFile.IniReadValue("NumericUpDown", tempNumberic.Name));
                        }
                    }
                    else if (ctl is CheckBox)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("CheckBox", ctl.Name)))
                            ((CheckBox)ctl).Checked = Convert.ToBoolean(iniFile.IniReadValue("CheckBox", ctl.Name));
                    }
                    else if (ctl is RadioButton)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("RadioButton", ctl.Name)))
                            ((RadioButton)ctl).Checked = Convert.ToBoolean(iniFile.IniReadValue("RadioButton", ctl.Name));
                    }
                    else if (ctl is TextBox)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("TextBox", ctl.Name)))
                            ((TextBox)ctl).Text = iniFile.IniReadValue("TextBox", ctl.Name);
                    }
                    else if (ctl is ComboBox)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("ComboBox", ctl.Name)))
                            ((ComboBox)ctl).Text = iniFile.IniReadValue("ComboBox", ctl.Name);
                    }

                    else if (ctl is TrackBar)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("TrackBar", ctl.Name)))
                            ((TrackBar)ctl).Value = Convert.ToInt32(iniFile.IniReadValue("TrackBar", ctl.Name));
                    }
                    else if (ctl is CheckBoxX)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("CheckBoxX", ctl.Name)))
                            ((CheckBoxX)ctl).Checked = Convert.ToBoolean(iniFile.IniReadValue("CheckBoxX", ctl.Name));
                    }
                    else if (ctl is TextBoxX)
                    {
                        if (!string.IsNullOrEmpty(iniFile.IniReadValue("TextBoxX", ctl.Name)))
                            ((TextBoxX)ctl).Text = iniFile.IniReadValue("TextBoxX", ctl.Name);
                    }
                    else if (ctl is RichTextBox)
                    {
                        int i = 0;
                        while(!string.IsNullOrEmpty(iniFile.IniReadValue("RichTextBox=" + ctl.Name, "row"+i)))
                        {
                            ((RichTextBox)ctl).Text += iniFile.IniReadValue("RichTextBox=" + ctl.Name, "row"+i)+"\n";
                            i++;
                        }
                     
                       
                          
                    }
                }
                else
                {
                    ForeachControl(ctl);
                }
            }
        }
    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【转载】C#使用Trim方法去除字符串前后的所有空格发布时间:2022-07-10
下一篇:
C#中文和UNICODE编码转换发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap