如何在多行文本框中添加文本?

我必须将我的文件的详细信息添加到多行文本框中。 但是所有细节都会在文本框中的一行中添加,而不是在垂直序列中添加。 我使用了Environment.NewLine并使用了“\ r \ n”,但它没有任何帮助。 我在Windows窗体表单中勾选了多行文本框,并将其设置为true但无效。

我的代码行是这样的:

m_Txt.Multiline = true; m_Txt.Text = fileInfo.m_Title + "\r\n" + fileInfo.m_Identifier + Environment.NewLine + fileInfo.m_TotalTime; 

一个更清洁的答案是:

假设txtStatus是一个文本框:

 txtStatus.Multiline = True; txtStatus.Clear(); txtStatus.Text += "Line 1" + Environment.NewLine; txtStatus.Text += "Line 2" + Environment.NewLine; 

使用内置枚举意味着更清晰的代码。

Shift + Enter键

 In the Visual Studio resource editor, you can hit "Shift + Enter" to create a new line, as doing something like "\r\n" will get escaped out. You will also need to increase the cell height to see both lines as it does not auto-size. 

如果您以编程方式执行此操作,请将新行追加到m_Txt.Lines,这是一个字符串[]。

 m_Txt.Lines = new string[]{ fileInfo.m_Title, fileInfo.m_Identifier, fileInfo.m_TotalTime}; 

我刚写了这段代码,似乎工作得很好。

 public void setComments(String comments) { String[] aux; if(comments.Contains('\n')) //Multiple lines comments { aux = comments.Split('\n'); for (int i = 0; i < aux.Length; i++) this.textBoxComments.Text += aux[i] + Environment.NewLine; } else //One line comments this.textBoxComments.Text = comments; } 

不确定为什么你的代码不起作用,除非发生其他事情。

我刚刚使用C#创建了一个WinForms项目,添加了一个文本框,将其设置为多行并添加了以下代码 – 一个魅力。

 textBox1.Text = "a\r\nb";