0

I struggle with selecting proper item in winforms combobox. Previously I described in detail here but I think that problem remain unsolved cause I use one form for add/edit record. so on form load I have

  private void AddEditForm_Load(object sender, EventArgs e)
  {
      PopulateComboBoxLanguage();
  }

private void PopulateComboBoxLanguage()
{
    comboBoxLang.DataSource = Enum.GetValues(typeof(Book.EnumLang));
}

and on edit action I want to populate form with existing data and everything is populated as it should except combobox where first item from EnumLang is always displayed. from my second constructor I call PopulateWithExisingData(book) where I use

comboBoxLang.SelectedItem = book.Language;

but even when passed book.Language is set to German SelectedItem is always null on debug mode.

p.s. I tried with comboBoxLang.SelectedItem = (book.EnumLang)book.Language; also with SelectedValue but remains the same.

Once more I guess that problem is on populating combobox on page load but I don't know is it and how to fix that.

Please ask for more info.

Community
  • 1
  • 1
user2783193
  • 992
  • 1
  • 12
  • 37
  • 1
    `comboBoxLang.SelectedItem = book.Language;` where do you have this line? – Sriram Sakthivel Oct 18 '13 at 17:33
  • inside constructor I'm sending book object which has besides other properties Language property. On page load I'm populating combobox values but I need to selected proper not default one. – user2783193 Oct 18 '13 at 17:34
  • @SriramSakthivel inside PopulateWithExisingData(book) method on edit action – user2783193 Oct 18 '13 at 17:35
  • @user2783193 please use the language of `winforms`, not `asp.net`, `winforms` doesn't have any the so-called `page load`. – King King Oct 18 '13 at 17:36
  • before calling `PopulateWithExisingData` call `PopulateComboBoxLanguage` and remove it from FormLoad – Sriram Sakthivel Oct 18 '13 at 17:38
  • @SriramSakthivel But I do need populated combobox with action new record, that's why I'm put in form load. PopulateWithExistingData populates form fields with existing data (just on edit action). – user2783193 Oct 18 '13 at 18:29

2 Answers2

2
  1. Declare an instance of the object type you are adding/editing in your form.
  2. Add a bool isEdit to the form and set it to false
  3. Add a method public void Initialize(ObjectType name)
  4. Your Initialize method should set the form instance equal to the parameter and it should set a boolean flag isEdit = true.
  5. Put all your code that loads data/populates controls (like your combobox) in your forms load event.
  6. At the bottom of your load event, after your controls are populated do

        if (isEdit)
        {
            //Set your controls selected values from the object you are editing
        }
    

Now, for new objects, just make your form and call a Show or a ShowDialog on it. This will cause the Load event to fire and your controls will populate.

For edits, make your form, call Initialize, THEN do the Show/ShowDialog. Since your Initialize method sets isEdit = true, the if(isEdit) block of code at the bottom of your load event will be hit and the controls values will be set equal to the properties of the object you are editing.

Here is some very simple example code:

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //This is simulating an add...First Language will be displayed on form2, 
        //which is English
        Form2 form = new Form2();
        form.ShowDialog();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //This is simulating an edit...this will display french 
        //(or whatever is passed in)
        Form2 form = new Form2();
        form.Initialize(Languages.French);
        form.ShowDialog();
    }

    Languages editValue;
    bool isEdit = false;

    public Form2()
    {
        InitializeComponent();
    }

    public void Initialize(Languages var)
    {
        editValue = var;
        isEdit = true;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        comboBox1.DataSource = Enum.GetValues(typeof(Languages));

        if (isEdit)
        {
            comboBox1.SelectedItem = editValue;
        }
    }

public enum Languages
{
    English = 0,
    French = 1,
    Spanish = 2,
    German = 3
}
Scope Creep
  • 547
  • 3
  • 15
1

When you set DataSource, you pass an array of objects. When you set SelectedItem, you pass an enum value, so it gets boxed to an object again. ComboBox searches for your item among the DataSource values, using method IndexOf, which uses method Object.Equals to compare those values against your new value. And since they are different objects (the references differ), your item is never found in the DataSource collection, so the selection is not changed.

JustAndrei
  • 863
  • 4
  • 17