Jonathan
Jonathan

Reputation: 99

saving Textbox values for next time I run an application

I have a form with 30 text boxes which contain default setting I set in my code. the user has an option to change it , but I want to know how can I save the new values chosen by the user in one (or more) of those text boxes for next time I run the application. thanks a lot

Upvotes: 3

Views: 6732

Answers (4)

apomene
apomene

Reputation: 14389

the app.config is an XML file with many predefined configuration sections available and support for custom configuration sections. An example where we have a tag for setting Name mySetting and mySetting2 is shown

<?xml version="1.0"?>
    <configuration>  
     <appSettings>    
    <add key="mySetting" value="myValue1"/>
    <add key="mySetting2" value="myValue12" />  
  </appSettings>
    </configuration>

In order to use config files you must add a reference to System.Configuration, so you can use ConfigurationManager class. Below is a sample for reading and writing to app.config file:

WRITE TO CONFIG From TEXTBOX:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["mySetting"].Value=textBox1.Text;
config.Save();

READ from Config:

 var mySetting = ConfigurationManager.AppSettings["mySetting"];

Upvotes: 2

Mong Zhu
Mong Zhu

Reputation: 23732

If you don't care about editing your saved file and don't intend to rename the textboxes or change the layout of them you can leave the ordering of values to the compiler. Use this for saving:

// first collect all values
List<string> allvalues = this.Controls.OfType<TextBox>().Select(x => x.Text).ToList();
// write to file
System.IO.File.WriteAllText("yourPath", String.Join(Environment.NewLine, allvalues));

and this for loading:

//load the values:
string [] alllines = System.IO.File.ReadAllLines("yourPath");

List<TextBox> allTextboxes = this.Controls.OfType<TextBox>().ToList();
for (int i = 0; i < allTextboxes.Count; i++)
{
    allTextboxes[i].Text = alllines[i];
}

Explanation:

this.Controls.OfType<TextBox>() gets you all controls that are textboxes in your windows form.

String.Join(Environment.NewLine, allvalues) combines all values to a string separated by a newline.

Upvotes: 2

MarcioAT
MarcioAT

Reputation: 60

Simple way to solve this problem:

//To write textbox values to file (may be used on form close)
using (System.IO.StreamWriter sw = new StreamWriter("values.txt"))
{
    foreach (var control in this.Controls)
    {
        if (control is TextBox)
        {
            TextBox txt = (TextBox)control;
            sw.WriteLine(txt.Name + ":" + txt.Text);
        }
    }
}


//To read textbox values from file (may be used on form load)
using (System.IO.StreamReader sr = new StreamReader("values.txt"))
{
    string line = "";
    while((line=sr.ReadLine()) != null)
    {
        //Ugly, but work in every case
        string control = line.Substring(0, line.IndexOf(':') );
        string val =  line.Substring(line.IndexOf(':') + 1);

        if(this.Controls[control] != null)
        {
            ((TextBox)this.Controls[control]).Text = val;
        }
    }
}

Upvotes: 1

Student
Student

Reputation: 1

Save values in file. For 30 text field you can use 30 lines or just comma separated values.

When user change the value then write new values in that file, so on next startup you will have new values in file and each time don't have to read old data.

Upvotes: 0

Related Questions