您可以使用以下代码选择项目的索引,这些索引以您的字符串开头,然后遍历它们并设置选定的项目:
memlist.ClearSelected(); // clear previous selection
memlist.Items.Cast<object>()
.Select((item, index) => new { Text = memlist.GetItemText(item), Index = index })
.Where(x => x.Text.StartsWith(term, StringComparison.CurrentCultureIgnoreCase))
.ToList()
.ForEach(x => memlist.SetSelected(x.Index, true));
顺便说一句,不要忘记将SelectMode
ListBox 设置为多选。
您还可以在 foreach 循环中选择项目:
var indexesToSelect = memlist.Items.Cast<object>()
.Select((item, index) => new { Text = memlist.GetItemText(item), Index = index })
.Where(x => x.Text.StartsWith(term, StringComparison.CurrentCultureIgnoreCase))
.Select(x => x.Index);
foreach(int index in indexesToSelect)
memlist.SetSelected(index, true);