views:

1000

answers:

2

Hello,

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.

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; }
}

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) ?

2 - why when I click buttonAddCity and then buttonDisplay, the diplayed city is not the one selected (not the one displayed in the comobox )

A: 

You should override GetHashCode() and Equals() methods.

Rock
why I need this ?
Toto
+1  A: 
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) ?

That's how Data Binding works, data is moved from the control to the property when validation occurs, and validation occurs when the control loses focus.

why when I click buttonAddCity and then buttonDisplay, the diplayed city is not the one selected (not the one displayed in the comobox )

I don't know. I created a simple form (Visual C# Express 2008 using .Net 3.5 SP1) and pasted your code pretty much verbatim, and it works as expected: it shows the new city in the combo box.

If you add comboBoxCities.Focus (); to the end of buttonAddCity_Click (), you'll make sure the new city gets pushed into _person.BirthCity earlier rather than on ValidateChildren ().

XXXXX
Thx it was exactly what I didn't know. Do you have a link explaining how databinding works ? Other question : it works perfectly if I add the comboboxCities.focus() as suggested but it does not work with a call to validatechildren insteed, why ?
Toto
<blockquote>Do you have a link explaining how databinding works ?</blockquote>Sadly no. I'm just now teaching myself C# using Visual Studio Express, and the documentation is rather sparse. I'm reading "Data Binding with Windows Forms 2.0" by Noyes, but that's mostly concerned with making data binding work with databases.
XXXXX
2: If it "works perfectly" when you add SetFocus (), then you probably mean in your original question that the new city is not pushed to the property when you add it, rather than not appearing in the combo box when you add it. Again, the problem is that if you don't call SetFocus (), the combo box can't *lose* the focus causing validation and then binding.
XXXXX
3: You should be able to call ValidateChildren () on the containing control (a Form, UserControl or whatnot) which calls the validation and binding on all the contained controls. The FormClose event is one obvious place to call it, but there are others.
XXXXX
to 2 : indeed the problem was that the new city was not pushed -----to 3 : ok I will try to find a good place to call validatechildren() since calling SetFocus is not really good, thx
Toto