I want to format my text in a special way. I have a method that logs text in a TextBox control.
It simply logs text to the output, nothing special. However, I want it to be like so, so it formats the text in a special way.
Info: This is a test. Blah-blahl-blah-blah reaching the end of the line.
Here's the rest of the sentence.
As you can see, thet text reached the end of the line, and then went down and was formatted beneath the text before. The "Info : " is just a label, so I need help making a method that writes text with a label (in this case the label was "Info"). How can I do that? I'm having trouble creating such thing.
In my console application, I had something similar, but I'm not sure on how to do it in WinForms.EDIT: I converted it to WinForms.
private const byte LabelWidth = 11;
public static string Margin
{
get
{
return new string(' ', Main.LabelWidth);
}
}
private void Write(string label, string text, params object[] args)
{
if (this.InvokeRequired)
{
this.Invoke(this._logDelegate, text, args);
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append(' ', Main.LabelWidth - label.Length - 3);
sb.Append("[");
sb.Append(label);
sb.Append("]");
sb.Append(" ");
label = sb.ToString();
text = string.Format(text, args);
this.LogTextBox.AppendText(label);
bool first = true;
foreach (string s in text.Split('\n'))
{
string[] lines = new string[(int)Math.Ceiling((float)s.Length / (float)(this.LogTextBox.Size.Width - 11))];
for (int i = 0; i < lines.Length; i++)
{
if (i == lines.Length - 1)
{
lines[i] = s.Substring((this.LogTextBox.Size.Width - Main.LabelWidth) * i);
}
else
{
lines[i] = s.Substring((this.LogTextBox.Size.Width - Main.LabelWidth) * i, (this.LogTextBox.Size.Width - Main.LabelWidth));
}
}
foreach (string line in lines)
{
if (!first)
{
this.LogTextBox.AppendText(Main.Margin);
}
if ((line.Length + Main.LabelWidth) < this.LogTextBox.Size.Width)
{
this.LogTextBox.AppendText(line);
}
else if ((line.Length + Main.LabelWidth) == this.LogTextBox.Size.Width)
{
this.LogTextBox.AppendText(line);
}
first = false;
}
}
}
}
**EDIT: ** I put up the WinForms version, however the text doesn't align beneath the line before like shown inthe example.