GeekSmack Community: [C#] Save contents of multiple richTextBox's and checkBoxes - GeekSmack Community

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[C#] Save contents of multiple richTextBox's and checkBoxes In a specific spot... for a To-Do list!

#1 User is offline   WizardCM 

  • Tech Rules!
  • PipPipPipPipPip
  • Group: Member
  • Posts: 1,004
  • Joined: 21-August 08
  • Gender:Male
  • Location:Melbourne, AUS
  • Interests:Technology, Computers and Windows 7!

Posted 13 March 2010 - 04:09 AM

Posted Image

The above shows you what I already have.
All the boxes work, and the "New List" button clears them. The main problem I'm facing right now:

Basically, when you Load/Save a list, it only uses the first richTextBox while ignoring the rest of the form. When you load a list it shows this, and when you save a list it shows this.
What I want them to do is save a text document containing the filled out boxes (etc) in a place like %userfolder%\AppData\To-Do List\current.txt
Currently it can be saved under any name in any place.
But mainly I want it to read the contents of all the boxes, then save that information in a text document.

Eg. Contents of the list / Contents of the text document

Posted Image Posted Image

Meaning: Put a * if the box is checked, leave a space, and add the contents of the box, while putting an _ if the box is not checked, and the rest of the line blank if the richTextBox next to it is blank.

Any ideas? Sorry if I didn't explain it well. :/
Posted Image
Posted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted Image
0

#2 User is offline   cheesepenguins 

  • Member
  • Pip
  • Group: Member
  • Posts: 43
  • Joined: 11-March 10

Posted 13 March 2010 - 11:41 AM

View PostWizardCM, on 13 March 2010 - 04:09 AM, said:

~snipz0rz~
Any ideas? Sorry if I didn't explain it well. :/

I've never used C# before, but it sounds like you are struggling with concatenation and then reading the file at specified points. Why not try instead to use a comma separated list in the text file so as to avoid multiple lines, which may be your issue.

As for the _ or * before each item that should be simple enough in the form of a basic if statement, if box checked, string 1 = "*"+string1 etc. Obviously in the relevant markup...
Posted Image
0

#3 User is offline   Lander 

  • Member
  • Pip
  • Group: Member
  • Posts: 10
  • Joined: 17-May 09

Posted 13 March 2010 - 10:57 PM

What I would do if I were you, is create a new local string, and do this:
(the string STRING = what goes in the text box or whatever)


string Checked = STRING.Substring(0, 1);
if (Checked.Contains('_'))
{
checkbox.Checked = false;
}
else
{
checkbox.Checked = true;
}


Basically all that does is it reads the first character of the string, and then checks to see if it should be checked or not. I would also make a string array to make things easier as well.
Posted Image
0

#4 User is offline   WizardCM 

  • Tech Rules!
  • PipPipPipPipPip
  • Group: Member
  • Posts: 1,004
  • Joined: 21-August 08
  • Gender:Male
  • Location:Melbourne, AUS
  • Interests:Technology, Computers and Windows 7!

Posted 13 March 2010 - 11:09 PM

View Postcheesepenguins, on 13 March 2010 - 11:41 AM, said:

View PostWizardCM, on 13 March 2010 - 04:09 AM, said:

~snipz0rz~
Any ideas? Sorry if I didn't explain it well. :/

I've never used C# before, but it sounds like you are struggling with concatenation and then reading the file at specified points. Why not try instead to use a comma separated list in the text file so as to avoid multiple lines, which may be your issue.

As for the _ or * before each item that should be simple enough in the form of a basic if statement, if box checked, string 1 = "*"+string1 etc. Obviously in the relevant markup...

Well so far I've only figured out how to code it to receive and send information with 1 richTextBox, so it's not an issue so far. :P

View PostLander, on 13 March 2010 - 10:57 PM, said:

What I would do if I were you, is create a new local string, and do this:
(the string STRING = what goes in the text box or whatever)


string Checked = STRING.Substring(0, 1);
if (Checked.Contains('_'))
{
checkbox.Checked = false;
}
else
{
checkbox.Checked = true;
}


Basically all that does is it reads the first character of the string, and then checks to see if it should be checked or not. I would also make a string array to make things easier as well.

Well then, time for me to teach myself stuff about strings. :P
Posted Image
Posted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted Image
0

#5 User is offline   Lander 

  • Member
  • Pip
  • Group: Member
  • Posts: 10
  • Joined: 17-May 09

Posted 14 March 2010 - 01:24 AM

Well, if I were you, I'd write the text as one string in the text file vs. one large line, then a character between each textbox's string that would be your splitting character to create an array.

For this example, the text within the file would be using | as a separator, the number after the separator shows the textbox/checkbox ID, and everything else you used.

Quote

_1 Clean Room|_2 Buy New Power Board|*3 Start Learning C#



So it'd look like this:

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


Your resulting code would look something like that.

In case you don't know what a switch is (I don't mean this in an offensive way at all, but based off of your last post & what's in your text document it doesn't seem as though you're too experienced with C#), it's basically like an easier way of writing a bunch of if() statements.

In the first switch statement, we don't check if it's "_1" or anything like that, because assuming the checkboxes are by default unchecked, there's no need to uncheck something that's already unchecked.
Posted Image
0

#6 User is offline   WizardCM 

  • Tech Rules!
  • PipPipPipPipPip
  • Group: Member
  • Posts: 1,004
  • Joined: 21-August 08
  • Gender:Male
  • Location:Melbourne, AUS
  • Interests:Technology, Computers and Windows 7!

Posted 14 March 2010 - 01:29 AM

View PostLander, on 14 March 2010 - 01:24 AM, said:

~snip
//Split the string
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);
}
}

~snip

Alright, so I think I'm starting to understand. Just one thing: where does the above code go?

And yes, you are right. I've been teaching myself C# for almost a week. :lol:
Posted Image
Posted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted ImagePosted Image
0

#7 User is offline   Lander 

  • Member
  • Pip
  • Group: Member
  • Posts: 10
  • Joined: 17-May 09

Posted 14 March 2010 - 02:02 AM

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.
Posted Image
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


This page is sponsored by TopHostingCenter.com, a web hosting provider that specializes in cloud hosting.

As well as Hosted Exchange