Justin8051
Justin8051

Reputation: 429

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. 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. 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. 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.

Upvotes: 0

Views: 91

Answers (1)

toni
toni

Reputation: 133

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

Upvotes: 0

Related Questions