在C#中设置多行TextBox的特定行 [英] Set specific line of multiline TextBox in c#

查看:474
本文介绍了在C#中设置多行TextBox的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是C Sharp& amp;的新手. Windows窗体.我无法设置多行文本框的特定字符串.到目前为止,我已经尝试过以下方法.

Hello I am new to C Sharp & Windows Forms. I am unable to set the specific string of a multiline TextBox. I have tried below things so far.

textBox1.Lines[1] = "welcome to stackOverflow";

上面的代码没有给出编译时错误,但是当我使用Debug模式看到结果时,没想到.

The above code does not give a compile time error but when I saw the result using Debug mode it was not expected.

然后,我也正在阅读 MSDN 文章,但是在此文章中,有一个使用stream[]构造函数创建的新集合,但是仍然出现相同的问题.

Then i was also reading this MSDN article but in this there is a new collection created by using stream[] constructor but still the same problem arises.

推荐答案

它应该给编译器错误,因为您试图在此处将string分配给char:

It should give compiler error because you are trying to assign a string to char here:

textBox1.Text[1] = "welcome to stackOverflow";

Text属性的类型为string,当您在string上使用索引器时,它会在该位置提供char.而且字符串是不可变的,因此您不能在不创建新字符串的情况下真正更改特定位置的字符.

Text property is of type string, when you use indexer on a string it gives you the char at that position. And also string is immutable so you can't really change a character at specific position without creating a new string.

您应该像这样直接设置Text:

You should set the Text directly like this:

textBox1.Text = "welcome to stackOverflow";

或者,如果字符串数组中有多行,则应设置Lines属性:

Or if you have more than one line in an array of string you should set the Lines property:

var lines = new [] { "foo", "bar" };
textBox1.Lines = lines;

这篇关于在C#中设置多行TextBox的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆