I have this event handler which I need to, under certain conditions change it's selected item in code. When I do this it will recall the handler due to being changed and re-execute. How can I prevent this?
MessageBox.Show("Must have a repair report.", "No Report");
txtLocation.SelectedItem = MAIN_BACKGROUND.UserName; //here it recalls itself as I return its value to what it was before the change
You could use a global boolean value to stop the event body from performing its substantive work during the "inner" event handler:
private bool _alreadyChanging = false;
private void txtLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!_alreadyChanging)
{
_alreadyChanging = true;
MessageBox.Show("Must have a repair report.", "No Report");
txtLocation.SelectedItem = MAIN_BACKGROUND.UserName;
_alreadyChanging = false;
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加