简体   繁体   English

为什么/ n在具有多行且AcceptsReturn设置为True的文本框中不起作用

[英]Why does /n not function on a TextBox with Multiline and AcceptsReturn set to True

I've been attempting to work with the \\n in a TextBox with Multiline, and AcceptsReturn set to true. 我一直在尝试在具有多行的TextBox中使用\\ n,并将AcceptsReturn设置为true。 I have looked around and tried \\r\\n (without any better result) and also creating a String to host "Environment.NewLine" called CRLF, but this approach seems excessive. 我环顾四周并尝试了\\ r \\ n(没有更好的结果),并且还创建了一个字符串来托管名为CRLF的“ Environment.NewLine”,但是这种方法似乎过分了。 I've checked MSDN, and multiple entries here on Stackoverflow, but I'm coming up blank. 我已经检查了MSDN,并在Stackoverflow上检查了多个条目,但是我空白了。 What I find funny is that the text output still contains the new line when I paste said output to any other source. 我觉得很有趣的是,当我将上述输出粘贴到任何其他源时,文本输出仍包含新行。 My output window using Console.Write() as well as my CRLF entries work without a hitch. 使用Console.Write()的输出窗口以及CRLF条目都可以正常使用。

Here is a copy of the code: 这是代码的副本:

public partial class Form1 : Form
    {
        private String CRLF = Environment.NewLine;
        private String test = "";
        public Form1()
        {
            InitializeComponent();

        }

        private void btnNewManifest_Click(object sender, EventArgs e)
        {
            test = "Hello World 1\nHello World 2" + CRLF + "Hola!";
            txtOutput.Text = test;
            Console.WriteLine(test);
        }
    } 

You need to use \\r\\n for Windows, but I'd still recommend using Environment.NewLine as this will make your code run properly on all platforms. 您需要在Windows上使用\\r\\n ,但我仍然建议您使用Environment.NewLine因为这将使您的代码在所有平台上都能正常运行。

The following code works fine on Windows: 以下代码在Windows上可以正常运行:

public partial class Form1 : Form
{
    public TextBox txtOutput = new TextBox();

    public Form1()
    {
        InitializeComponent();

        this.txtOutput.Width = 200;
        this.txtOutput.Height = 100;
        this.txtOutput.Multiline = true;
        this.Controls.Add(this.txtOutput);

        this.txtOutput.Text = "My test\r\nNext line";
    }
}

Use this code and compare it to the textbox you have to try and figure out what else is up. 使用此代码并将其与您必须尝试找出其他问题的文本框进行比较。 I think there's something else going on behind the scenes. 我认为幕后还有其他事情发生。

By the way, you could do something like this (works for me), but I'm not sure it would work on other systems: 顺便说一句,您可以执行以下操作(对我有用),但是我不确定它是否可以在其他系统上运行:

this.txtOutput.Text = 
@"Line one
Line two
Line three";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM