Note that there are some explanatory texts on larger screens.

plurals
  1. POC# databinding on combobox
    primarykey
    data
    text
    <p>I can't understand the following 2 issues given this code. I mapped a combobox to a custom object and I want each time that the selected value change on the combobox, the custom object changes too.</p> <pre><code>public partial class MainForm : Form { private Person _person; public MainForm() { InitializeComponent(); _person = new Person(); //Populating the combox, we have this.comboBoxCities.DataSource = this.cityBindingSource; cityBindingSource.Add(new City("London")); cityBindingSource.Add(new City("Paris")); _person.BirthCity = new City("Roma"); cityBindingSource.Add(_person.BirthCity); cityBindingSource.Add(new City("Madrid")); //Doing the binding comboBoxCities.DataBindings.Add("SelectedItem", _person, "BirthCity"); } private void buttonDisplay_Click(object sender, EventArgs e) { MessageBox.Show("BirthCity=" + _person.BirthCity.Name); } private int i = 0; private void buttonAddCity_Click(object sender, EventArgs e) { City city = new City("City n°" + i++); cityBindingSource.Add(city); comboBoxCities.SelectedItem = city; } } public class Person { private City _birthCity; public City BirthCity { get { return _birthCity; } set { Console.WriteLine("Setting birthcity : " + value.Name); _birthCity = value; } } } public class City { public string Name { get; set; } public City(string name) { Name = name; } public override string ToString() { return Name; } } </code></pre> <p>1 - why when I manually select twice in a row (or more) different value on the combobox, I got only one call to BirthCity.Set witht he last selected value (and the call seems firing only when the combobox lost the focus) ?</p> <p>2 - why when I click buttonAddCity and then buttonDisplay, the diplayed city is not the one selected (not the one displayed in the comobox )</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload