How to set multiple items as selected in ListBox?

烂漫一生 提交于 2019-12-04 06:31:55

What about setting the Selected-property of the ListItem?

var names = new List<string>(new string[] { "ragu", "raju" });

foreach (var item in lbto.Items)
{
    if (names.Contains(item.Text))
        item.Selected = true;
}
Alejandro Haro

You can use FindByValue method

foreach (string item in stringList)
    lbxList.Items.FindByValue(item).Selected = true;

Using One line of linq

lbto.Items.Cast<String>().ForEach(i => i.Selected = names.Contains(i.Text));

OR

lbto.Items.OfType<string>().ForEach(i => i.Selected = names.Contains(i.Text));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!