I have a string string a="A:B:D"
. Which means the selected listview is the 1st, 2nd, and 3rd index listview
I want to display multiple selection listview according to above string.
code:
string a="A:B:D"
string pattern = @"[^:]";
foreach (Match m in Regex.Matches(a, pattern))
{
if (m.Value.Contains("A"))
{
ListManyOption.SelectedIndex = 0;
}
if (m.Value.Contains("B"))
{
ListManyOption.SelectedIndex = 1;
}
if (m.Value.Contains("C"))
{
ListManyOption.SelectedIndex = 2;
}
if (m.Value.Contains("D"))
{
ListManyOption.SelectedIndex = 3;
}
But when I use the code above, only the 3rd index listview is selected. How do I display the multiple selection listview according to the string above?
CodePudding user response:
Set the SelectionMode
property of the ListView
to Extended
or Multiple
and add the items to be selected to the SelectedItems
collection:
foreach (Match m in Regex.Matches(a, pattern))
{
if (m.Value.Contains("A"))
{
ListManyOption.SelectedItems.Add(ListManyOption.Items[0]);
}
if (m.Value.Contains("B"))
{
ListManyOption.SelectedItems.Add(ListManyOption.Items[1]);
}
if (m.Value.Contains("C"))
{
ListManyOption.SelectedItems.Add(ListManyOption.Items[2]);
}
if (m.Value.Contains("D"))
{
ListManyOption.SelectedItems.Add(ListManyOption.Items[3]);
}
}