[英]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.