te7
te7

Reputation: 609

ComboBox SelectedIndexChanged event not firing

I am using VS 2017 for an Visual C# application (.Net 4.6.2, 32 bit) that calls a form from the main form. In that second form the SelectedIndexChanged event is not firing for one of the ComboBoxes. Following is the code. If I have to register the event I don't know how. I originally had copied and pasted the ComboBoxes onto the form. Then I deleted that control and re-added the ComboBoxes from the ToolBox. Any help would be appreciated.

namespace Lottery_C_Sharp
{
    public partial class Dialog_Matches_Input_Lotomania : Form
    {
        MatchesMethods_LM m;
        public string[] lotomania_list = new string[10];
        public string[] pick10_list = new string[5];
        Utilities u;

        public Dialog_Matches_Input_Lotomania(MatchesMethods_LM mm)
        {
            InitializeComponent();

            m = mm;
            u = new Utilities();
            set_combos(); 

            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
        }
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("comboBox3_SelectedIndexChanged");

            if (m.NumCurrLimit == 99)
            {
                set_lotomania_time_text();
                set_lotomania_totals_text();
            }
            else
            {
                set_pick10_time_text();
                set_pick10_totals_text();
            }
        }

        public void set_combos()
        {
            set_lists();

            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();

            if (m.NumCurrLimit == 99)
            {
                textBox1.Text = "Brazilian LotoMania";

                AddToCombo(comboBox1, lotomania_list);
                comboBox1.SelectedIndex = 0;

                AddToCombo(comboBox2, lotomania_list);
                comboBox2.SelectedIndex = 0;

                AddToCombo(comboBox3, lotomania_list);
                comboBox3.SelectedIndex = 0;

                set_lotomania_time_text();
                set_lotomania_totals_text();

            }
            else
            {
                textBox1.Text = "USA New York Pick 10";

                AddToCombo(comboBox1, pick10_list);
                comboBox1.SelectedIndex = 0;

                AddToCombo(comboBox2, pick10_list);
                comboBox2.SelectedIndex = 0;

                AddToCombo(comboBox3, pick10_list);
                comboBox3.SelectedIndex = 0;

                set_pick10_time_text();
                set_pick10_totals_text();
            }
        }

Upvotes: 0

Views: 693

Answers (2)

Genotypek
Genotypek

Reputation: 213

You should register the event for Your desired combobox in Designer properties, like this: How to register event in WinForms Designer

It generates event registration in Form1.Designer.cs (form name depends on how You've named it) auto-generated file:

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);

Then Your function, where You can perform some actions when event occures shows in Form1.cs

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do something
}

Upvotes: 1

Eliahu Aaron
Eliahu Aaron

Reputation: 4552

You have several options to register the event.

Option 1:

Register in code:

public Dialog_Matches_Input_Lotomania(MatchesMethods_LM mm)
{
    InitializeComponent();

    ...

    comboBox1.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}

Option 2:

Double click on comboBox1 in the designer, this will automatically add and register an event.

Option 3:

Select the comboBox1 in the designer, right click and select "Properties", in the properties window select the event icon on top (the lightning symbol), find "SelectedIndexChanged" and enter the name of the event or double click to automatically add and register an event.

Upvotes: 1

Related Questions