如何避免EnterKey()在TextBox()顶部留下空白行? [英] How to avoid EnterKey() For Leaving Blank Line At Top of the TextBox()?

查看:49
本文介绍了如何避免EnterKey()在TextBox()顶部留下空白行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我使用C#.Net。使用我的程序,我有一个多行TextBox显示文本,如下所示。当我按< enter key =>只是它在textBox行的顶部留下一个空行。

因此文本在下面,并在TextBox顶部显示一个空白的空行。

是可以避免多行TextBox的空行。?



 textBox1.Text =    Line1 + System.Environment.NewLine +   Line2 + System.Environment.NewLine +   Line3 



按下< enter>后键输出是



  空行 
Line1
< span class =code-string> Line2
< span class =code-string> Line3





感谢帮助& guidances

解决方案

处理 PreviewKeyDown 事件


在代码中以编程方式将文本分配给TextBox(以编程方式)。 ..如果你调用了TextBox的'Focus方法...不会将TextBox中的当前插入点移动到插入文本的末尾,就像人们预期的那样。插入点设置为指定文本的开头。



因此,粘贴后,当您按Enter时,您将插入一个空行。



我怀疑你做的是这样的:

  private   void  button1_Click( object  sender,System.EventArgs e)
{
textBox1.Focus() ;
textBox1.Text = Line1 + System.Environment.NewLine + Line2 + System.Environment.NewLine + < span class =code-string> Line3;
}

是的,如果您在此之后按Enter键将导致空行。



如果您这样做:< pre lang =cs> 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;
}

观察会发生什么:)



为了进一步理解,我建议你只在Button Click事件中指定Text。 ..现在你必须单击TextBox才能开始输入它,插入点将设置在你点击的位置,但是,有时插入点的结果位置可能不是你所期望的。



观察,试验,分析,演绎:重复:)


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.?

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


After pressing the <enter> Key the Output is

"Blank Line"
"Line1"
"Line2"
"Line3"



Thanks for the helps & guidances

解决方案

Handle PreviewKeyDown event


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:

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:

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 :)


这篇关于如何避免EnterKey()在TextBox()顶部留下空白行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆