Next text entry on another line

I have a text box that I use for diagnostic purposes. The code behind is very simple:

XAML:

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

WITH#:

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

How can I determine that each new entry is on a different line? Because now all errors are only in 1 long line.

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

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

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

+5
source share
3 answers

add Environment.NewLine at the end of each line

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

and make sure the text box is capable of multi-line

XAML:

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

EDIT: to answer a normal discussion of a discussion of a string sequence, 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 the reference code for LINQPad with 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:

+ taken concatentation (on my machine) 16 msek
Concat 10 msek required

Choose yourself, you should know how many error messages you want to "tell" the user;)

Disclaimer: 1000 lines is a very bad indicator, but I chose it here to fit the option used. Reading more than 1000 (or even 1000) lines of error messages is not the software that I would like to use. If you start concatenating larger sets of strings (x> 1000), you really should use StringBuilder , as is also mentioned in the string concatenation topic discussed in the discussion section.

+5
source

Implementation of Environment.NewLine from source code:

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

That way you can use \r\n as the last two digits of string as the output text, and the result will be exactly the same as the Mong Zhu answer, since Environment.NewLine is its implementation.

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

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

From MSDN:

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

+2
source

You can also use AppendText ():

 this.txtTest.AppendText("blablabla" + Environment.NewLine); 
+1
source

Source: https://habr.com/ru/post/1274816/


All Articles