C#中的CheckedListbox [英] CheckedListbox in C#

查看:62
本文介绍了C#中的CheckedListbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach (string check in checkedListBox1.CheckedItems)
            {
                Properties.Settings.Default.CheckedItems = check;
                Properties.Settings.Default.Save();
                
            }


我无法提取所有选中的项目并将其存储在settings.settings中.

有人可以帮我吗?


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

Can anyone help me with this?

推荐答案

SelectedItems属性将为您提供所有选定项目的列表.
您可以使用它来获取所有选定项目的列表.请参见此处 [
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[^].


好,从您的代码中写入Xml文件..
Ok, Writing the Xml file from your code..
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>


您不能直接获取所有选定的项目,
与cbl1.SelectedItem一样,将仅返回一个选定的项目(索引值最低..),
如果要所有选定的项目,则必须检查每个项目,无论是否已选中..
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..
foreach (ListItem item in cbl1.Items)
            {
                if (item.Selected)
                {
                    // you can use item.Value; and your logic
                }
            }


这篇关于C#中的CheckedListbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆