读书人

CheckedListBox邦定值有关问题

发布时间: 2012-08-22 09:50:35 作者: rapoo

CheckedListBox邦定值问题
string strValue = "abc";
foreach (CheckBox c in CheckedListBox.Controls)
{
string chkValue = c.Text;
if (strValue.IndexOf(chkValue) >= 0)
{
c.Checked = true;
}
}

调试发现Controls为0,但实现有4个CheckBox ,请问如何处理?

[解决办法]
CheckedListBox的实现机制并非你所想像的那样
参考以下代码

C# code
            this.checkedListBox1.Items.Clear();            foreach (char s in "abigcat")            {                this.checkedListBox1.Items.Add(s);            }            string strValue = "abc";            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)            {                string chkValue = this.checkedListBox1.Items[i].ToString();                if (strValue.IndexOf(chkValue) >= 0)                {                    this.checkedListBox1.SetItemChecked(i, true);                }            } 

读书人网 >C#

热点推荐