Click here to Skip to main content
15,881,630 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
foreach (string check in checkedListBox1.CheckedItems)
            {
                Properties.Settings.Default.CheckedItems = check;
                Properties.Settings.Default.Save();
                
            }

I am unable to fetch all checked items and store in settings.settings.

Can anyone help me with this?
Posted
Updated 13-Nov-11 20:43pm
v2
Comments
shashikanth 2011 14-Nov-11 2:38am    
Where you have placed that code and what is the problem in that..
bhagyap 14-Nov-11 2:45am    
I have placed it into an button event to save it..

The SelectedItems property will get you a list of all items selected.
You can use this to get the list of all selected items. See here[^].
 
Share this answer
 
Comments
bhagyap 14-Nov-11 2:47am    
Ya i have tried with selected items too but still i am unable to fetch the checked items..
Ok, Writing the Xml file from your code..
C#
try
                  {
                      //pick whatever filename with .xml extension
                      string filename = @"C:\Users\shashik\Desktop\"+"XML" + DateTime.Now.Day + ".xml";

                      XmlDocument xmlDoc = new XmlDocument();

                      try
                      {
                          xmlDoc.Load(filename);
                      }
                      catch (System.IO.FileNotFoundException)
                      {
                          //if file is not found, create a new xml file
                          XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                          xmlWriter.Formatting = Formatting.Indented;
                          xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                          xmlWriter.WriteStartElement("CheckBox");
                          //If WriteProcessingInstruction is used as above,
                          //Do not use WriteEndElement() here
                          //xmlWriter.WriteEndElement();
                          //it will cause the <root> to be <Root />
                          xmlWriter.Close();
                          xmlDoc.Load(filename);
                      }

                      foreach (ListItem item in cbl1.Items)
                      {

                          if (item.Selected)
                          {
                              XmlNode root = xmlDoc.DocumentElement;
                              XmlElement childNode = xmlDoc.CreateElement("CheckedItem");
                              XmlText textNode = xmlDoc.CreateTextNode("hello");
                              textNode.Value = "It is Checked Item";

                              root.AppendChild(childNode);
                              childNode.SetAttribute(item.Text, item.Value);
                              childNode.AppendChild(textNode);
                              xmlDoc.Save(filename);
                          }
                      }

                  }
                  catch (Exception ex)
                  {

                  }
              }</root>
 
Share this answer
 
Comments
bhagyap 17-Nov-11 1:22am    
Thanks for ur help.. but i am not understanding the scope of ListItem so can u please help me..
bhagyap 21-Nov-11 5:42am    
Please help me with ListItem as i am getting an TypeCast error..
shashikanth 2011 22-Nov-11 1:44am    
Where r u getting type casting error..
Send your code
You cant get all selected items directly,
As cbl1.SelectedItem will return only one selected item (which has the lowest index value..),
if you want all selected items then you have to check each and every item, whether it was selected or not..
C#
foreach (ListItem item in cbl1.Items)
            {
                if (item.Selected)
                {
                    // you can use item.Value; and your logic
                }
            }
 
Share this answer
 
Comments
bhagyap 14-Nov-11 4:19am    
I want only the checked items from checkedlistbox not all items.. please help me out..
That what i have written in the code,
the If condition filters the Selected items from the item list,
so you can write the code within the If condition..
C#
foreach (ListItem item in cbl1.Items)
            {
                if (item.Selected)
                {

                //Properties.Settings.Default.CheckedItems = check;
                //Properties.Settings.Default.Save();

                }
            }

What you want to do actually..?
What i sthis property(checkedListBox1.CheckedItems) that you have written in your code..?
their is no CheckedItems property in CheckBoxList.
 
Share this answer
 
v3
Comments
bhagyap 14-Nov-11 7:28am    
Thank You..
bhagyap 16-Nov-11 2:26am    
Thank You For ur help.. Now i want to build an XML File using the checked items..

Can u please help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900