简体   繁体   中英

C#: create a second identifier for a listbox item

I have been coding a reporting system. All the main variables from the reports get stored in a single .xml file, and then some of that same data is stored in other folders as single word .txt files that are saved under the same name as the reports main .xml file.

When it comes to loading all the reports into a listbox in which their display names are changed according to the filter (All|Newest|Oldest|Priority|A - Z|Z - A) and probably will have more, however although it displays and sorts the listbox items perfectly fine, when it comes to loading the reports up into the loader I have coded it requires the directory of the selected listbox item.

Originally when I was making this they were all listed by the name of the file, which obviously worked fine because the file name was used by the display name of the listbox item. Now since I'm making it cleaner and including filters, I can't exactly use that same technique, so I was thinking if there was a way to add a second identifier to the listbox items so that I can call the second identifier the name of the file its data is stored in, so that I can load the data via the second identifier or perhaps any other way that would have the same outcome.

public void UpdateReports()
{
    if (ReportsPrioritySelection.Text == "All")
    {
        ReportsList.Items.Clear();

        SortDescription sd = new SortDescription("", ListSortDirection.Descending);

        DirectoryInfo dinfo = new DirectoryInfo(@"Report&Fix\\Reports\\");
        FileInfo[] Files = dinfo.GetFiles("*.xml");
        foreach (FileInfo file in Files)
        {
            ReportsList.Items.Add(Path.GetFileNameWithoutExtension(file.Name));
            ReportsList.Items.SortDescriptions.Add(sd);
        }
    }

    if (ReportsPrioritySelection.Text == "Newest")
    {
        ReportsList.Items.Clear();

        SortDescription sd = new SortDescription("", ListSortDirection.Descending);

        DirectoryInfo dinfo = new DirectoryInfo(@"Report&Fix\\Reports\\");
        FileInfo[] Files = dinfo.GetFiles("*.xml");
        foreach (FileInfo file in Files)
        {
            ReportsList.Items.Add(File.ReadAllText(@"Report&Fix\\ReportData\\Date\\" + Path.GetFileNameWithoutExtension(file.Name) + ".txt") + " " + Path.GetFileNameWithoutExtension(file.Name));
            ReportsList.Items.SortDescriptions.Add(sd);
        }
    }
}

What you can do is bind the list to your collection of items, and then set which property in the collection should be the display one, and which should be the 'key'.

  var myItems = new List<SomeItem>() 
  {
    new SomeItem() { Id = 1, Name = "File1.txt" }, 
    new SomeItem() { Id = 2, Name = "File2.txt" }, 
    new SomeItem() { Id = 3, Name = "File3.txt" } 
  };

  ReportsList.DataSource = myItems;

  ReportsList.DisplayMember = "Name";

  ReportsList.ValueMember = "Id";
}

Then when you reference the ListItem you can do .ValueMember to get the Id to use for your unique reference.

So in your case, instead of just adding a string of the path, you should create a new object with two properties, Id and Path or whatever, and add instead (since .Add takes an Object). Then tell it which property to display and which is the value for looking up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM