Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a TextBox in WPF for information, and where someone can take notes.
The user can write in it, save it, etc. But, for some reason the TextBox won't take the enter key while the app is running.
If you type enough characters to get to the end of the text box a new line is made, but it won't make one if you press enter.

this is the XAML for my text box.

<TextBox x:Name="___informationBox_" HorizontalAlignment="Left" Height="326" Margin="358,40,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="432" Text="Logarithms are used to find the powers of numbers. 

They are also used in the Richter scale, the PH scale, and the Decibel scale. 

A logarithm is written like this: 

log a(b) = c

Where a is the base, b is the product, and c is the power in an exponential expression. 

the above is converted to an exponential expression like this:

a^c = b

THE LOG LAWS:

The log laws. 

log law 1:
log A + log B = log AB

log law 2:
log A - log B = log A/B

log law 3: 
log A^n = n log A

The change of base formula: log b C = (log a C) / (log a B)

" Grid.ColumnSpan="2"/>



So how can this be fixed?

What I have tried:

I've searched the internet looking for something that can fix this, didn't find it.
Posted
Updated 20-Oct-19 21:30pm

you can do it on KeyDown event like

HTML
<StackPanel>
  <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDown"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>



The following code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a message is displayed and you'll see a new line appended.

C#
private void OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
       {
		MessageBox.Show($"You entered : {textBox1.Text}");
		var sb = new StringBuilder();
		sb.Append(textBox1.Text);
		sb.AppendLine("");
		textBox1.Text = sb.ToString();
		textBox1.CaretIndex = textBox1.Text.Length;
	}
}
 
Share this answer
 
Try this: Multiline for WPF TextBox - Stack Overflow[^]

The first answer explains what needs to be set:
TextWrapping="Wrap"
AcceptsReturn="True" 
 
Share this answer
 
Comments
i like food 21-Oct-19 5:03am    
Than both of you, they are very helpful answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900