记忆用户设置-提升程序的体验VB/C#

有时候,设计的程序有很多的控件,甚至多达近百个,尤其是一些工控软件等,程序运行所需的各种参数都是由用户通过这些控件设置而来,那么记录用户的设置就显得十分必要。如果程序出现异常,起码重新打开可以不用再一个一个的去设置。在调试的过程中也方便了很多。
而很多人的代码是类似这样:
config.write("id",txtID.text) ;
config.write("pw",txtPW.text) ;
... ...
一个一个的输入,这样显得非常繁琐,下面提供了VB和C#两种方法来简化这一过程
Private Sub SaveConfig(frm As Form)
    Dim objX As Object
    For Each objX In frm.Controls
        If TypeOf objX Is TextBox Then
            Config.cWrite objX.Name, objX.Text
        ElseIf TypeOf objX Is CheckBox Then
            Config.cWrite objX.Name, objX.Value
        End If
    Next
End Sub

Private Sub ReadConfig(frm As Form)
    Dim objX As Object
    Dim s As String
    For Each objX In frm.Controls
        If TypeOf objX Is TextBox Then
            s = Config.cRead(objX.Name)
            If Len(s) > 0 Then objX.Text = s
        ElseIf TypeOf objX Is CheckBox Then
            s = Config.cRead(objX.Name)
            If Len(s) > 0 Then objX.Value = CLng(s)
        End If
    Next
End Sub
如果用上面的函数就显得非常方便了,即使以后添加了控件也不需要另外写代码
其中Config对象是一个封装的操作配置文件(*.ini)的类,由于这个类不在本篇讨论范围,所以不贴了,反正也简单,百度一下
如果只想针对某个frame或picturebox容器内控件,那么可以用对象的Container属性
If objX.Container Is Picture1 Then
... ...'这样就可以筛选出在Picture1下的所有控件
end if
当程序启动的时候,先读取配置到控件
当用户确定设置的时候,就记录一下,很简单方便
===============================================================================
===============================================================================
C#中也可以这么写:
//把相应控件的值写入到config.ini配置文件中
void ReadConfig(Control item)
{
    foreach (Control ctl in item.Controls)
    {
        string s = ctl.GetType().ToString();
        string[] sp = s.Split('.');
        switch (sp[sp.Length - 1])
        {
            case "TextBox":
                TextBox tb = ctl as TextBox;
                tb.Text = INI.IniReadValue("配置", ctl.Name);
                break;
            case "RadioButton":
                if (INI.IniReadValue("配置", ctl.Name) == "True")
                {
                    RadioButton rb = ctl as RadioButton;
                    rb.Checked = true;
                }
                break;
            case "ComboBox":
                ComboBox cb = ctl as ComboBox;
                if (cb.Items.Count > 0)
                {
                    s = INI.IniReadValue("配置", ctl.Name);
                    if (IsNumber(s) == true)
                    {
                        int i = Convert.ToInt32(s);
                        if ( i>= 0 && i < cb.Items.Count)
                            cb.SelectedIndex = i;
                    }
                }
                break;
            case "ListBox":
                ListBox lst= ctl as ListBox;
                if (lst.Items.Count > 0)
                {
                    s = INI.IniReadValue("配置", ctl.Name);
                    if (IsNumber(s) == true)
                    {
                        int i = Convert.ToInt32(s);
                        if (i >= 0 && i < lst.Items.Count)
                            lst.SelectedIndex = i;
                    }
                }
                break;
            case "CheckBox":
                CheckBox ck = ctl as CheckBox;
                if (INI.IniReadValue("配置", ctl.Name) == "True")
                    ck.Checked = true;
                else
                    ck.Checked = false;
                break;
        }
        if (ctl.HasChildren) //如果控件有子控件,就递归
            ReadConfig(ctl);
    }
}
//把相应控件的值写入到config.ini配置文件中
void SaveConfig(Control item)
{
    foreach (Control ctl in item.Controls)
    {
        string s = ctl.GetType().ToString();
        string[] sp = s.Split('.');
        switch (sp[sp.Length - 1])
        {
            case "TextBox":
                TextBox tb = ctl as TextBox;
                if(tb.Multiline==false ) //不是多行文本框就写入
                    INI.IniWriteValue("配置", ctl.Name, tb.Text);
                break;
            case "RadioButton":
                RadioButton rb = ctl as RadioButton;
                INI.IniWriteValue("配置", ctl.Name, rb.Checked.ToString ());
                break;
            case "ComboBox":
                ComboBox cb = ctl as ComboBox;
                if (cb.Items.Count > 0)
                    INI.IniWriteValue("配置", ctl.Name, cb.SelectedIndex.ToString());
                break;
            case "ListBox":
                ListBox lst = ctl as ListBox;
                if (lst.Items.Count > 0)
                    INI.IniWriteValue("配置", ctl.Name, lst.SelectedIndex.ToString());
                break;
            case "CheckBox":
                CheckBox ck = ctl as CheckBox;
                INI.IniWriteValue("配置", ctl.Name, ck.Checked.ToString ());
                break;
        }
        if (ctl.HasChildren) //如果控件有子控件,就递归
            SaveConfig(ctl);
    }
}
其中INI是封装的操作配置文件(*.ini)的类,如果只想要操作某一个frame、picture或tabControl容器内的所有控件,传值该控件对象即可。

上一篇:locality


下一篇:python3+dlib人脸识别及情绪分析