0

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.

user3171969
  • 845
  • 1
  • 6
  • 4
  • Console? Why do you use console in WinForms application? – Sergey Berezovskiy May 15 '14 at 07:24
  • First you need a font with fixed width like Consolas. Then you'll need to know how many characters fit on your line and how large the labels will be.. – TaW May 15 '14 at 07:25
  • You can reuse this piece of code, just build a string inside it instead of writing it out to the console. (But you should use a monospaced font to achieve exactly what you want.) And for multiple colors you must use RichTextBox, unfortunately. – qqbenq May 15 '14 at 07:26
  • @qqbenq: you could also use a Webbrowser control which contains a table with 2 columns (do it with CSS if you like), one for the type and one for the text. It would even break the text at the end of the line automatically. – Thomas Weller May 15 '14 at 07:33
  • You might want to check [My Example](http://stackoverflow.com/a/16745054/643085) of a similar using current, relevant, non-deprecated .Net UI technology. – Federico Berasategui May 15 '14 at 12:37

1 Answers1

0

First of all: unfortunately you can't use multiple colors in a TextBox, so use a RichTextBox instead.

I would also recommend to separate the label (as it's name suggest) to a Label control, but if you don't want to do that, then you can re-use most of the code from your Console application, but instead of writing things out to the console, you should add them to your RichTextBox. See this page for further help on how to do that.

qqbenq
  • 10,220
  • 4
  • 40
  • 45
  • @qqbenbq I tried using the code, but the text doesn't align beneath the line before it properly. It just continues to flow to the next line (I wanted it to be like in the example I provided). I eidted to main post and put the code for WinForms. – user3171969 May 15 '14 at 07:47
  • You should add line breaks at the end of your lines, as AppendText does not add them. Apart from that I suggest you debug the code and see where the problem could lie. – qqbenq May 15 '14 at 07:58
  • I've tried that, and it seems like it doesn't split by \n for some reason, each text I enter is one big line so i tjust appends it normally .Can you help? – user3171969 May 15 '14 at 08:30