0

I have a ComboBox that contains a list of names: LastName + ", " + FirstName.

When a name is selected, it populates two text boxes with the First and Last names, respectively.

What I am trying to do is, if the name is changed in the text boxes, I want the change to be updated to the ComboBox as well without having to reload the entire thing. My ComboBox is NOT loaded directly from a database, so I cannot use RefreshItem()

Is this even possible?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Keith Clark
  • 609
  • 1
  • 7
  • 19
  • 2
    Yes, it is. How do you populate your combobox? – Fjut Dec 26 '16 at 20:15
  • I use a ViewModel that I populate, then set it as the DataSource. It contains in int for the index and a string for the displaymember. I populate it using rules that concatenates information from different sources as the text, then assign the DisplayMember, ValueMember and DataSource of the ComboBox – Keith Clark Dec 26 '16 at 20:22
  • i believe this is what you are looking for http://stackoverflow.com/questions/1064109/dynamically-changing-the-text-of-items-in-a-winforms-combobox – Fjut Dec 26 '16 at 20:29
  • 1
    [Connect List to a ListBox and see changes of data source in ListBox](http://stackoverflow.com/questions/33623991/connect-listt-to-a-listbox-and-see-changes-of-data-source-in-listbox) – Reza Aghaei Dec 26 '16 at 23:36

1 Answers1

1

You could implement the INotifyPropertyChanged interface and use a BindingSource as the DataContext of your ComboBox. Please refer to the following sample code.

Person.cs:

public class Person : INotifyPropertyChanged
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; NotifyPropertyChanged(); }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set { _lastName = value; NotifyPropertyChanged(); }
    }

    public string FullName { get { return LastName + ", " + FirstName; } }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Form1.cs:

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

        List<Person> people = new List<Person>()
            {
                new Person() { FirstName = "Donald", LastName = "Duck" },
                new Person() { FirstName = "Mickey", LastName = "Mouse" }
            };
        BindingSource bs = new BindingSource();
        bs.DataSource = people;
        comboBox1.DataSource = bs;
        comboBox1.DisplayMember = "FullName";

        textBox1.DataBindings.Add(new Binding("Text", bs, "FirstName", false, DataSourceUpdateMode.OnPropertyChanged));
        textBox2.DataBindings.Add(new Binding("Text", bs, "LastName", false, DataSourceUpdateMode.OnPropertyChanged));

    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88