views:

564

answers:

8

I'm creating a bit of a calculator type program.

I have a textbox which the user can change the value of. This textbox is used in some of the calculations.

How do i save the value of a textbox so that the next time the form opens the text box holds that value.

For example the textbox default is 1.5. The user changes it to 5 and then closes the form. The next time the user opens the form the textbox default is now 5 not 1.5

Any help would be appreciated Im new at this

A: 

You can store the data in a data source, e.g. :

  1. Text File
  2. XML File
  3. Database

in the Form_Load method, you can retreive that value and set it in the text box. Suppose, you write it in a text file, the code would be:

StreamReader re = File.OpenText("MyData.txt");
string input = null;
input = re.ReadLine();
txtFormData.Text = input;
re.close;
Bhaskar
where do i put this is it in the form load or where the user changes the value?
Christina
Put it in the form load. Also on close of your application, you owuld need to write the value of the text box in a text file. http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=132.
Bhaskar
A: 

A better way to do this is to bind your text Box to a dataSource. A datasource could be any thing as per your requirements. for e.g, if you prefer an xml file

here is how it works using sample xml file, values.xml

<Values>
<default>1.5</default>
.
.
</Values>

here is how to bind your textbox to the default value

  string xml = @"<Values><default>1.5</default></Values>";

   XElement x = XElement.Parse(xml);
   var defaultValue = from d in x.Elements("default")
                      select d.Element("default").Value;

   TextBox1.Text = defaultValue;
Asad Butt
A: 

And write the final input back to data source

  1. in FormClosed event handler or
  2. exception handler that will force the form to be closed.
Ricky
+7  A: 

Chances are, you do not have a formal data source, such as a back-end database. For situations like this, use the .NET application settings architecture to save and restore application settings between runs.

Matt Davis
you're right I don't. I dont quite understand the msdn explanation though. Any further help? :-S Please id be sooo greatful
Christina
tried it and visual studio doesnt like it has a problem with the get and set ?
Christina
@Christina: What problem? I've used this approach several times, so it does work. Since I've never experienced a problem, unless you explain what problem you are having, no one will be able to help.
recursive
@Christina: If you use the settings architecture, changes may affect all users, doesn't sound like that's what you want.
Andrew Neelands
@Christina: See if this link helps you - http://msdn.microsoft.com/en-us/library/ms171565.aspx. It's more of a cookbook approach than the link above, which simply provides an overview.
Matt Davis
A: 

I am not sure if that's what you need but when you define TextBox (through designer) you can set it's Text property to any value. By default it's empty but it can be 1.5 if you wish so. If user will open program again it will be 1.5 and not the last one used. Same would be true when you close the form and open it up again. There's no need to save the value anywhere else since it's global value that you always want to use.

Optionally you could add in MainForm_Load() something like this: myTextBox.Text = '1.5';

Both solutions will work only if it's constant value that you don't want to change ever (otherwise you will have to redeploy application.

MadBoy
A: 
public partial class Form1
{
    public string defaultValue;

    private void form2Open_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2();
        if (defaultValue != null)
            f.textBox1.Text = defaultValue;
        f.mainForm = this;
        f.Show();
    }
}

public partial class Form2
{
    public Form1 mainForm;
    private void Form2_Closing(object sender, DontRememberWhatItsCalledEventArgs e)
    {
        mainForm.defaultValue = textBox1.Text;
    }
}

This is the method I use when I encounter a similar problem of the one your having. (I can't test it now, but you should be able to see what the code is supposed to do :))

Phoexo
A: 

public partial class Form1 { public string defaultValue;

private void form2Open_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    if (defaultValue != null)
        f.textBox1.Text = defaultValue;
    f.mainForm = this;
    f.Show();
}

}

public partial class Form2 { public Form1 mainForm; private void Form2_Closing(object sender, DontRememberWhatItsCalledEventArgs e) { mainForm.defaultValue = textBox1.Text; } }

pravesh
A: 

Another approach would be to use the Application.UserAppDataRegistry Property.

Here is a little example:

private void LoadSettings()
{
    textBoxOutput.Text = (String)Application.UserAppDataRegistry.GetValue("SomeName", String.Empty);
}

private void SaveSettings()
{
    Application.UserAppDataRegistry.SetValue("SomeName", textBoxOutput.Text);
}
Oliver