The above code goes in to where ever your loading function is
You can create a new void for it, or simply put it on the button click, but if you're going to do that, then let me fix it to make it complete:
(in this situation, OFD1 = Open File Dialog 1 - the one you open your file with)
private void LoadFile(string filepath)
{
//Load File
string STRING = System.IO.File.ReadAllText(filepath);
//Code we did earlier...
//Split the string in to an array
string[] split = STRING.Split('|');
//Create a loop that uses our array
foreach (string Checked in split)
{
//create a new local string so we can get whether or not the checkbox is checked
string Substring = Checked.Substring(0//starting point//, 2//read first two characters//);
//Create another new local string for the textbox
string WhichTextbox = Checked.Substring(1//skip the first character//, 1//read the next character (textbox ID)//);
//Determine whether the box should be checked or not, and which text box does this go to?
switch (Substring)
{
case "*1":
checkbox1.Checked = true;
break;
case "*2":
checkbox2.Checked = true;
break;
case "*3":
checkbox3.Checked = true;
break;
}
//Now put the text in to the proper textbox
switch (WhichTextbox)
{
case "1":
TextBox1.Text = Checked.Substring(2//we want to skip the first two characters since those aren't a part of what goes in the textbox//, Checked.Length - 2 //for the length, we take the length of the string, and subtract 2 from it since we skipped over 2 characters//);
break;
case "2":
TextBox2.Text = Checked.Substring(2, Checked.Length - 2);
break;
case "3":
TextBox3.Text = Checked.Substring(2, Checked.Length - 2);
break;
}
}
}
On the button that opens the file, you'd write:
//We want to make sure the user selects a file, and then presses OK
DialogResult result = OFD1.ShowDialog();
if (result == DialogResult.OK)
{
LoadFile(OFD1.FileName);
}
And voila.
The reason why we write "LoadFile(
OFD1.FileName);" is because in the void, we denoted that the file path could be a variable (so if you're using something besides OFD1, or something among those lines), so the "OFD1.FileName" (which is also a string) fills in the place of the string "filepath" in the void.
If you have any questions at all, feel free to ask.
Edit:
PS:
There may be some errors with some capitalization or something like that with the code if you do a direct copy/paste, since I wrote all of that code directly in this reply box vs. in Visual Studio (which also explains why everything doesn't look all nice and neat and organized)
Double Edit:
To add on to this, you may also want to save the file with a file extension like .txtt or something so that you can ensure that it belongs with your program, and if you want to incorporate error handling, then on the button-click event that loads the file, you'd write:
//We want to make sure the user selects a file, and then presses OK
DialogResult result = OFD1.ShowDialog();
if (result == DialogResult.OK)
{
try
{
LoadFile(OFD1.FileName);
}
catch
{
MessageBox.Show("There was an error in trying to load the file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
And edit number three...
I completely misread the OP.
I thought you were trying to LOAD the file, not save it.
Yeah, to save, what you'd do is this:
private void Save(string savePath)
{
string 1 = "";
string 2 = "";
string 3 = ""t;
string Master = "";
if (checkbox1.Checked == true)
{
1 = "*1 " + TextBox1.Text;
}
else
{
1 = "|_1 " + TextBox1.Text;
}
if (checkbox2.Checked == true)
{
2 = "|*2 " + TextBox2.Text;
}
else
{
2 = "|_2 " + TextBox2.Text;
}
if (checkbox3.Checked == true)
{
3 = "|*3 " + TextBox3.Text;
}
else
{
3 = "|_3" + TextBox3.Text;
}
Master = 1 + 2 + 3;
System.IO.File.WriteAllText(savePath, Master);
}
Then, on the save button, write:
(This context, SFD1 = SaveFileDialog1)
SFD1.ShowDialog();
DialogResult result = SFD1.ShowDialog();
if (result == DialogResult.OK)
{
try
{
Save(SFD1.FileName);
}
catch
{
MessageBox.Show("A problem occurred when trying to save the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Of course, you'll have to add on for all of the other textboxes and checkboxes, but that will get you set.