简体   繁体   中英

CheckedListBox get selected index

How can I get the value of the selected index in a CheckedListBox. I tried via through an if condition and switch cases, but it is not working as expected.

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{            
   if (checkedListBox1.GetItemCheckState(0)==CheckState.Checked)
   {                               
      richTextBox1.Font = new Font(richTextBox1.Font, FontStyle.Bold);
   }
}

i think you can use

    checkedListBox1.CheckedIndices

Something like

    foreach(int index in checkedListBox1.CheckedIndices)
    {
    if(index == 1)
    {
          //do something
     }
  }

Try this:

if(checkedListBox1.CheckedItems.Count != 0)  
{  
   // If so, loop through all checked items and print results.  
   string s = "";  
   for(int x = 0; x <= checkedListBox1.CheckedItems.Count - 1 ; x++)  
   {  
      s = s + "Checked Item " + (x+1).ToString() + " = " + checkedListBox1.CheckedItems[x].ToString() + "\n";  
   }  
MessageBox.Show (s);  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM