0

I'm populating a combobox from a datasource and I have code for when the user changes the selection in the combobox. So obviously I don't want the code in the SelectedIndexChanged method to fire on form load.

This SO question was answered by suggesting two things:

1) Before and after loading the data to the combobox use this code:

private void LoadYourComboBox()
{
    this.comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
        // Set your bindings here . . .
    this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}

I tried that with this code:

this.cboSelectCategory.SelectedIndexChanged -= new EventHandler(cboSelectCategory_SelectedIndexChanged);

However, the cboSelectCategory_SelectedIndexChanged part has a red error squiggly and hovering over it says: The name cboSelectCategory_SelectedIndexChanged does not exist in the current context. I tried that code in both the form_load and the method that actually populates the combobox.

2) That same SO question had the answer to use the event SelectedIndexChangeCommitted.

private void cboSelectCompany_SelectedIndexChangeCommitted(object sender, EventArgs e)
{
    string selectedCat = cboSelectCategory.SelectedValue.ToString();
    Console.WriteLine(selectedCat);
}

But that event isn't firing when I change the selection in the combobox.

Am I missing something somewhere? Is my code off or in the wrong place?

marky
  • 4,878
  • 17
  • 59
  • 103
  • 1
    Do you have a method called `cboSelectCategory_SelectedIndexChanged` in your code? – LarsTech Mar 18 '20 at 18:56
  • @LarsTech - I added the event, added the code to remove/add the event in the form load event and added this in the SelectedIndexChanged event: `Console.WriteLine("This shouldn't fire on form load");` The event did fire on form load - but no errors. – marky Mar 18 '20 at 19:10
  • 1
    Maybe if you showed us YOUR code rather than code from another SO question we could be of more help! – Caius Jard Mar 18 '20 at 19:16
  • @Caius & LarsTech - sorry, guys - I don't know what I did wrong/different, but adding the SelectedIndexChanged event is now being prevented from firing on form load so it's working as needed. Is it Monday?? – marky Mar 18 '20 at 19:21
  • `SelectedIndexChangeCommitted` (in the title) is a curious mix of `SelectedIndexChanged` and `SelectionChangeCommitted`. The latter event is only raised when you *manually* change an Item, while is not raised when you fill the List and the Index *may* be set to `-1` (something that you always check when subscribing to the former event) – Jimi Mar 18 '20 at 20:43
  • @Jimi - thanks for the tip – marky Mar 18 '20 at 20:53

1 Answers1

1

So obviously I don't want the code in the SelectedIndexChanged method to fire on form load.

If you bind your combobox in the form's constructor (after InitializeComponent()) then SelectedIndexChanged will fire before the form is visible, so you can simply return from the selectedindexchanged event if the form is invisible:

    public MainForm()
    {
        InitializeComponent();

        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Code");


        dt.Rows.Add("Milk", "MLK");
        dt.Rows.Add("Bread", "BRD_WHITE");
        dt.Rows.Add("Bread", "BRD_BROWN");
        dt.Rows.Add("Coffee", "COFF");

        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Code";

    }


    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!this.Visible)
            return;

        MessageBox.Show("a");
    }

It's often easier to simply return from an event handler at an unwanted time than to mess around trying to remove and add event handlers

Side note: If you use a strongly typed dataset and create the bindings using the windows forms designer, the event doesn't fire, I believe because the forms designer InitializeComponent() calls Begin/EndInit on the components at the start and end

Caius Jard
  • 72,509
  • 5
  • 49
  • 80