简体   繁体   English

Winforms在列表框中显示字典中的信息(按值排序)

[英]Winforms displaying information from dictionary in a Listbox (sorting by value)

So, I've got a Sorted Dictionary where my keys are first names & values are last names. 因此,我有一个排序字典,其中我的键是名字,值是姓氏。

I have a list-box on my form that displays all possible Values from which the user can pick a last name. 我的表单上有一个列表框,显示所有可能的值,用户可以从中选择姓氏。 However, since first-names are the Keys, these values displayed in the list box aren't sorted. 但是,由于名字是键,因此列表框中显示的这些值不会排序。

Andrews -> Carter -> Johnson

Carter -> Johnson -> Andrews

I want the last names to be alphabetically arranged as in the first line, rather than how they are now (the second). 我希望按第一行的字母顺序排列姓氏,而不要像现在这样(第二行)。

Would creating a List that only contains the Values be the best way to do this? 创建仅包含值的列表是执行此操作的最佳方法吗? I couldn't find any way to internally sort the contents of a ListBox, so I was at a loss for what else I could do. 我找不到任何对内部ListBox内容进行排序的方法,因此我无所适从。

You could create another list in the correct order, by something like: 您可以按以下正确顺序创建另一个列表:

// Given that 'Person' for the sake of this example is:

public class Person
{
    public string FirstName;
    public string LastName;
}

// And you have a dictionary sorted by Person.FirstName:

var dict = new SortedDictionary<string, Person>();
// ...initialise dict...

// Make list reference a List<Person> ordered by the person's LastName

var list = (from entry in dict
            orderby entry.Value.LastName
            select entry.Value).ToList();

// Use list to populate the listbox

This has the advantage of leaving the original collection unmodified (if that's important to you). 这样做的好处是可以保持原始集合不变(如果这对您很重要)。

The overhead of keeping references to the objects in two different collections is not very high, because it's only one reference per object (plus some fixed overhead for the List<> implementation itself). 在两个不同的集合中保留对对象的引用的开销不是很高,因为每个对象只有一个引用(加上List<>实现本身的一些固定开销)。

But remember that the objects will be kept alive as long as either of the collections are alive (unless you explicitly clear the collections). 但是请记住,只要两个集合中的任何一个处于活动状态,这些对象都将保持活动状态(除非您明确清除了这些集合)。


Compilable sample: 编译样本:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    public static class Program
    {
        private static void Main()
        {
            dict = new SortedDictionary<string, Person>();

            addPerson("iain", "banks");
            addPerson("peter", "hamilton");
            addPerson("douglas", "adams");
            addPerson("arthur", "clark");
            addPerson("isaac", "asimov");

            Console.WriteLine("--------------");

            foreach (var person in dict)
                Console.WriteLine(person.Value);

            var list = (from entry in dict
                        orderby entry.Value.LastName
                        select entry.Value).ToList();

            Console.WriteLine("--------------");

            foreach (var person in list)
                Console.WriteLine(person);
        }

        private static void addPerson(string firstname, string lastname)
        {
            dict.Add(firstname, new Person{FirstName = firstname, LastName = lastname});
        }

        private static SortedDictionary<string, Person> dict;
    }

    public class Person
    {
        public string FirstName;
        public string LastName;
        public override string ToString(){return FirstName + " " + LastName;}
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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