0

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

我的问题是,当我单击列表框中的项目时,例如 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
4

1 回答 1

1

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

编辑:如果要从列表框中获取对象,则应将它们作为对象添加到列表框中,例如:

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();
                    }
                }
            }
于 2012-11-25T18:23:44.177 回答