在C#中没有触发事件修改ComboBox SelectedIndex [英] Modifying ComboBox SelectedIndex Without Triggering Event in C#

查看:461
本文介绍了在C#中没有触发事件修改ComboBox SelectedIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#应用​​程序有一个 comboBox ,带有一个 SelectedIndexChanged 事件。通常,我想要这个事件开火,但有时我需要事件不开火。我的 comboBox 是MRU文件列表。如果发现列表中的文件不存在,则该项目将从 comboBox 中删除​​,而 comboBox code> SelectedIndex 设置为零。但是,将 comboBox SelectedIndex 设置为零会导致 SelectedIndexChanged 事件触发,在这种情况下是有问题的,因为它会导致在事件处理程序中运行一些UIF代码。有没有一个优雅的方法来禁用/启用C#表单控件的事件?谢谢。

My C# application has a comboBox with a SelectedIndexChanged event. Usually, I want this event to fire, but but sometimes I need the event to not fire. My comboBox is an MRU file list. If a file in the list is found to not exist, the item is removed from the comboBox, and the comboBox SelectedIndex is set to zero. However, setting the comboBox SelectedIndex to zero causes the SelectedIndexChanged event to fire, which in this case is problematic because it causes some UIF code to be run in the event handler. Is there a graceful way to disable/enable events for C# form controls? Thanks.

推荐答案

使用

ComboBox combo = sender as ComboBox;
if (combo.SelectedIndex == 0)
{
    return;
}

如果问题是使用不同的事件处理程序,您可以删除事件处理程序的事件注册首先。

If you're issue is with a different eventhandler you could remove the eventhandler's event registration first.

combo.SelectedIndexChanged -= EventHandler<SelectedIndexChangedEventArgs> SomeEventHandler;
combo.SelectedIndex = 0;
combo.SelectedIndexChanged += EventHandler<SelectedIndexChangedEventArgs> SomeEventHandler;

这篇关于在C#中没有触发事件修改ComboBox SelectedIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆