如何在文本框中添加换行符 [英] how to add a newline to textbox

查看:712
本文介绍了如何在文本框中添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
        {
            string a;
            a = textBox1.Text;
            textBox2.Text += a;
            textBox1.Clear();
        }



这就是我所写的问题是代码在上一行旁边的textbox1中写下一个输入,但我想写入下一行...........我该怎么做........请帮助..........









问候,

Ahsan Naveed


this is what i have written the problem is that the code writes the next input in textbox1 next to the previous line but i want to write it in the next line...........how do I do it........please help..........




Regards,
Ahsan Naveed

推荐答案

使用此:



Use this:

private void button1_Click(object sender, EventArgs e)
{
    string a;
    a = textBox1.Text;
    textBox2.Text += String.Format("{0}{1}", (String.IsNullOrEmpty(textBox2.Text)) ? "" : Enviroment.NewLine, a);
    textBox1.Clear();
}





这会在textbox1的附加文本前面添加换行符,但仅限于textbox2已经有一些非空文本了。



问候,



Manfred



This will put a newline in front of the appended text from textbox1, but only if textbox2 already had some non-empty text inside it.

Regards,

Manfred


除非将其设置为多行文本框(将其多行属性设置为true),否则不能。然后,只是在文本中添加换行符的情况:

You can't, unless it is set as a multiline textbox (Set it's Multiline property to "true"). Then, it's just a case of adding a newline to the text:
string a = textBox1.Text;
textBox2.Text += "\r\n" + a;
textBox1.Clear();

或者更好:

Or better:

textBox2.Text = string.Format("{0}\r\n{1}", textBox2.Text, textBox1.Text);
textBox1.Clear();

因为它生成的中间字符串较少。

Since it generates fewer intermediate strings.


这篇关于如何在文本框中添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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