I am trying to figure out checkedListBoxes. I have found out through hours of searching how to display the index of the items as well as the items themselves.
int
i; string s;s =
"Checked items:\n"; for (i = 0; i <= (checkedListBox1.Items.Count - 1); i++){
if (checkedListBox1.GetItemChecked(i)){
s = s +
"Item " + (i + 1).ToString() + " = " + checkedListBox1.Items[i].ToString() + "\n"; MessageBox.Show(s);
what i cannot figure out is how to display information based on what is checked. like for example.
I have a list of names and each name carries 10 values that i need to print to a text file. How do i get the code to recognize the index and print the values based on what is checked? so if index 0 is checked then print x,y,z?
jamiePosted Jan 25, 2008, 3:32 PM
that is much to complicated at my level of understanding of c sharp. I just started learning this stuff. I was hoping there was a way like the regular checkboxes.. for example
if(checkbox1.Checked)
{
sw.WriteLine("some values here");
sw.Close();
}
if(checkBox2.Checked)
{
sw.WriteLine("some values here");
sw.Close();
}
so when i found out that a listcheckbox returns an index for each item, i thought there might be a simple way of using that. Like
if(index == 1)
{
sw.WriteLine("some values here");
sw.Close();
}
but i am finding out that it doesn't seem to work that way.
Matthew CochranPosted Jan 25, 2008, 10:03 AM
jamiePosted Jan 22, 2008, 8:53 PM
well i am trying to make a generator that will generate text. so you have choices of sky settings for a game
1. red sky
2. blue sky
3.purple sky
and if you choose one of those checkboxes, then it will use streamwriter to write to a text file the values needed to make those sky settings. So i don't want to write red, blue, purlple, i want to write a bunch of other things based on the choice. So I don't understand if what you wrote will help.
Scott LyslePosted Jan 22, 2008, 8:48 PM
You can just loop through the checked items. For example, if you have a checkedlistbox control called ckbList, you can get the collection of checked items; each item is a string containing the text from the checklistbox item. This code will get a list of all of the checked items in a checkedlistbox control and write each checked item into a richtextbox control.
foreach (string cb in ckbList.CheckedItems) { rtbText.Text += cb + Environment.NewLine; }