On the Grid , I have a TextBox :

 <TextBox x:Uid="tboxWriteAMessage" Name="TboxInput" Margin="5" SelectionStart="0" Grid.Column="0" IsReadOnly="False" Style="{StaticResource TextBoxStyle1}" MinHeight="50" TextWrapping="Wrap" TextChanged="TboxInput_TextChanged" GotFocus="TboxInput_GotFocus" LostFocus="TboxInput_LostFocus" KeyDown="TboxInput_KeyDown" KeyUp="TboxInput_KeyUp"/> 

When you enter a string whose length exceeds the width of the text field, it is translated to a new line. With this, everything is fine. However, I cannot make the carriage move to a new line when pressing certain buttons.

I tried to do it like this:

 TboxInput.Text += Environment.NewLine; 

Only it does not work as it should - takes the carriage to the very beginning of the text field.

    1 answer 1

    In general, everything was easy. You should have used this code:

     TboxInput.Text += "\n"; TboxInput.SelectionStart = TboxInput.Text.Length; 

    Although with this option, not everything is good, because you can not make a new line break, if this line is empty. It is also impossible to find out the length of the longest string.

    Update

    It turns out that you can find out the length of the longest string simply by the text:

     var strArray = TbMessage.Text.Split(new char[] {}, StringSplitOptions.None); var maxLength = strArray.Max().Length;