5

I have textbox that I use for diagnostic purposes. The code behind is really simple:

XAML:

<TextBox HorizontalAlignment="Left" Margin="640,20,0,0" TextWrapping="Wrap" Height="280" Width="840" Name="txtDiagnostic" IsHitTestVisible="True" />

C#:

private void AddMessage(string message)
{
    txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message);
}

How can I define that each new input is on a different line? Because right now all errors are in just 1 long line.

14:15:00 Error 1 14:16:00 Error 2 14:17:00 Error 3

Instead of readable with line breaks between each error like this example:

14:15:00 Error 1
14:16:00 Error 2
14:17:00 Error 3

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Belekz
  • 480
  • 1
  • 7
  • 18

3 Answers3

5

add an Environment.NewLine at the end of each string

txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message) + Environment.NewLine;

and make sure that the textbox is capable of multiline

XAML:

<TextBox
  Name="tbMultiLine"
  TextWrapping="Wrap"
  AcceptsReturn="True"                    <-- IMPORTANT
  VerticalScrollBarVisibility="Visible"   <-- IMPORTANT
>

EDIT: As to respond to the usual string concatination debate you can of course use string.Concat()

String.Concat(txtDiagnostic.Text,DateTime.Now.ToString("hh:mm:ss:fff") , " " , "ERROR....." , Environment.NewLine);

It will be faster. Here is a benchmark code for LINQPad with a 1000 lines:

void Main()
{
    Stopwatch sw = new Stopwatch();

    string text = "";
    sw.Start();
    for (int i = 0; i < 1000; i++)
    {
        //text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + "ERROR.....") + Environment.NewLine;
        String.Concat(text,DateTime.Now.ToString("hh:mm:ss:fff") , " " , "ERROR....." , Environment.NewLine);
    }
    sw.Stop();
    Console.WriteLine("ELAPSED: " + sw.ElapsedMilliseconds);

}

Output:

+ concatentation took (on my machine) 16 msek
Concat needed 10 msek

Choose yourself, you should know how many error messages you would like to "inform" the user with ;)

Disclaimer: 1000 lines is a very bad benchmark, but I chose it here to fit the use case at hand. Reading more than a 1000 (or even a 1000) lines of error messages is not a software I would like to use. If you start concatenating larger sets of lines (x > 1000) then you really should use the StringBuilder as is also mentioned in the string concatenation debate Link that I provided.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 1
    By the way, you should consider using `string.Concat()` or a `StringBuilder` instead of `+` when putting strings together. – Atlasmaybe Jan 19 '18 at 13:33
  • Which benefits does that have? – Belekz Jan 19 '18 at 13:36
  • @Atlasmaybe not necessarily. It depends on the amount. Using a `StringBuilder` looks like overhead to me here. `Concat` would be probably the fastes way, but the difference might be negligable depending on the amount of lines OP wants to show – Mong Zhu Jan 19 '18 at 13:38
  • @Belekz what is the guessed maximum amount of lines that you would like to display? – Mong Zhu Jan 19 '18 at 13:38
  • If you repeat this task quite often of creating this new string you set together , you get a huge performance improvement with using a `StringBuilder` instead of using the *default* string set together thingy. – L. Guthardt Jan 19 '18 at 13:38
  • At this moment I would say 15 lines, because it's a very small application, but I'll use this technique again in next applications for sure. – Belekz Jan 19 '18 at 13:40
  • 1
    @Belekz, then you should really consider using a `StringBuilder`. Also, if you want to debug your application, you could also use `Debug.WriteLine()` or `Trace.WriteLine()` – Atlasmaybe Jan 19 '18 at 13:44
2

Implementation of Environment.NewLine from the source code:

The implementation in .NET 4.6.1: Source

/*===================================NewLine====================================
**Action: A property which returns the appropriate newline string for the given
**        platform.
**Returns: \r\n on Win32.
**Arguments: None.
**Exceptions: None.
==============================================================================*/
public static String NewLine {
    get {
        Contract.Ensures(Contract.Result<String>() != null);
        return "\r\n";
    }
}

So you could go with \r\n as your last two digits of the string as output text and the result is the exact same as Mong Zhu's answer, since Environment.NewLine is it's implementation.

txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message + "\r\n");

It depends on the platform if you either use \n or \r\n. On Windows it is actually \r\n.

From MSDN:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

L. Guthardt
  • 1,990
  • 6
  • 22
  • 44
1

You can use also AppendText():

this.txtTest.AppendText("blablabla" + Environment.NewLine);
Luke
  • 58
  • 6