C # ListBox update when an item changes

I have a list of custom objects that I added to a ListBox control in a WinForms C # 4.0 application.

When the user selects a specific item in the ListBox, the properties of this object appear in the window next to the ListBox in different input fields. The user can change them and click "Save" to change the data elements of the objects in accordance with the changes made by the user.

Function works. The values ​​are stored in the object, and when the user selects the element again, their changes confirm that they are saved correctly.

What doesn't work is updating the text in the ListBox. For example, if we have a list of employees in the ListBox, and we can see "John Smith" there, we can click on his name - edit his name on "John Smithe" and click "OK." The ListBox still shows "John Smith", however, if we click on his name, we will see in the text blocks on the right that his name has been correctly changed to "John Smithe".

I tried calling the Refresh () method on the ListBox, but that didn't work.

I can fix this by removing the item from the ListBox and adding it again. This works, and this is not a problem, because the items are stored in separate lists anyway, so I cannot lose any of my employees.

But is this really the best way to do this? Is there a more elegant way to update text in a ListBox without deleting / adding an item again?

+3
source share
2 answers

Do objects execute in ListBox INotifyPropertyChanged?

Update:

It seems that you can solve the problem with a few steps:

  • Set a DisplayMemberproperty to a ListBoxproperty on your objects that provides everything that you want to display in the list. I assume this property is called for this answer DisplayText.
  • Have objects INotifyPropertyChanged.
  • In the settings for all properties that affect the value DisplayText, raise an event NotifyPropertyChangedwith DisplayTextfor the property name.

Then you should go well.

+3
source

Following the above guide, I made a quick and dirty example of using a BindingList. Hope this helps you.

public partial class Listbox_Databinding : Form
{
    BindingList<Person> People = new System.ComponentModel.BindingList<Person>();

    public Listbox_Databinding()
    {
        InitializeComponent();

        People.Add(new Person("John", "Smith"));
        People.Add(new Person("John", "Jacob"));

        lstSelectPerson.DataSource = People;

    }

    private void lstSelectPerson_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtLast.Text = ((Person)lstSelectPerson.SelectedItem).Last;
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        ((Person)lstSelectPerson.SelectedItem).Last = txtLast.Text;
    }
}

public class Person : INotifyPropertyChanged
{
    public Person(string first, string last)
    {
        First = first;
        Last = last;
    }

    public override string ToString()
    {
        return Last + ", " + First;
    }

    string p_first;
    string p_last;

    public string First
    {
        get { return p_first; }
        set
        {
            p_first = value;
            OnDisplayPropertyChanged();
        }
    }

    public string Last
    {
        get { return p_last; }
        set
        {
            p_last = value;
            OnDisplayPropertyChanged();
        }
    }

    void OnDisplayPropertyChanged()
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("DisplayName"));
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}
+2
source

All Articles