Saving default value for C # text field

I am creating a little calculator type program.

I have a text box that the user can change the value. This text box is used in some calculations.

How to save the value of a text field so that the next time you open the form, the text field holds this value.

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

Any help would be appreciated Im new on this

+4
source share
8 answers

Most likely, you do not have an official data source, such as a database. For situations like this, use the .NET application settings to save and restore application settings between runs.

+7
source

You can store data in a data source, for example.

  • Text file
  • XML file
  • Database

in the Form_Load method, you can return this value and set it to a text field. Suppose you write it in a text file, the code will be:

StreamReader re = File.OpenText("MyData.txt"); string input = null; input = re.ReadLine(); txtFormData.Text = input; re.close; 
0
source

The best way to do this is to bind the text field to the data source. A data source can be any thing according to your requirements. for example if you prefer xml file

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

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

here's how to bind your text box 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; 
0
source

And write the final entry back to the data source

  • in a FormClosed event handler or
  • an exception handler that will cause the form to close.
0
source

I'm not sure what you need, but when you define a TextBox (via the constructor), you can set its Text property to any value. By default it is empty , but it can be 1.5 if you want it. If the user opens the program again, it will be 1.5, not the last. The same would be true if you close the form and reopen it. There is no need to store the value anywhere else, as it is a global value that you always want to use.

Optionally, you can add something like this to MainForm_Load() : myTextBox.Text = '1.5';

Both solutions will only work if it is a constant value that you do not want to change ever (otherwise you will have to redistribute the application.

0
source
 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 that I use when I encounter a similar problem of what you have. (I can't check it now, but you should be able to see what the code should do :))

0
source

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

0
source

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

Here is a small example:

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

Source: https://habr.com/ru/post/1302670/


All Articles