ListBox,选择索引更改

时间:2012-11-25 17:46:27

标签: c# list listbox

一个用于访问,一个用于提取,当用户将任一访问或提取添加到列表时,它也会添加到列表框中以显示给用户。

我的问题是当我点击列表框中的项目时,例如Indexchanged,我希望它打开与访问或交付相关的新GUI,因此,如果他们点击访问它打开访问表单,如图所示在我的代码中,但我怎么能让它区分列表,以便它知道打开哪个表单?

 private void lstVisits_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Allow the user to click on the listbox to open a visit
        //This event is called after the user has clicked on the list
        int index = lstVisits.SelectedIndex;
        //Get the index of the Visit that the user has clicked upon

        Visits selected = theList.getVisits(index);
        //Get the visits object from the list

        Visitsform.visits = selected;
        //Ensure that the appointment form references the selected visit

        Visitsform.ShowDialog();
        //Show the visits form

        updateList();
        //update the list as the user may have deleted the appointment

1 个答案:

答案 0 :(得分:1)

如果两个列表中的项目都存储在同一个listBox中,那么您可以使用以下内容:

编辑:如果你想从listBox中获取对象,那么你应该将它们作为对象添加到listBox中,例如:

Visits v = new Visit();
Pickups p = new Pickup();
lstVisits.Items.Add(v);
lstVisits.Items.Add(p);    


private void lstVisits_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (listBox1.SelectedItems.Count > 0)
                {
                    object o = listBox1.SelectedItems[0];
                    if (o is Visits)
                    {
                        Visits visit = (Visits)o;
                        Visitsform.visits = visit;
                        Visitsform.ShowDialog();
                    }
                    else
                    {
                        Deliveries delivery = (Deliveries)o;
                        Deliveriesform.visits = visit;
                        Deliveriesform.ShowDialog();
                    }
                }
            }