简体   繁体   English

以编程方式更改索引时,不会触发ComboBox.SelectedIndexChanged

[英]ComboBox.SelectedIndexChanged does not fire when programatically changing the index

I am working with Windows Forms in C#.net. 我正在使用C#.net中的Windows窗体。 I have 2 ComboBox and 3 TextBox on a form. 我在表单上有2个ComboBox和3个TextBox When I change the value in ComboBox1 (by interating with the item in the UI) it changes the items and selected item in ComboBox2 . 当我更改ComboBox1的值(通过与UI中的项目进行交互)时,它会更改ComboBox2的项目和所选项目。

When the selected index of ComboBox2 is changed, it should change the text in all of the TextBox , but it seems that the SelectedIndexChanged is not being fired. 当所选的ComboBox2索引发生更改时,它应该更改所有TextBox中的TextBox ,但似乎没有触发SelectedIndexChanged

public void comboSelectionChanged(object sender, EventArgs e)
    {
        //when the selection changes...
        // 1) cast the sender as a comboBox
        ComboBox cBox = (ComboBox)sender;

        // 2) identify the sender
        if (cBox.Name.ToString() == "ComboBox1")
        {   //this is the 1st combo box                

            //load the children of the new selection into the form
            //child of ComboBox1 is ComboBox2
            ComboBox2.Items.Clear();
            ComboBox2.Text = null;
            string selected = null;

            foreach (string item in {"box2_item1","box2_item2"})
            {
                ComboBox2.Items.Add(item);
            }
            //need to set the selection last, because this will (hopefully) fire the selection changed event on the child

            if (selected != null)
            {//here I am actually getting the selected item from XML
                ComboBox2.SelectedItem = selected;
            }
            else
            {//this should be action that is initiated, which should definitely change the selected index
                ComboBox2.SelectedIndex = -1;
            }
        }
        else if (cBox.Name.ToString() == "ComboBox2")
        {   //this is the 2nd combobox              

            //load the children of the new selection into the form
            //the textBoxes are the children
            TextBox1.Text = "some new text that I am getting from XML";
            TextBox2.Text = "some other new text as above.";
            TextBox3.Text = "same thing, one more time" ;
        }
        else
        {   //I messed something up, because the combobox name is invalid
            Debug.Write("Unreachable code encountered: The combobox name {" + cBox.Name.ToString() + "} is not valid!");
            return;
        }

I've tried to simplify this, and hopefully I haven't overdone it with the simplification. 我试图简化这个,希望我没有过度简化。 As noted, I am getting data from an XML file, and ideally the goal is to use this form to read from and write to the XML file. 如上所述,我从XML文件中获取数据, 理想情况下 ,目标是使用此表单来读取和写入XML文件。 All of the XML seems to me working fine, so I have left all of that out. 在我看来,所有的XML工作正常,所以我把所有这些都留下了。

both of the boxes' SelectedIndexChanged events are tied to the code above. 两个框的SelectedIndexChanged事件都绑定到上面的代码。 What happens is when I change the value of ComboBox1 the values of ComboBox2 are changed, and the selected item is cleared, but the ComboBox2.SeletedIndexChanged event is never fired (breakpoins show me that the code clock is never re-entered), so the none of the TextBox are updated with new data. 会发生什么事是当我改变的价值ComboBox1的值ComboBox2被改变,并且所选的项目被清除,但ComboBox2.SeletedIndexChanged事件从来没有发射(breakpoins告诉我,代码时钟永远不会重新进入),所以没有任何TextBox使用新数据进行更新。

I hope that all makes sense and somone can help me figure out what I am doing wrong. 我希望一切都有道理,而somone可以帮助我弄清楚我做错了什么。

The event handler is just a method so you can just call it. 事件处理程序只是一种方法,因此您可以调用它。 If your handler is named ComboBox2_SelectedIndexChanged you can do: 如果您的处理程序名为ComboBox2_SelectedIndexChanged您可以执行以下操作:

        ...
        else
        {//this should be action that is initiated, which should definitely change the selected index
            ComboBox2.SelectedIndex = -1;
            ComboBox2_SelectedIndexChanged(ComboBox2, new EventArgs());
        }
        ...

comboSelectionChanged(object sender, EventArgs e)方法中通过调用自己comboSelectionChanged(object sender, EventArgs e) SelectedIndexChanged事件

ComboBox2_SelectedIndexChanged(ComboBox2, new EventArgs());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么ComboBox.SelectedIndexChanged事件在加载表单之前触发三次? - Why does ComboBox.SelectedIndexChanged event fire three times before form load? Combobox.SelectedIndexChanged的行为 - Behavior of Combobox.SelectedIndexChanged 不引发ComboBox.SelectedIndexChanged - ComboBox.SelectedIndexChanged is not raised Winforms:哪个事件要触发? combobox.selectedindexchanged或bindingsource.currentchanged - Winforms: Which event to fire? combobox.selectedindexchanged OR bindingsource.currentchanged 当我将值写入ComboBox时,不会触发SelectedIndexChanged事件。 当我从comboBox.Items列表中选择一个值时,它会触发 - SelectedIndexChanged event does not fire when I write a value into the ComboBox. It does fire when I select a value from the comboBox.Items list 如何在 comboBox.SelectedIndexChanged 事件中更改 comboBox.Text? - How do I Change comboBox.Text inside a comboBox.SelectedIndexChanged event? 在后面的C#代码中选择索引时,不会触发DropDownList.SelectedIndexChanged - DropDownList.SelectedIndexChanged does not fire when selecting index in C# code behind 以编程方式选择索引0(从-1)时,组合框未触发SelectedIndexChanged - Combobox not firing SelectedIndexChanged when programmatically selecting index 0 (from -1) 为什么在修改所选项目时,ListBox会触发SelectedIndexChanged事件? - Why does the SelectedIndexChanged event fire in a ListBox when the selected item is modified? 无法为组合框中的单个项目触发SelectedIndexChanged事件 - Unable to fire SelectedIndexChanged Event for a single item in a combobox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM