0
votes

Ok so I have two listboxes in one form, the content of one of which is based on which item is selected in the other one. Basically listbox1 is a list of employers, and listbox2 is a list of employees for that selected employer. When I select an employer with no employees, there are no items in listbox2 as expected. But for some reason when I select another employer AFTER selecting the one with no employees, listbox2 refuses to populate with a list of employees again. I have a label next to listbox2 that automatically updates its text based on the selected item in listbox2, and that updates when I click where the items in listbox2 SHOULD be. Almost as though the computer is having issues rendering the listbox. I tried methods like listbox2.Refresh(), but I don't even really know what that does. So basically, the listbox WORKS, it just doesnt SHOW anything. Here is some relevant code:

Code for when selected index in listbox1 is changed:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.DataSource = Program.MEmployerList;
        listBox1.DisplayMember = "Name";
        Employer temps = (Employer)listBox1.SelectedItem;
        listBox2.DataSource = temps.Employees();
        listBox2.DisplayMember = "Name";
        listBox2.Refresh();
        label5.Text = temps.CompanyName() + " (" + temps.EmployerID() + ")";
        label4.Text = "Phone Number: " + temps.PhoneNumber() + "\nCell Number: " + temps.CellNumber() + "\nAdress: " + temps.StreetAdress() + " - " + temps.City() + ", " + temps.State() + " " + temps.ZipCode() + "\nContact Person: " + temps.ContactPerson();
        if (listBox2.Items.Count != 0 && listBox1.Items[0] != null)
        {
            Employee temped = (Employee)listBox2.SelectedItem;
            label4.Text = label4.Text + "\n\nSelected Employee Info: " + temped.Name + "\nPhone Number: " + temped.PhoneNumber() + "\nCell Number: " + temped.CellNumber() + "\nAddress: " + temped.StreetAdress() + " - " + temped.City() + ", " + temped.State() + " " + temped.ZipCode();
        }
    }

Here is the code for when a selected index in listbox2 is changed:

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.DataSource = Program.MEmployerList;
        listBox1.DisplayMember = "Name";
        Employer temps = (Employer)listBox1.SelectedItem;
        listBox2.DataSource = temps.Employees();
        listBox2.DisplayMember = "Name";
        listBox2.Refresh();
        label5.Text = temps.CompanyName() + " (" + temps.EmployerID() + ")";
        label4.Text = "Phone Number: " + temps.PhoneNumber() + "\nCell Number: " + temps.CellNumber() + "\nAdress: " + temps.StreetAdress() + " - " + temps.City() + ", " + temps.State() + " " + temps.ZipCode() + "\nContact Person: " + temps.ContactPerson();
        if (listBox2.Items.Count != 0 && listBox1.Items[0] != null)
        {
            Employee temped = (Employee)listBox2.SelectedItem;
            label4.Text = label4.Text + "\n\nSelected Employee Info: " + temped.Name + "\nPhone Number: " + temped.PhoneNumber() + "\nCell Number: " + temped.CellNumber() + "\nAddress: " + temped.StreetAdress() + " - " + temped.City() + ", " + temped.State() + " " + temped.ZipCode();
        }
    }

Let me know if there is any other code that would help solve this issue.

UPDATE: Keep in mind the fact that the program is registering the fact that I am selecting different items in listbox2, the items are just not showing, which is leading me to believe it is a rendering issue. I click where entries in listbox2 SHOULD be, and label4 updates.

2
Hehe... noob coder here, what do you mean?Kartik
When I was doing my first semi-large project it helped me a lot. It is a way to interact with data sources. msdn.microsoft.com/en-us/library/bb386976.aspxThe_Cthulhu_Kid

2 Answers

1
votes

Are you missing this at the end?

listBox1.DataBind()
0
votes

Figured it out.

I just had to hide the control and then show it again to force it to be re-drawn and re-populated. Seemed to be a rendering issue as the list contents were being changed but not re-drawn.

listbox1.Hide();
listbox1.Show();