Visual Studio C# listview保存和加载问题

我对 Visual Studio C# listview 项目有疑问,无法通过谷歌搜索找到解决方案。


我是用带有列表视图的应用程序制作的,我可以添加、删除、更新列表视图项目。我使用以下代码正确保存列表视图到文件或从文件加载列表视图:


 private void saveListViewItems(string path, ListView lv)

    {

        var delimeteredListviewData = new List<string>();

        string delimeteredItems = string.Empty;


        foreach (ListViewItem lvi in lv.Items)

        {


            foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)

            {


                    delimeteredItems += lvsi.Text + "#";


            }

            delimeteredListviewData.Add(delimeteredItems);

        }



        System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());

    }


    private void loadListViewItems(string path, ListView lv)

    {


        foreach (string line in System.IO.File.ReadAllLines(path))

        {

            lv.Items.Add(new ListViewItem(line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)));


        }

    }

问题是我已经激活了每个项目旁边的复选框。我无法保存选中复选框的列表视图项目。我想保存列表视图并加载所选项目。谢谢

https://img1.sycdn.imooc.com/65abc7c500017c9404990314.jpg

森林海
浏览 202回答 3
3回答

呼啦一阵风

像这样的东西?:&nbsp; &nbsp; &nbsp; &nbsp; List<string> delimeteredItems = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (ListViewItem lvi in listView1.CheckedItems)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; &nbsp;item= String.Join("#", lvi.SubItems.Cast<ListViewItem.ListViewSubItem>().Select(si=>si.Text));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delimeteredItems.Add(item);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.IO.File.WriteAllLines(@"c:\temp\lines.txt", delimeteredItems);

万千封印

我已经管理该程序以使其正常工作。我对读/写文本文件的方式是错误的。现在我正在使用 ini-parser nuget。将ini解析器安装到项目中。然后:使用 IniParser;使用 IniParser.Model;&nbsp;private void saveListViewItems(string path, ListView lv)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; string IPFrom;&nbsp; &nbsp; &nbsp; &nbsp; string IPFromval;&nbsp; &nbsp; &nbsp; &nbsp; string IPTO;&nbsp; &nbsp; &nbsp; &nbsp; string IPTOval;&nbsp; &nbsp; &nbsp; &nbsp; string Comment;&nbsp; &nbsp; &nbsp; &nbsp; string Commentval;&nbsp; &nbsp; &nbsp; &nbsp; string CheckState;&nbsp; &nbsp; &nbsp; &nbsp; string CheckStateval;&nbsp; &nbsp; &nbsp; &nbsp; int IPSectioncount;&nbsp; &nbsp; &nbsp; &nbsp; var parser = new FileIniDataParser();&nbsp; &nbsp; &nbsp; &nbsp; IniData data = parser.ReadFile(path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ( i < lv.Items.Count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((lv.Items[i].Selected) || (lv.Items[i].Checked))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckStateval = "1";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckStateval = "0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckState = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckState = "CheckState";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckState += String.Join(CheckState, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFromval = lv.Items[i].SubItems[0].Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFromval = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFrom = "IPFrom";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFrom += String.Join(IPFrom, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFromval = lv.Items[i].SubItems[0].Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTOval = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTO = "IPTO";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTO += String.Join(IPTO, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTOval = lv.Items[i].SubItems[1].Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Commentval = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment = "Comment";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment += String.Join(Comment, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Commentval = lv.Items[i].SubItems[2].Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data["IP"][CheckState] = CheckStateval;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data["IP"][IPFrom] = IPFromval;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data["IP"][IPTO] = IPTOval;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data["IP"][Comment] = Commentval;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPSectioncount = lv.Items.Count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data["IP"]["IPSectionCount"] = IPSectioncount.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parser.WriteFile(path, data);&nbsp; &nbsp; }&nbsp; &nbsp; private void loadListViewItems(string path, ListView lv)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string IPFrom;&nbsp; &nbsp; &nbsp; &nbsp; string IPTO;&nbsp; &nbsp; &nbsp; &nbsp; string Comment;&nbsp; &nbsp; &nbsp; &nbsp; string CheckState;&nbsp; &nbsp; &nbsp; &nbsp; string IPSectioncount;&nbsp; &nbsp; &nbsp; &nbsp; string row="";&nbsp; &nbsp; &nbsp; &nbsp; var parser = new FileIniDataParser();&nbsp; &nbsp; &nbsp; &nbsp; IniData data = parser.ReadFile(path);&nbsp; &nbsp; &nbsp; &nbsp; IPSectioncount = data["IP"]["IPSectionCount"];&nbsp; &nbsp; &nbsp; &nbsp; int m = Int32.Parse(IPSectioncount);&nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; while ( i < m )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFrom = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTO = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFrom = "IPFrom";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFrom += String.Join(IPFrom, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPFrom = data["IP"][IPFrom];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTO = "IPTO";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTO += String.Join(IPTO, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPTO = data["IP"][IPTO];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment = "Comment";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment += String.Join(Comment, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment = data["IP"][Comment];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckState = "CheckState";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckState += String.Join(CheckState, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckState = data["IP"][CheckState];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row = String.Join(",",IPFrom,IPTO,Comment);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lv.Items.Add(new ListViewItem(row.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (CheckState == "1")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lv.Items[i].Checked = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

潇湘沐

您需要保存更多信息,而不是仅保存字符串。您可以添加 IsChecked 值和字符串。您需要一个小的包装类来将这两个值作为属性保存。然后您可以使用序列化,它是一个术语,描述将对象转换为可以存储在磁盘上的内容的方法。有很多格式可供选择,但 Json 是一种很好的可读格式。您可以通过 nuget 包管理器下载 Nuget 包 NewtonSoft.Json。public class StoreListView{&nbsp; &nbsp; public void StoreToDisk(string path, List<ItemDataHolder> list)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string theListInJsonFormat = JsonConvert.SerializeObject(list);&nbsp; &nbsp; &nbsp; &nbsp; File.WriteAllText(path, theListInJsonFormat);&nbsp; &nbsp; }}public class ItemDataHolder{&nbsp; &nbsp; public string SomeValuesYouWantToSave { get; set; }&nbsp; &nbsp; public bool IsChecked { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP