TAM
TAM

Reputation: 347

ListBox, Select index change

One for visits and one for pickups, When a user adds either of the visits or pickups to the list it is also added into a list box for showing to the user.

My question is when I clicked on the item inside the list box eg Indexchanged, I would like it to open the new GUI that relates to either Visits or delivery's, So if they click on a Visit it opens the visit form as shown bellow in my code, but how can I get it to differentiate between the lists so that It knows which form to open up?

 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

Upvotes: 0

Views: 3789

Answers (1)

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

If items from both lists are stored in the same listBox, then you could use something like this:

EDIT:If you want to get the objects from the listBox, then you should add them as objects to the listBox, for example:

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();
                    }
                }
            }

Upvotes: 1

Related Questions