[英]textbox Insert and remove function in vb.net or c#
How to update the text from the TextBox
control?如何更新
TextBox
控件中的文本?
Consider a TextBox
that already contains the string "Wel"考虑一个已经包含字符串“Wel”的
TextBox
To insert text in the TextBox
, I use:要在
TextBox
中插入文本,我使用:
TextBox1.Text = TextBox1.Text.Insert(3, "come")
And to remove characters from the TextBox
:并从
TextBox
中删除字符:
TextBox1.Text = TextBox1.Text.Remove(3, 4)
But I need to be able to do this:但我需要能够做到这一点:
TextBox1.Text.Insert(3, "come");
TextBox1.Text.Remove(3, 4);
However, this code doesn't update the TextBox
.但是,此代码不会更新
TextBox
。
It this possible?这可能吗?
Can this be accomplished via the append method?这可以通过 append 方法完成吗?
Text
property of TextBox is of type string which is immutable it's not possible to change the existing string. TextBox 的
Text
属性是字符串类型,它是不可变的,无法更改现有字符串。 Insert()
or Remove()
returns a new instance of string with the modification and you will have to assign this new instance back to TextBox's Text property. Insert()
或Remove()
返回一个带有修改的新字符串实例,您必须将这个新实例分配回 TextBox 的 Text 属性。
There is TextBox.AppendText()
that you might be interested in. It appends text to the end of the string but you cannot do anything like Insert()
or Remove()
with it though.您可能对
TextBox.AppendText()
感兴趣。它会将文本附加到字符串的末尾,但您不能对它执行诸如Insert()
或Remove()
之类的操作。
EDIT:编辑:
for your keypress, you could do something like this对于你的按键,你可以做这样的事情
char charToReplace = (char) (e.KeyChar + 1); // substitute replacement char
textBox1.SelectedText = new string(charToReplace,1);
e.Handled = true;
'string'
is a immutable type, so each time string value changes new memory is allocated. 'string'
是不可变类型,因此每次字符串值更改时都会分配新的 memory。 So if you want to insert or remove text from TextBox
, you have to assign it back to TextBox.Text
property.因此,如果要从
TextBox
插入或删除文本,则必须将其分配回TextBox.Text
属性。 However, if you just want to append text to the TextBox.Text
, you can do但是,如果您只想将 append 文本发送到
TextBox.Text
,您可以这样做
textBox.AppendText("Hello");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.