如何在文本框中添加换行符
问题描述:
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.