解决方案

CheckListBox用法详解

seo靠我 2023-09-25 13:14:11

1.添加项

checkedListBox1.Items.Add("蓝色"); 

checkedListBox1.Items.Add("红色"); 

checkedListBox1.Items.Add("黄色")SEO靠我;

2.判断第i项是否选中,选中为true,否则为false

if(checkedListBox1.GetItemChecked(i))

{

  return true;

else

{

  return false; 

}

3SEO靠我.设置第i项是否选中

checkedListBox1.SetItemChecked(i, true);  //true改为false为没有选中。

4.设置全选 

添加一个名为select_all的checkbSEO靠我ox控件,由其控制checkedListBox是全选还是全不选。

private void select_all_CheckedChanged(object sender, EventArgs e) 

iSEO靠我f(select_all.Checked)

{

     for (int j = 0; j < checkedListBox1.Items.Count; j++) 

checkedListBox1.SetItemChSEO靠我ecked(j, true);

}

else 

{

for (int j =0; j < checkedListBox1.Items.Count; j++) 

checkedListBox1.SetItemChecSEO靠我ked(j, false);

}

}

5.得到全部选中的值 ,并将选中的项的文本组合成为一个字符串。

 string strCollected = string.Empty;

for (int i = 0; i SEO靠我< checkedListBox1.Items.Count; i++)

 {

      if (checkedListBox1.GetItemChecked(i))

      {

if (strCollected == strinSEO靠我g.Empty)

          {

               strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);

          }

          else

          {

strCollected = sSEO靠我trCollected + "/" + checkedListBox1.GetItemText(checkedListBox1.Items[i]);

           }

       }

}

6.设置CheckedListBox中第i项的SEO靠我Checked状态

checkedListBox1.SetItemCheckState(i, CheckState.Checked);

7. 

private void checkBoxAll_CheckedSEO靠我Changed(object sender, EventArgs e)

    if (checkBoxAll.Checked) 

    { 

//被选择了则将CheckedListBox中的所有条目都变为Checked状SEO靠我

        for (int i = 0; i < checkedListBoxLayerControl.Items.Count;i++) 

        {     

checkedListBoxLayerControl.SetItemCSEO靠我heckState(i,CheckState.Checked);

}

else 

      //否则变成Unchecked状态 

for (int i = 0;i < checkedListBoxLayerControSEO靠我l.Items.Count; i++)

{

checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked); 

}              

}

}

8. checSEO靠我kedListBox 单选设置(代码实现)

private void chkl_ItemAuditing_ItemCheck(object sender, ItemCheckEventArgs e)

iSEO靠我f(chkl_ItemAuditing.CheckedItems.Count > 0)

    { 

for(int i = 0; i < chkl_ItemAuditing.Items.Count; i++) 

{

iSEO靠我f(i != e.Index)

this.chkl_ItemAuditing.SetItemCheckState(i,System.Windows.Forms.CheckState.UncheckedSEO靠我);

}

9.checkedListBox1显示一个数据库中关键字对应的所有记录

for (int i = 0; i < table.Rows.Count; i++) 

string name = taSEO靠我ble.Rows["myname"].ToString();

    string paw = table.Rows["mypaw"].ToString(); 

checkedListBox1.Items.Add(SEO靠我name + paw);

}

10. for(i=0;i

{   

if(CheckedListBox.GetItemText(CheckedListBox.Items)=="你得到的值"SEO靠我)

{   

CheckedListBox.SetItemChecked(i,true);   

}   

}

11.清除checkedListBox1中所有的选项

for (int i = 0; i < checkedListBSEO靠我ox1.Items.Count; i++)

{

    checkedListBox1.Items.Clear();

}

12.设置索引为index的项为选中状态

for (int i = 0; i < checkedSEO靠我ListBox1.Items.Count; i++)

{

    checkedListBox1.SetItemChecked(i, true);

13.   

for (int i = 0; i < checkedLiSEO靠我stBox1.Items.Count; i++)

{

if (checkedListBox1.GetSelected(i)) 

{

MessageBox.Show(checkedListBox1.CheckedSEO靠我Items.ToString());

}

}

14.选中checkedListBox1所有的选项

for (int i = 0; i < checkedListBox1.Items.Count; i++)         

{

cSEO靠我heckedListBox1.SetItemCheckState(i, CheckState.Checked);

}

15.             

for (int i = 0; i < checkedListBox1.ItemSEO靠我s.Count; i++)

{  

//如果checkedListBox1的第i项被选中,

//则显示checkedListBox1对应的值

if (checkedListBox1.GetItemChecked(SEO靠我i))

     MessageBox.Show(checkedListBox1.Items.ToString()); 

}

}

16.反向选择checkedListBox1的选项

for (int i = 0; i <SEO靠我 checkedListBox1.Items.Count; i++)

if (checkedListBox1.GetItemChecked(i)) 

    { 

checkedListBox1.SetItemCheSEO靠我cked(i, false);

    } 

    else 

    { 

        checkedListBox1.SetItemChecked(i, true); 

    } 

}

17.checkedListBox1中选定的项->checkedListBSEO靠我ox2

for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) 

checkedListBox2.Items.Add(this.checkSEO靠我edListBox1.CheckedItems);

//remove是除去一个具体的值,不是index,注意了

this.checkedListBox1.Items.Remove(this.checkedSEO靠我ListBox1.CheckedItems);

} 18.绑定数据

checkedListBox1.DataSource = dt;

checkedListBox1.DisplayMembSEO靠我er = "item";

checkedListBox1.ValueMember = "code";

这个属性在checklistbox里是没有的,但是可以直接使用

19.

for (int i = 0; iSEO靠我 < checkedListBox1.Items.Count; i++)

{

if (checkedListBox1.GetItemChecked(i))

{

checkedListBox1.SelectedSEO靠我Index = i;

//利用SelectedValue取得Value值时,只能取得当前焦点项的值。所以要对整个CheckedListBox中的所有勾选项,让其都做一次焦点 项才能取得所有勾选的项的值。SEO靠我

str+= checkedListBox1.SelectedValue;

}

}

20.

CheckedlistBox控件比较有用到两个属性分别为CheckOnClick为True:表示单击就选中当前行,为FSEO靠我alse :要点两下才可以选中。(默认值为False)。还有一个属性为ThreeDCheckBoxes为True:表示三维的选中标记,为False:表示表面的显示标记。(默认值为False)。

“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2