C# 如何在 textBox 中允许换行和空格

标签 c# user-interface textbox newline whitespace

<分区>

我正在尝试通过按下按钮在 C# 文本框中写入一个字符串。当我按下按钮时,我的字符串应该添加到文本框中。但是,添加了字符串,但是“\n”被完全忽略了。我怎么能允许呢?

这是一个非常简单的示例,我编写它只是为了了解问题出在哪里,但我注意到当我尝试将控制台输出重定向到文本框时,空格也会发生同样的事情。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String s = "first line";
            s += "\n"; 
            s += "second line";
            Console.Out.WriteLine(s);
            textBox1.AppendText(s);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }

我怎么调用它:

class Program
{
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

output in console and output in textBox

最佳答案

使用\r\nEnvironment.NewLine

关于C# 如何在 textBox 中允许换行和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912656/

相关文章:

javascript - ASP.NET:如何在回发后使用 javascript 保留在服务器控件上设置的值

c# - 如何在迁移中使用 DbContext?

c# - .NET WebBrowser 控件中的阻止对话框

c# - 使用 newtonsoft 进行 Json 序列化

c# - 当字体不支持符号时,如何在WPF文本框中插入版权,商标,服务标记等

user-interface - 如何在 Ubuntu 中使用 g++ 编译 fltk 程序?

android UI Material 风格

user-interface - 用于从大型层次结构中进行选择的 Web UI 设计指南

c# - 鼠标悬停文本框时删除浅蓝色边框

JavaScript/PHP/HTML : Disabling respective textbox on unchecked checkbox