0

I am writing a program (My first program...) that contains a form that saves textbox info, checkbox, and radio button selections off to a text file when the save button is clicked. This part works.

Then when the form opens (the next time you run the program) it collects all these selections from the text file and saves them as variables in the form. This part works.

I can not find a way to auto-populate these settings back into the form when it opens.

Example:

textBox1 should show the contents of "string userName" when the form opens.

chkBox1 should be checked, or not, depending on the value of "Int chkBox1."

The same for radio buttons.

I am reading the settings file one line at a time and have confirmed that the variable have the information, I just can't get it to show on the newly opened form. Is there a simple way to do this? Thank you...

Sean Connelly
  • 135
  • 3
  • 13
  • You need to show your code for a reasonable amount of help with this. Show the code for your form being initialised. – Derek Jun 05 '15 at 16:24
  • Can't you just set the appropriate property in the properties for the control while working in the designer? – Jeff B Jun 05 '15 at 18:16

3 Answers3

1

usually when it come to store an user interface setting the standard way is to use XML file to save or load the setting i made this example that save user interface components using xml hope it be useful

https://www.dropbox.com/s/1j1qbe7udqxizr6/4.XMLConfigurationEditor.rar?dl=0

0

Yes there is. You need to store component (textbox, checkbox etc.) name into file also. Your saved fill will look some thing like chkBox1, true chkBox2, false textBox1, "Some cool value".

Then when you read those values, you search for correct control and set it value. You can access form controls by calling Controls property: For example you can create Loaded event or access control in constructor after InitializeComponents method call. Control are accessed like this: ((CheckBox)this.Controls["chkBox1"]).Checked = true;

Now just combine this control usage into your file reading code.

Btw. You can also store values into application settings. This is easier way to store this kind of information. See this link

Community
  • 1
  • 1
Panu Oksala
  • 3,245
  • 1
  • 18
  • 28
0

you can try loading the values from the file before initializing the components. Later, you just pass the values with its respective cast. I also suggest to use a try/catch block to prevent possible errors during the conversions.

Something like:

string _TextBoxData = "No data";
bool _ChkBox1Data = false;
...
try{
  System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
  _TextBoxData = file.ReadLine();
  _ChkBox1Data = Conver.ToBoolean(file.ReadLine());
}catch(Exception e){
  MessageBox.Show(ex.ToString());
}
...
private void Form1_Load(){

  TextBox1.Text = TextBoxData;
  CheckBox1.Checked = ChkBox1Data;

}