问题
I have problem to select item from string separated comma and check item in checkedlistbox
How can I select item from string separated comma and then check in checkedlistbox? Preview My Program
download My project from this file Download How can check in checkedlistbox with textbox and separated comma
When My checkedlistbox is connect to SQl database is no action for TextBox Separated by comma
Download New Program with SQL Script From Comment
回答1:
Insert this code in your button2 click and it should work :
string s = textBox1.Text.ToString();
string[] values = s.Split(',');
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);//First uncheck the old value!
//
for (int x = 0; x < values.Length; x++)
{
if (checkedListBox1.Items[i].ToString() == values[x].ToString())
{
//Check only if they match!
checkedListBox1.SetItemChecked(i, true);
}
}
}
来源:https://stackoverflow.com/questions/42378515/how-to-set-checked-item-in-checkedlistbox-from-a-textbox-with-string-separated-b