0

我正在尝试做的是将项目添加到 listbox1 中,当我选择特定项目时,它会在另一个列表框中显示更多信息

所以这里有一个例子:

ListBox1有一个人鲍勃(Bob)选择时,他的电话号码显示在ListBox2

您还可以将电话号码添加到所选项目。当另一个项目被选中时 Bob 电话号码消失并显示下一个选定的姓名和电话

So in My Case When a Organisation is Selected it shows all the names of the people that work in that Organisation

这就是我所拥有的(不确定对 c# 来说是对还是错)

个人.cs

string FirstName;
        string PhoneNumber;

        public Person(string FName, string PNumber)
        {

            FirstName = FName;
            PhoneNumber = PNumber;

        }

组织.cs

string Name;

    public string OrggName
    {
        get 
        {
            return Name;
        }
        set
        {
            Name = value;
        }

    }

    public override string ToString()
    {
        return Name;
    }

按钮点击事件

  private void button1_Click(object sender, EventArgs e)
        {
            NewOrgg = new Organisation();
            NewOrgg.OrggName = textBox1.Text;
            listBox1.Items.Add(NewOrgg);
        }
4

2 回答 2

0

您可以使用SelectedIndexChanged事件

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   string curItem = listBox1.SelectedItem.ToString();
   //clear and add to listBox2
}
于 2013-08-26T03:10:38.580 回答
0

我认为,如果您可以的话,为了更简单起见,像这样更改 Person 类:添加指向 Person 所属组织的链接:

    public string FirstName;
    public string PhoneNumber;
    public string OrggName; //Person's Organisation.

    public Person(string FName, string PNumber, string OName)
    { 
        FirstName   = FName;
        PhoneNumber = PNumber; 
        OrggName    = OName;
    }

好的,现在我们有了 Person 和 Organization 类。在项目的 main.cs 中,我认为您可以执行以下操作:

    //Define the data providers
    List<Organisation> listofOgg; //List of Organisation or maybe Organisation[];
    List<Person> listofPers; //Again List of related Person or maybe Person[];

现在我们得到了我们的 Providers。让他们用一些数据填充它们。

    public void FillThemAll()
    {
       //Initialize some Lists
        listofOgg = new List<Organisation>();
        listofPers = new List<Person>();

        Organisation o = new Organisation();
        o.OrggName = "Stackoverflow";
        listofOgg.Add(o);
        //Another one
        o.OrggName = "Internet"; // Yes I know, I don't have any Organisation name :-)
        listofOgg.Add(o);
        //Now let's handle some Person
        Person p  = new Person("Tash Nar", "0123456", "StackOverFlow");
        Person p2 = new Person("Lionnel", "0123456", "StackOverFlow");
        Person p3 = new Person("You and Me", "0123456", "Internet");
        //Add them
        listofPers.Add(p);  listofPers.Add(p2); listofPers.Add(p3);

        //Now assuming that we have our two displayed ListBox (listbox1 and listbox2)
        //listbox1 for all organisations and listbox2 for more details about organisations
        //Let's fill listbox1 with our data
        for(int i=0; i < listofOgg.Count; i++)
        {
             listbox1.Items.Add(listofOgg[i].Name);
        }  
    }

现在我们准备好了 90% :D。如前所述,我们只需要处理 ListBox ( SelectedIndexChanged ) 中的项目更改事件。

    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
       string curItem = listBox1.SelectedItem.ToString();
       //clear all items in listbox2 
       listBox2.Items.Clear();
       //Add SelectedItem's related Person in listbox2
       foreach(Person pers in listofPers)
       {
          if(pers.OrggName == curItem)
          {
           //Add this person in listbox2
            listbox2.Items.Add(pers.FName);
          } 
       }
    }

就是这样,我们做到了:D 让我知道它是否是(或不是)你想要的。

于 2013-08-26T17:42:40.543 回答