Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

Hi, Iam using C# .Net. With my program, I have a multiline TextBox showing the text as like the below manner. When I press <enter key=""> simply it's leaving a blank line at the top of the textBox line.
Hence the Text is going below, and showing a blank empty line at the top of TextBox.
Is it possible to avoid the blank line for multiline TextBox.?

C#
textBox1.Text="Line1"+System.Environment.NewLine+"Line2"+System.Environment.NewLine+"Line3"

After pressing the <enter> Key the Output is

C#
"Blank Line"
"Line1"
"Line2"
"Line3"


Thanks for the helps & guidances
Posted
Updated 19-Sep-14 3:11am
v2
Comments
ravikiran from Hyderabad 19-Sep-14 8:53am    
In your code why did you put two + symbols

++System.Environment.NewLine+"Line3"

Paramu1973 19-Sep-14 9:13am    
Yes. It's Paste() unknown mistake....
ZurdoDev 19-Sep-14 9:14am    
I'm confused. You say you are pressing Enter and you don't want that to create a blank line?

Handle PreviewKeyDown event
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 19-Sep-14 10:30am    
+5 :-)
Assigning Text to a TextBox in code (programatically) ... if you've called the 'Focus method of the TextBox ... does not move the current insertion point in the TextBox to the end of the inserted Text as one might expect. The insertion point is set to the beginning of the assigned Text.

So, after your paste, when you press 'Enter, you'll insert a blank line.

I suspect you are doing something like this:
C#
private void button1_Click(object sender, System.EventArgs e)
{
    textBox1.Focus();
    textBox1.Text = "Line1" + System.Environment.NewLine + "Line2" + System.Environment.NewLine + "Line3";
}
Yes, that will result in an empty line if you press Enter after that.

If you do this:
C#
private void button1_Click(object sender, System.EventArgs e)
{
    textBox1.Text = "Line1" + System.Environment.NewLine + "Line2" + System.Environment.NewLine + "Line3";
    textBox1.SelectionStart = textBox1.Text.Length;
}
Observe what happens :)

To further your understanding I suggest you only assign the Text in the Button Click event ... now you will have to click on the TextBox to start typing in it, and the insertion point will be set where you click, but, sometimes the resulting location of the insertion point may not be what you expect.

Observe, experiment, analyze, deduce: repeat :)
 
Share this answer
 
v2

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