ComboBox.SelectedIndexChanged 以编程方式更改索引时不会触发
ComboBox.SelectedIndexChanged does not fire when programatically changing the index
我正在使用 C#.net 中的 Windows 表单。
我在表格上有 2 ComboBox
和 3 TextBox
。当我更改 ComboBox1
中的值(通过与 UI 中的项目交互)时,它会更改 ComboBox2
.
中的项目和选定项目
当 ComboBox2
的选定索引更改时,它 应该 更改所有 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
事件(断点向我展示了代码时钟永远不会重新输入),因此 TextBox
的 none 会用新数据更新。
我希望一切都是有道理的,希望有人能帮助我找出我做错了什么。
事件处理程序只是一个方法,因此您可以直接调用它。如果你的处理程序被命名为 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());
}
...
通过调用
在 [=11th=] 方法中自行触发 SelectedIndexChanged 事件
ComboBox2_SelectedIndexChanged(ComboBox2, new EventArgs());
我正在使用 C#.net 中的 Windows 表单。
我在表格上有 2 ComboBox
和 3 TextBox
。当我更改 ComboBox1
中的值(通过与 UI 中的项目交互)时,它会更改 ComboBox2
.
当 ComboBox2
的选定索引更改时,它 应该 更改所有 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
事件(断点向我展示了代码时钟永远不会重新输入),因此 TextBox
的 none 会用新数据更新。
我希望一切都是有道理的,希望有人能帮助我找出我做错了什么。
事件处理程序只是一个方法,因此您可以直接调用它。如果你的处理程序被命名为 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());
}
...
通过调用
在 [=11th=] 方法中自行触发 SelectedIndexChanged 事件ComboBox2_SelectedIndexChanged(ComboBox2, new EventArgs());