Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CheckedListBox Help for Newbie?

Status
Not open for further replies.

homer33

IS-IT--Management
Sep 19, 2002
3
0
0
GB
Hi

I'm just starting out learning c# and at the moment playing with a checkedlistbox in a windows form.
I have a query that populates the checkedlistbox then I want to iterate through the checked items and get the text into a string.

This is what I've been trying

for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
{
string ticked = checkedListBox1...?..(i).ToString();
..//code that uses ticked;
}
Is this completely wrong or is there something simple I'm missing?

Any help would be great.
 
To iterate through the checked items and get the text into a string, do as follows:

IEnumerator myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator();
while (myEnumerator.MoveNext() != false)
{
int i = (int)myEnumerator.Current;
string s = checkedListBox1.Items.ToString();
// Now the text of checked item is in s, you can do whatever you want such as:
MessageBox.Show (s);
}

Minh Hoa
 
Excellent!
Thankyou, I'm back to my books to make sure I understand the code.

 
Another way to do this(easier,I think):

for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
{
label1.Text += checkedListBox1.CheckedItems.ToString();
}

MaryDia
 
Actually:

for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
{
label1.Text += checkedListBox1.CheckedItems.ToString();
}
 
Here's another way:
Code:
foreach (ListItem li in checkedListBox1.Items){
 if (li.Selected){
   label1.Text +=     //li.Value or li.Text
 }
}

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top