c#-替换列表框中的文本
我该如何说,检测列表框中的特定文本并用特定文本替换它。 例如:c#-替换列表框中的文本,c#,listbox,C#,Listbox,我该如何说,检测列表框中的特定文本并用特定文本替换它。 例如: 在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.I
在
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
}
if(listBox1.Items.Cast().Contains(“Hi”){//检查项目是否有“Hi”字符串,将每个项目按字符串大小写
int a=listBox1.Items.IndexOf(“Hi”);//获取“Hi”的索引
listBox1.Items.RemoveAt(a);//删除元素
listBox1.Items.Insert(a,“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
}
if(listBox1.Items.Cast().Contains(“Hi”){//检查项目是否有“Hi”字符串,将每个项目按字符串大小写
int a=listBox1.Items.IndexOf(“Hi”);//获取“Hi”的索引
listBox1.Items.RemoveAt(a);//删除元素
listBox1.Items.Insert(a,“Hello”);//重新插入替换元素
}
在WinForm列表框中,Text属性包含当前所选项目的文本。(我假设您拥有所有字符串项) 如果需要查找某个项目的文本并使用其他内容对其进行更改,则只需在Items集合中查找该项目的索引,然后直接将实际文本替换为新文本
int pos = listBox1.Items.IndexOf("Hi");
if(pos != -1) listBox1.Items[pos] = "Hello";
还请注意,如果字符串不存在,IndexOf将返回-1,因此无需添加另一项检查来查找字符串是否在列表中。在WinForm列表框中,Text属性包含当前选定项的文本。
(我假设您拥有所有字符串项) 如果需要查找某个项目的文本并使用其他内容对其进行更改,则只需在Items集合中查找该项目的索引,然后直接将实际文本替换为新文本
int pos = listBox1.Items.IndexOf("Hi");
if(pos != -1) listBox1.Items[pos] = "Hello";
另外请注意,如果字符串不存在,IndexOf将返回-1,因此无需再添加一个检查来查找字符串是否在列表中您是在谈论Winforms ListBox吗?您是在谈论Winforms ListBox吗?