Listbox freezes visually after firing SelectedIndexChanged

Viewed 111

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.

1 Answers

i did this with a form and a usercontrol (not a form) and it works:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public List<Phone> PhoneList { get; private set; }

        private void button1_Click(object sender, EventArgs e)
        {
            PhoneList = new List<Phone>();

            PhoneList.Add(new Phone(PhoneList.Count + 1));

           
            listBox1.DataSource = null;
            listBox1.DataSource = PhoneList;
            listBox1.DisplayMember = "Id";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1!=null && listBox1.SelectedIndex>=0)
                ShowCurrentPhone(PhoneList[listBox1.SelectedIndex]);
        }
        public void ShowCurrentPhone(Phone phone)
        {
            panel2.Controls.Clear();
            UserControl1 FramePhone = new UserControl1(phone.Id);
            FramePhone.Name = "Phone " + phone.Id;
            panel2.Controls.Add(FramePhone);
            panel2.BorderStyle = BorderStyle.FixedSingle;
        }
    }

public class Phone
    {
        private int id;
        public string PonheNumber;

        public int Id { get => id; set => id = value; }

        public Phone(int id)
        {
            this.id = id;
            this.PonheNumber = "1111";
        }
    }
   

     public partial class UserControl1 : UserControl
    {
        private int id;
        public UserControl1(int id)
        {
            InitializeComponent();
            this.Id = id;
            label1.Text = Id.ToString();
        }

        public int Id { get => id; set => id = value; }
    }
Related