Search code examples
c#winui-3winui

How to give new line in a TextBox in WinUi3


I have a problem in adding in a new line in a TextBox in WinUi3.

I am getting the following data as input:

WEATHERCHECK/It's raining heavily/Start of play delayed for 2 hours 43 mins

For every '/', its supposed to go to a new Line, but not happening.

  • I first replaced '/' with '\n', didnt work
  • Then replaced '/' with '\r\n', didnt work
  • Then split the text with '/' & added '\r\n' - didnt work
  string[] values = value.Split("/");

  foreach(string value1 in values)
  {
      Commentary.Text += value1 + "\r\n";
  }
  • Used Environment.NewLine - didnt work
  string[] values = value.Split("/");
string newLine = Environment.NewLine;

  foreach(string value1 in values)
  {
      Commentary.Text += value1 + newLine;
  }

Result is still same: enter image description here

XAML for Textbox

 <StackPanel Orientation="Horizontal">
     <TextBox Height="100" TextWrapping="Wrap" Width="500" x:Name="Commentary" BorderThickness="2" BorderBrush="Black" Foreground="Red" Background="AliceBlue" Text="Commentary" />
 </StackPanel>

What can I do to show each part in new Line ?

Solved:

As per Comment from Luuk, I gave AcceptsReturn = "true" in XAML and used the same loop to add "\r\n" at the end and it worked.


Solution

  • Setting AcceptsRetrun to true should o that ? (quote: "true if pressing the ENTER key inserts a new line at the current cursor position; otherwise, the ENTER key is ignored." )