C# 中的 TextBox 中换行

本教程将讨论在 C# 中向文本框中添加新行的方法。

C# 中带有 TextBox.Multiline 属性的 TextBox 新行

TextBox.Multiline 属性在其中存储一个布尔值。TextBox.Multiline 属性的值确定控件是否为多行文本框。该属性的值只能是 truefalsetrue 值表示此特定的文本框可以包含多行。false 值表示此特定的文本框不能包含多行。下面的代码示例向我们展示了如何使用 C# 中的 TextBox.Multiline 属性向文本框添加新行。

private void button1_Click(object sender, EventArgs e)
{
    string longtext = "This is some text and it is very very long for just one line";
    textBox1.Text = longtext;
    textBox1.AppendText(Environment.NewLine);
    textBox1.Multiline = true;
}

输出:

C# 中的 TextBox 中换行

在上面的代码中,我们使用 textBox1.Text = longtexttextBox1 中插入了一个很长的字符串,然后使用 textBox1.AppendText(Environment.NewLine)textBox1 中添加了新行。此后,通过将 textBox1.Multiline 属性的值设置为 true,我们指定 textBox1 是多行文本框。