如何从文本框中删除项目c#
我正在研究winapp。
这是一个机器人。
我想要添加和删除控制机器人的主人。
I'm working on a winapp.
It's a bot.
I want to add and delete masters who controls the bot.
//master add n remove
if (msg.Body != null && msg.Body.ToLower().StartsWith("addcon/"))
{
textBox4.Text = textBox4.Text + msg.Body.ToLower().Replace("addcon/", "#");
}
if (msg.Body != null && msg.Body.ToLower().StartsWith("delcon/"))
{
string str = msg.Body.ToLower().Replace("delcon/", "");
textBox4.Text.Replace(str + "#", "");
}
在上面的代码中,addcon / xyz命令被赋予bot来添加xyz到它的控制器,在textbox4中添加xyz#
和delcon / xyz用于从textbox4中删除xyz#
textbox4收集所有可通过远程命令控制机器人的机器人大师。
delcon无法正常工作。
任何帮助?
In the above code addcon/xyz command is given to bot to add xyz to its controller that adds xyz# in textbox4
and delcon/xyz coomand is given to delete xyz# from textbox4
textbox4 collects all ids of bot masters who can control the bot by remote commands.
The delcon is not working properly.
Any help?
只需添加到Mehdi所说的:替换不能就地工作 - 这意味着它不会修改现有的字符串,而是返回一个新的字符串,其中应用了更改(如果有), ,这同样适用于所有其他字符串方法 。
这是因为字符串是不可变的:一旦创建它们就无法更改。如果它们是可变的那么:
Just to add to what Mehdi said: Replace does not work in-place - which means it does not modify the existing string but returns a new string with the changes (if any) applied, and the same applies to every other string method.
This is because strings are immutable: they cannot be changed once created. If they were mutable then this:
string s1 = "Hello there!";
string s2 = s1;
s2.Replace("there", "World");
会导致更改发生在 s1
和 s2
因为它们都引用相同的字符串实例。
相反,它返回一个新的字符串,所以这个:
Would cause the change to happen to both s1
and s2
since they both reference the same string instance.
Instead, it returns a new string, so this:
string s1 = "Hello there!";
string s2 = s1;
s2 = s2.Replace("there", "World");
Console.WriteLine(s1);
Console.WriteLine(s2);
会打印
Would print
Hello there!
Hello World!
哪个更有用。
替换()
不就地操作并返回一个字符串,所以:
Replace()
does not operate in-place and gives you back a string, so :
textbox4.Text = textBox4.Text.Replace(str + "#", "");
请参阅我对该问题的评论。很难想象用一根弦操作会需要任何特别的帮助,除非你特别混淆了某些东西。
然而,使用 TextBox 似乎很糟糕。如果您将TextBox.Text
字符串的部分视为项目,请使用一些控件来代替项目:组合框,列表框,列表视图或更复杂的内容。 />-SA
Please see my comment to the question. It's hard to imagine that the manipulation with one string would need any special help, unless you have a confusion with something in particular.
However, the whole idea of usingTextBox
seems to be bad. If you consider parts of theTextBox.Text
string as "items", use some control with items instead: combo box, list box, list view or something more complex.—SA