I created an application that askes several questions. One of the questions is
Installations instructions. So the user of my application will type installation instructions into a textbox and that info will be written to a textfile using streamwriter(). My problem is that on the form, the text wordwraps to fit the textbox fine, but when you open up the text file in notepade, you get one really long line of text. I need a way to get my text to wordwrap in notepad also. I am aware of the fact that notepad has a wordwrap option, but i need to program this into my code. I have no idea how to do that. this is a sample of the code i am using
sw.WriteLine(
"***********************************************************************");sw.WriteLine(
"* *");sw.WriteLine(
"* Installation Instructions *");sw.WriteLine(
"* *");sw.WriteLine(
"***********************************************************************");sw.WriteLine(text8); //this needs to be word wrapped !
sw.WriteLine();
DavePosted Jan 21, 2008, 4:22 AM
private String GetWrappedText(String txtSomeTextBox.Text)
{
StringBuilder sb = new StringBuilder();
String theText = txtSomeTextBox.Text;
String tmpLine = "";
int start = 0;
int idxLastSpace;
int offset = 0;
for (int i = 0; i < theText.Length; i++)
{
if (i % 80 == 0 && i != 0)
{
tmpLine = theText.Substring(start, 80);
idxLastSpace = tmpLine.LastIndexOf(' ');
sb.Append(tmpLine, 0, idxLastSpace);
sb.Append("\n");
offset = tmpLine.Length - 1 - idxLastSpace;
sb.Append(tmpLine, idxLastSpace + 1, offset);
start = i;
}
}
tmpLine = theText.Substring(start);
sb.Append(tmpLine);
}
You can then use the StreamWriter with that method as follows:
See how that goes.
jamiePosted Jan 20, 2008, 10:54 PM
OK, i gave it a try, but i cannot figure out how to integrate it with my code. Here is the relevant code. textbox 8, 9, 10 and 11 are the ones that need to word wrap
text8 = textBox8.Text;
text9 = textBox9.Text;
text10 = textBox10.Text;
text11 = textBox11.Text;
if (textBox8.Text.Equals(""))sw.Close();
else{
sw.WriteLine(
"***********************************************************************");sw.WriteLine(
"* *");sw.WriteLine(
"* Installation Instructions *");sw.WriteLine(
"* *");sw.WriteLine(
"***********************************************************************");sw.WriteLine(text8 +
"\r\n");sw.WriteLine();
}
if (text9.Equals(""))sw.Close();
else{
sw.WriteLine();
sw.WriteLine(
"***********************************************************************");sw.WriteLine(
"* *");sw.WriteLine(
"* Custom Content *");sw.WriteLine(
"* *");sw.WriteLine(
"***********************************************************************");sw.WriteLine();
sw.WriteLine(text9);
}
if (text10.Equals(""))sw.Close();
else{
sw.WriteLine();
sw.WriteLine(
"***********************************************************************");sw.WriteLine(
"* *");sw.WriteLine(
"* Special Thanks *");sw.WriteLine(
"* *");sw.WriteLine(
"***********************************************************************");sw.WriteLine();
sw.WriteLine(text10);
sw.WriteLine();
}
if (text11.Equals(""))sw.Close();
else{
sw.WriteLine();
sw.WriteLine(
"***********************************************************************");sw.WriteLine(
"* *");sw.WriteLine(
"* Additional Notes *");sw.WriteLine(
"* *");sw.WriteLine(
"***********************************************************************");sw.WriteLine();
sw.WriteLine(text11);
}
MessageBox.Show("The ReadMe file has been generated", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);sw.Close();
}
}
jamiePosted Jan 20, 2008, 8:54 PM
DavePosted Jan 20, 2008, 8:46 PM
I had a bit of time, so I refined my suggestion to get it working properly. Try plugging in the following code (adapting it appropriately for your situation). This is based on a line-length of 80, but you can change that easily.
StringBuilder sb = new StringBuilder();
String theText = textBox1.Text;
String tmpLine = "";
int start = 0;
int idxLastSpace;
int offset = 0;
for (int i = 0; i < theText.Length; i++)
{
if (i % 80 == 0 && i != 0)
{
tmpLine = theText.Substring(start, 80);
idxLastSpace = tmpLine.LastIndexOf(' ');
sb.Append(tmpLine, 0, idxLastSpace);
sb.Append("\n");
offset = tmpLine.Length - 1 - idxLastSpace;
sb.Append(tmpLine, idxLastSpace + 1, offset);
start = i;
}
}
tmpLine = theText.Substring(start);
sb.Append(tmpLine);
You could then write sb to file or whatever you need to do with it.
Guest UserPosted Jan 20, 2008, 6:12 PM
Dave's suggestion would work, by inserting newline characters at predefined intervals. I would only add to that by reducing the interval as I explained above.
jamiePosted Jan 20, 2008, 12:37 PM
Guest UserPosted Jan 20, 2008, 12:36 PM
jamiePosted Jan 20, 2008, 11:37 AM
DavePosted Jan 20, 2008, 7:41 AM
If so, there is a Property of TextBoxes called 'acceptsreturn'. Set that to true and any returns will be included in the TextBox text.
If not, you should be able to manipulate the string entered into the textbox. You could use the StringBuilder class to assist in that. If each line is to have 80 characters, something like:
That's untested and off the top of my head.