C# - 如何在GUI中使用字典

时间:2016-11-02 15:41:46

标签: c# dictionary

我很难理解用户如何使用GUI为字典添加值。

我设法通过使用列表来实现这一目标:

    List<Person> clients = new List<Person>();


    Person x = new Person();
    x.Name = nameTextbox.text;
    x.Address = addressTextbox.Text;
    clients.Add(x); 

public void AddClientButton_Click(object sender, EventArgs e)
{
    Class Person{

    public string Name{
    get {return Name}
    value { name = value;}

    public string Address{
    get {return Address}
    value { name = Address;}
    }

}

我刚刚输入了这个,因为我不在我的Windows机器上(所以请原谅我所有的错误),但不过它的工作原理。但是,我需要使用一个词典,因为它有一个Key&amp;值。

每个人似乎都自己添加数据,并且在ConsoleApplication中,我需要让用户使用GUI添加数据。我想知道这个概念是否与使用字典相似,还是它们分开了?

 Dictionary<string, string> clients = new  Dictionary<string, string();


        Person x = new Person();
        x.Name = nameTextbox.text;
        x.Address = addressTextbox.Text;
        clients.Add(x); 

    public void AddClientButton_Click(object sender, EventArgs e)
    {
        Class Person{

        public string Name{
        get {return Name}
        value { name = value;}

        public string Address{
        get {return Address}
        value { name = Address;}
        }

    }

有人可以指出我正确的方向,可能使用一个例子,所以我可以掌握这个概念。

谢谢。

1 个答案:

答案 0 :(得分:2)

假设此人的姓名是唯一的

Dictionary<string, Person> clients = new  Dictionary<string, Person>();

 ....

public void AddClientButton_Click(object sender, EventArgs e)
{ 
        Person x = new Person();
        x.Name = nameTextbox.text;
        x.Address = addressTextbox.Text;
        clients.Add(x.Name, x); //Beware, if the name is not unique an exception will be thrown.

}
相关问题