簡體   English   中英

以編程方式更改索引時,不會觸發ComboBox.SelectedIndexChanged

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

我正在使用C#.net中的Windows窗體。 我在表單上有2個ComboBox和3個TextBox 當我更改ComboBox1的值(通過與UI中的項目進行交互)時,它會更改ComboBox2的項目和所選項目。

當所選的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;
        }

我試圖簡化這個,希望我沒有過度簡化。 如上所述,我從XML文件中獲取數據, 理想情況下 ,目標是使用此表單來讀取和寫入XML文件。 在我看來,所有的XML工作正常,所以我把所有這些都留下了。

兩個框的SelectedIndexChanged事件都綁定到上面的代碼。 會發生什么事是當我改變的價值ComboBox1的值ComboBox2被改變,並且所選的項目被清除,但ComboBox2.SeletedIndexChanged事件從來沒有發射(breakpoins告訴我,代碼時鍾永遠不會重新進入),所以沒有任何TextBox使用新數據進行更新。

我希望一切都有道理,而somone可以幫助我弄清楚我做錯了什么。

事件處理程序只是一種方法,因此您可以調用它。 如果您的處理程序名為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM