日期:2014-05-18  浏览次数:20900 次

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);
                }
            }