开发者

Unexpected character is displayed in the textbox

开发者 https://www.devze.com 2023-03-11 08:09 出处:网络
I have a very simple .NET program.It\'s just to write a string to the textbox.There is a strange character appearing at the end of my string.

I have a very simple .NET program. It's just to write a string to the textbox. There is a strange character appearing at the end of my string.

Unexpected character is displayed in the textbox

This happens only on my 32-bit XP box. The same program works fine on another 64bit Windows 2008 machine.

The program is as simple as this.

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = "Hello\n\0\0\0\0\0\0";
    }

I know it's odd to add \0 at the end of the string and I can trim them before applying to the textbox. The textbox is set to allow multi-line.

Just out of curiousity, does anybody know where the problem comes from? Both machines got .NET 3.5 SP1 installed. Both of them are set to have the same regional settings. I doubt if it is related to 32-bit or 64-bit.

UPDATE

Thanks to @DBM and @Andrew. The strange character is coming fro开发者_如何学编程m \n but nothing to do with \0. Now, it sounds like Windows 2008 can understand both \r\n and \n. Can anybody confirm that?


Standard end-of-line sequence in Windows is \r\n. The text box isn't recognising the \n as a new-line without the preceding carriage return (\r).


You should use Environment.NewLine instead of \r\n, in general.

In the way of explanation: Environment.NewLine will contain the proper sequence of escape characters for whatever platform the application is running in. On Windows, it's \r\n, but *nix (if I recall correctly) uses \n only.

0

精彩评论

暂无评论...
验证码 换一张
取 消