首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

CheckedListBox邦定值有关问题

2012-08-22 
CheckedListBox邦定值问题string strValue abcforeach (CheckBox c in CheckedListBox.Controls){str

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

热点排行