Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式选择索引0(从-1)时,组合框未触发SelectedIndexChanged_C#_.net_Combobox_.net 4.0 - Fatal编程技术网

C# 以编程方式选择索引0(从-1)时,组合框未触发SelectedIndexChanged

C# 以编程方式选择索引0(从-1)时,组合框未触发SelectedIndexChanged,c#,.net,combobox,.net-4.0,C#,.net,Combobox,.net 4.0,我有一个组合框,我使用SelectIndexChanged事件捕获用户和编程更改 清除并重新加载绑定到combobox的列表将触发索引为-1的eventhandler 但是,使用selectedindex=-1 combobox1.SelectedIndex = 0 ; // will NOT fire the event. 但是 在这两种情况下,我都以编程方式更改selextedindex,并期望触发事件 我用一种简单的形式验证了这种行为 namespace cmbTest { pu

我有一个组合框,我使用SelectIndexChanged事件捕获用户和编程更改

清除并重新加载绑定到combobox的列表将触发索引为-1的eventhandler

但是,使用selectedindex=-1

combobox1.SelectedIndex = 0 ; // will NOT fire the event.
但是

在这两种情况下,我都以编程方式更改selextedindex,并期望触发事件

我用一种简单的形式验证了这种行为

namespace cmbTest
{
    public partial class Form1 : Form
    {
        private BindingList<string> items = new BindingList<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = items;
            loadItems();
        }

        private void loadItems()
        {
            items.Add("chair");
            items.Add("table");
            items.Add("coffemug");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Fired with selected item index:" + comboBox1.SelectedIndex);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int index = comboBox1.SelectedIndex;

            // Clear and reload items for whatever reason.
            items.Clear();
            loadItems();

            // try select old item index; if it doesnt exists, leave it.
            try { comboBox1.SelectedIndex = index; }
            catch { }
        }





    }
}
名称空间测试
{
公共部分类Form1:Form
{
私有BindingList项=新建BindingList();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
comboBox1.DataSource=项目;
loadItems();
}
私有void loadItems()
{
项目。添加(“主席”);
项目。添加(“表格”);
项目。添加(“咖啡豆”);
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
MessageBox.Show(“使用所选项目索引激发:+comboBox1.SelectedIndex”);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
int index=comboBox1.SelectedIndex;
//出于任何原因清除并重新加载项目。
items.Clear();
loadItems();
//尝试选择旧项目索引;如果它不存在,请保留它。
请尝试{comboBox1.SelectedIndex=index;}
捕获{}
}
}
}
表单有一个组合框1和一个按钮1

为清晰起见进行编辑(我希望):

  • 运行程序
  • 选择“椅子”。消息“使用所选项目索引激发:0”
  • 按按钮。消息“使用所选项目索引激发:-1”
  • 选择“表格”。消息“使用所选项目索引激发:1”
  • 按按钮。消息“使用选定项索引激发:-1”和 “使用所选项目索引触发:1”
  • 当“chair”也被选中时,我希望在点击按钮时得到两条消息,因为我通过编程将索引更改为0


    那么,为什么这不能像我期望的那样工作,什么是可接受的解决方法?

    当您的第一个项目添加到项目集合时,索引会自动更改为0。这就是为什么以前的索引保存为0,然后使用以下行再次设置它的原因
    comboBox1.SelectedIndex=index,索引未更改。这就是事件未被触发的原因

    查看ComboBox的源代码,有两种情况下不会触发该事件:要么抛出表达式,要么将索引设置为与原来相同的值

    如果您真的想对此进行一些破解,可以这样做:

    int index = comboBox1.SelectedIndex;
    
    // Clear and reload items for whatever reason.
    items.Clear();
    loadItems();
    
    if(comboBox1.SelectedIndex == index)
    {
        comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
        comboBox1.SelectedIndex = -1;
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
    }
    
    // try select old item index; if it doesnt exists, leave it.
    try { comboBox1.SelectedIndex = index; }
    catch
    {
    }
    

    将第一项添加到items集合时,索引将自动更改为0。这就是为什么以前的索引保存为0,然后使用以下行再次设置它的原因
    comboBox1.SelectedIndex=index,索引未更改。这就是事件未被触发的原因

    查看ComboBox的源代码,有两种情况下不会触发该事件:要么抛出表达式,要么将索引设置为与原来相同的值

    如果您真的想对此进行一些破解,可以这样做:

    int index = comboBox1.SelectedIndex;
    
    // Clear and reload items for whatever reason.
    items.Clear();
    loadItems();
    
    if(comboBox1.SelectedIndex == index)
    {
        comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
        comboBox1.SelectedIndex = -1;
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
    }
    
    // try select old item index; if it doesnt exists, leave it.
    try { comboBox1.SelectedIndex = index; }
    catch
    {
    }
    

    接受例外?那不好。检查是否引发异常。是否引用捕获{}?try语句仅在重载列表不再具有项索引时使用。这里不是这种情况,本例也不需要它。我看不到
    comboBox1.SelectedIndex=0在您的代码中。问题到底出在哪里?当您运行程序并选择索引0(椅子),然后按下按钮时。当再次选择索引0时,它不会触发事件。我无法真实再现您的问题。您能否指定导致此问题的确切步骤?接受异常?那不好。检查是否引发异常。是否引用捕获{}?try语句仅在重载列表不再具有项索引时使用。这里不是这种情况,本例也不需要它。我看不到
    comboBox1.SelectedIndex=0在您的代码中。问题到底出在哪里?当您运行程序并选择索引0(椅子),然后按下按钮时。当再次选择索引0时,它不会触发事件。我无法真实再现您的问题。你能具体说明导致这个问题的具体步骤吗?你是对的。再次设置列表时,所选索引将设置为0。奇怪的是,我的调试窗口在设置为0之前仍然显示为-1。但是,在尝试将所选索引设置回0之前,一个额外的消息框清楚地说明了所选索引已经是0。它给了我一些som提示,但由于我的事件处理程序总是跳过-1,我可能会在loaditems方法中将所选项强制为-1。@NickSick这不起作用。试试看。您需要使用“取消订阅/订阅”活动才能使其正常工作。跟我一样。如果你觉得这个答案有帮助,你可以把它标为接受。你是对的。再次设置列表时,所选索引将设置为0。奇怪的是,我的调试窗口在设置为0之前仍然显示为-1。但是,在尝试将所选索引设置回0之前,一个额外的消息框清楚地说明了所选索引已经是0。它给了我一些som提示,但由于我的事件处理程序总是跳过-1,我可能会在loaditems方法中将所选项强制为-1。@NickSick这不起作用。试试看。您需要使用“取消订阅/订阅”活动才能使其正常工作。跟我一样。如果你觉得这个答案有帮助,你可以把它标记为接受。