Let’s say there is event ComboBox_SelectedIndexChange something like this
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//do something//
}
And i have a function which changes value of ComboBox.
Private void MyFunction()
{
MyComboBox.Text = "New Value";
}
Can i make MyFunction prevent from calling the event MyComboBox_SelectedIndexChanged while changing the value of MyComboBox?
No, you cannot. You have two fundamental options, both of which accomplish the same thing:
You can unhook the event handler method from the control, set the value, and then reattach the event handler method to the control. For example:
You can declare a class-level field that will keep track of whether the value was updated programmatically or by the user. Set the field when you want to update the combo box programmatically, and verify its value in the
SelectedIndexChangedevent handler method.For example: