替换列表框中的文本 [英] c# - Replace Text in ListBox
本文介绍了替换列表框中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么说,在列表框中检测特定的文本并将其替换为特定的文本. 例如:
How would I say, detect specific text in a listbox and replace it with specific text. Eg:
private void timer1_Tick(object sender, EventArgs e)
{
if(listBox1.Text.Contains("Hi"))
{
// replace with Hello
}
}
推荐答案
在WinForms
中,您可以这样做:
if(listBox1.Items.Cast<string>().Contains("Hi")){ //check if the Items has "Hi" string, case each item to string
int a = listBox1.Items.IndexOf("Hi"); //get the index of "Hi"
listBox1.Items.RemoveAt(a); //remove the element
listBox1.Items.Insert(a, "Hello"); //re-insert the replacement element
}
这篇关于替换列表框中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文