如何将文本框值,复选框状态,下拉菜单选择等保存并加载到.txt文件中?

时间:2018-12-29 15:16:47

标签: user-interface configuration save state-saving

我正在尝试在应用程序中实现一个简单的保存加载功能,该功能会将应用程序中各种GUI元素(文本框,复选框,下拉菜单等)的状态保存到自定义的.txt文件中,然后在用户下次运行我的应用程序时将其重新加载。我不想使用My.Settings,因为它是可移植的应用程序,因此设置文件必须在可执行文件旁边。同样因为我的应用程序是编辑器,并且设置必须按名称绑定到用户正在使用的当前文件。 写入权限不是问题。我想以某种方式进行编码,这样我只需要写下要在我的代码中(最好是在列表中)提到的GUI元素的名称。像这样(伪代码):

    'list
    Dim ElementsToSave() as Object = {
    Textbox1.text,
    Checkbox1.Checked,
    DropDownMenu1.SelectedItem,
    .....
    }

    'save-load sub
    Sub SaveLoad(Elements as Object, mode as string)
       If mode = "save" then
          For each Element in Elements
             ??? 'save each element state to .txt file
          Next
       If mode = "load" then
          For each Element in Elements
             ??? 'load each element state from .txt file
          Next
       End if
   End sub

   'call
   SaveLoad(ElementsToSave, "save")
   'or
   SaveLoad(ElementsToSave, "load")

我希望这能传达我正在努力实现的目标。任何人都可以就如何进行这项工作提供任何建议吗?

编辑:我忘了提。如果将.txt文件中的每个值都保存有一个指向特定元素的键,那将非常好,这样,如果将来在我的应用程序中添加更多GUI元素或重新排列它们时,此保存- load sub始终会从.txt文件中为特定元素选择正确的值。

1 个答案:

答案 0 :(得分:0)

   using System.IO;
   ...

    private enum ControlProperty
    {
        None = 0,
        Text = 1,
        Checked = 2,
        SelectedValue = 3
    }

    private string GetSettingsFile()
    {
        FileInfo fi = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
        string path = Path.Combine(fi.Directory.FullName, "settings.txt");
        return path;
    }

    private void test()
    {
        SaveSettings();
        LoadSettings();
    }

    private void SaveSettings()
    {
        object[] vals = new object[] { this.Textbox1, ControlProperty.Text, this.Textbox1.Text, this.Checkbox1, ControlProperty.Checked, this.Checkbox1.Checked, this.Menu1, ControlProperty.SelectedValue, this.Menu1.SelectedValue };
        string txt = "";
        for (int i = 0; i < vals.Length; i += 3)
        {
            string controlID = (vals[i] as Control).ID;
            ControlProperty property = (ControlProperty)vals[i + 1];
            object state = vals[i + 2];
            txt += controlID + ":" + property.ToString() + ":" + state.ToString() + "\n";
        }
        string file = GetSettingsFile();
        File.WriteAllText(file, txt);
    }

    private void LoadSettings()
    {
        string file = GetSettingsFile();
        string[] lines = File.ReadAllLines(file);
        foreach (string s in lines)
        {
            string[] parts = s.Split(':');
            if (parts.Length < 3) continue;
            string id = parts[0];
            var c = this.form1.FindControl(id);
            ControlProperty prop = ControlProperty.None;
            Enum.TryParse<ControlProperty>(parts[1], out prop);
            string state = parts[2];
            if (c is TextBox && prop == ControlProperty.Text)
            {
                TextBox t = c as TextBox;
                t.Text = state;
            }
            else if (c is CheckBox && prop == ControlProperty.Checked)
            {
                CheckBox chk = c as CheckBox;
                chk.Checked = state == "True";
            }
            else if (c is Menu && prop == ControlProperty.SelectedValue)
            {
                Menu m = c as Menu;
                foreach (MenuItem menuItem in m.Items)
                {
                    if (menuItem.Value == state)
                    {
                        menuItem.Selected = true;
                    }
                }
            }
        }
    }