How to set checked item in checkedlistbox from a TextBox with string separated by comma - c#

若如初见. 提交于 2019-12-14 04:24:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!