0
votes

What i Am Trying to do is Add Items into listbox1 and when i specific item is selected it shows more info in another list box

So Here an Example:

Listbox1 Has a Person Bob When Bob is Selected His Phone Number Shows up on Listbox2

You Can Also Add a Phone Number to the Selected Item. When another Item is Selected Bob Phone Number Disappears and shows the Next Selected Name and Phone

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

Here is what i have (Not Sure if it is right or wrong NEW to c#)

Person.cs

string FirstName;
        string PhoneNumber;

        public Person(string FName, string PNumber)
        {

            FirstName = FName;
            PhoneNumber = PNumber;

        }

Organisation.cs

string Name;

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

    }

    public override string ToString()
    {
        return Name;
    }

Button Click Event

  private void button1_Click(object sender, EventArgs e)
        {
            NewOrgg = new Organisation();
            NewOrgg.OrggName = textBox1.Text;
            listBox1.Items.Add(NewOrgg);
        }
2
how you load data to listbox1 and listbox2?Damith

2 Answers

0
votes

I think, if it's possible for you, that, for more simplicity, to change the Person class like this: Add a link to the organisation that the Person belongs to:

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

Ok, for now we have our Person and Organisation class. In the main.cs of the project, I think you can do something like that:

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

Now we get our Providers. Let fill them with some data.

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

Now we are 90% ready :D. We just have to handle the item change event in our ListBox ( SelectedIndexChanged ) as said before.

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

And that's It, we made it :D let me know if it's (or not) what you are watting for.

0
votes

You can use SelectedIndexChanged event

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   string curItem = listBox1.SelectedItem.ToString();
   //clear and add to listBox2
}