c#如果我在字符串中有值名称,如何选择列表框项?

5 c# listbox

我有一个字符串'item3'和一个带有'item1,item2,item3,item4'的列表框,当我在字符串中有项目名称时,如何在列表框中选择item3?

谢谢

Nes*_*cio 15

int index = listBox1.FindString("item3");
// Determine if a valid index is returned. Select the item if it is valid.
if (index != -1)
     listBox1.SetSelected(index,true);
Run Code Online (Sandbox Code Playgroud)

  • 或者你可以做lb.SelectedIndex = lb.FindStringExact(fieldValue); (2认同)