简体   繁体   English

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

[英]How can I save and load textbox values, checkbox states, dropdown menu selections, etc., into a .txt file?

I am trying to implement a simple save-load function in my application, which would save the states of various GUI elements in my application (textboxes, checkboxes, dropdown menus, and so on) to a custom-named .txt file, and then load them back the next time user runs my application. 我正在尝试在应用程序中实现一个简单的保存加载功能,该功能会将应用程序中各种GUI元素(文本框,复选框,下拉菜单等)的状态保存到自定义名称的.txt文件中,然后下次用户运行我的应用程序时将它们加载回去。 I do not want to use My.Settings, because it is a portable application, and therefore the settings file has to be next to the executable. 我不想使用My.Settings,因为它是可移植的应用程序,因此设置文件必须在可执行文件旁边。 Also because my application is an editor, and the settings have to be bound by name to the current file the user is working with. 同样因为我的应用程序是编辑器,并且设置必须按名称绑定到用户正在使用的当前文件。 Write permissions is not an issue. 写入权限不是问题。 I want to code this in a way so that I would only have to write down the names of the GUI elements to be mentioned once in my code, preferably in a list. 我想以某种方式进行编码,这样我只需要写下要在我的代码中(最好是在列表中)提到的GUI元素的名称。 Like this (pseudo-code): 像这样(伪代码):

    '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")

I hope this conveys what I'm trying to achieve. 我希望这能传达我正在努力实现的目标。 Can anyone give any advice on how to make this work, please? 任何人都可以就如何进行这项工作提供任何建议吗?

EDIT: I forgot to mention. 编辑:我忘了提。 It would be very nice if each value in the .txt file would be saved with a key that refers to a specific element, so that if I add more GUI elements in my application in the future, or re-arrange them, this save-load sub would always choose the correct value from the .txt file for a specific element. 如果将.txt文件中的每个值都保存有一个指向特定元素的键,那将非常好,这样,如果将来在我的应用程序中添加更多GUI元素或重新排列它们时,此保存- load sub始终会从.txt文件中为特定元素选择正确的值。

   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;
                    }
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使GUI中的对象(文本字段等)消失? - How do I make objects (textfields etc.) in the GUI disappear? 如何告诉用户工具提示位于文本/文本框/图像等后面? - How to tell Users that a Tooltip lies behind a Text/Textbox/Image, etc.? 如何在 Java 中将 JTextField 输入保存到 .txt 文件? - How Do I Save JTextField Inputs to a .txt file in Java? 如何更改:数组中的值并在MATLAB GUI中保存函数的状态 - How to change: values in an array and save states of a function in a MATLAB GUI 如何将文本从文本框中插入文本到autohotkey中? - How can I insert text from a textbox to a file in autohotkey? 如何根据文件路径保存文件? - how can i save the file based on file path? 要使我的raspberry pi托管一个带有拨号盘的网页,该拨号盘指示CPU温度,CPU负载等实时信息,我该怎么办? - What do I need to do to have my raspberry pi host a webpage with dials indicating CPU temp, CPU load, etc., in real time? 尝试从txt文件填充文本框和列表视图 - Trying to populate textbox and listview from txt file 在单独的GUI类(菜单,工具栏等)之间共享操作的最佳方法是什么? - What's the best way to share actions between separate GUI classes (menu, toolbars, etc.) 如何从 ROBLOX 的开始菜单 GUI 加载我的工作区? - How can I load my workspace from my start menu GUI in ROBLOX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM