Set default ComboBox without directly changing SelectedIndex

I have a ComboBox with several elements. I want to put SelectedIndexComboBox on 0, so when the user starts it, the first element is already selected (by default).

However, executing this ( combobox.SelectedIndex = 0;) interferes with my event combobox_SelectedIndexChanged(), which occurs when the user changes SelectedIndex from ComboBox, reloading the program:

private void combobox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Process.Start(Application.ExecutablePath);
            this.Close();
        }

This will lead to an infinite t23 loop, since it combobox.SelectedIndex = 0;will call it, which will again call another, etc ...

Is there a way for the program to be able to do something when the user changes SelectedIndex without this loop?

+4
2

( , SelectedIndexChanged).

SelectedValue, . :

public Form1()
{
    InitializeComponent();

    comboBox1.SelectedIndex = 0;
    comboBox1.SelectedIndexChanged += combobox_SelectedIndexChanged;
}

private void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
    Process.Start(Application.ExecutablePath);
    this.Close();
}
+7

internal bool SupressSelectIndexChanged {get; set;}

private void SomeCallingMethod(){
   this.SupressSelectIndexChanged = true;
   combobox.SelectedIndex = 0;
   this.SupressSelectionIndexChanged = false;
}

private void combobox_SelectIndexChanged(object sender, EventArgs e){
    if(this.SupressSelectIndexChanged){ return; }

    // - execution logic
}
0

All Articles