I have a Forms Application structured like this:
+--------------------------------------------------------------+
| FormMain |
|+------------------------+-----------------------------------+|
|| Panel ObjectCreation | Panel ObjectShow ||
|+------------------------+-----------------------------------+|
|| Button AddObject() | listPhones[selectedItem].Id ||
|| ListBox listBoxPhones | ||
|+------------------------+-----------------------------------+|
+--------------------------------------------------------------+
Both Panels display two seperate Forms, FormObjectCreation and FormObjectShow. As soon as the ListBox fires the SelectedIndexChanged Event, the Panel ObjectShow gets updated.
However, afterwards, the listbox does not update anymore visually. It still functions though, still firing events. The listbox just seems to be frozen visually.
this is the AddObject() Function called by the button:
private void buttonAddObject_Click(object sender, EventArgs e)
{
PhoneList.Add(new Phone(PhoneList.Count + 1));
buttonSentTokensToPhone.Visible = true;
listBoxPhones.DataSource = null;
listBoxPhones.DataSource = PhoneList;
}
Here the Function handling the raised Event
private void listBoxPhones_SelectedIndexChanged(object sender, EventArgs e)
{
this.FormMain.ShowCurrentPhone(PhoneList[listBoxPhones.SelectedIndex]);
}
// In FormMain.cs:
public void ShowCurrentPhone(Phone phone)
{
panelPhone.Controls.Clear();
FramePhone = new FrmPhone(this, phone) { Dock = DockStyle.Fill, TopLevel = false, TopMost = true };
FramePhone.Name = "Phone " + phone.Id;
FramePhone.FormBorderStyle = FormBorderStyle.None;
panelPhone.Controls.Add(FramePhone);
panelPhone.BorderStyle = BorderStyle.FixedSingle;
FramePhone.Show();
}
I tried debugging it, however I couldn't find anything abnormal. I'm on .NET 4.7.2. Did anyone encounter this issue too?
UPDATE:
Following Jimis comment below, I changed the PhoneList
from a regular List to a BindingList. The code piece looks now like this:
private BindingList<Phone> PhoneList { get; set; }
private void buttonAddPhone_Click(object sender, EventArgs e)
{
PhoneList.Add(new Phone(PhoneList.Count + 1));
buttonSentTokensToPhone.Visible = true;
}
The ListBox is now working again as intended.