我有一个名为cmbSubjects
的组合框。我的项目的目的是在测验竞赛中改变主题。我希望在更改所选项目时,如果在更改前组合框中有关于主题的未答复问题,则消息框应询问测验主机是否确定是否继续。如果他们回答“否”,则组合框应选择其先前选择的项目。我得到了这个代码,但问题是如果quix master在消息框中选择NO,则消息框会出现两次。我发现原因(在从断点进入代码之后)是当消息框返回NO时,我的代码将组合框的选定值更改为其先前的值,这会触发cmbSubjects_SelectedIndexChanged
事件。我尝试通过添加一个变量来修改代码,以计算消息框出现的次数以防止它。问题是它只在首次更改组合框的值后出现。下面的代码实际上是修改后的代码。谁能帮我这个?提前致谢。 (我正在使用C#)
private void cmbSubjects_SelectedIndexChanged(object sender, EventArgs e)
{
if (pnlAvailable.Controls.Count != 0)
{
if (countMsg < 1)
{
DialogResult res = MessageBox.Show("There are still available questions. Are you sure you want to change the subject?", "Changing subject...", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
if (res == DialogResult.Yes)
{
cmbIndex = cmbSubjects.SelectedIndex;
countMsg = 0;
switch (cmbSubjects.SelectedIndex)
{
case 0:
subject = "life";
break;
case 1:
subject = "math";
break;
case 2:
subject = "physical";
break;
case 3:
subject = "technology";
break;
case 4:
subject = "vocational";
break;
}
GenQstBtns();
}
else if (res == DialogResult.No)
{
countMsg = 1;
cmbSubjects.SelectedIndex = cmbIndex;
}
}
}
}
答案 0 :(得分:4)
您可以使用SelectionChangeCommitted活动。
不要使用SelectedIndexChanged或SelectedValueChanged来捕获用户更改,因为当选择以编程方式更改时也会引发这些事件。
因此,如果您在以编程方式更改所选项目后使用此事件,则此事件将不会再次触发
答案 1 :(得分:1)
是否有可能有多个处理程序连接SelectedIndexChanged事件?您可以在调试器中查看附加到事件的所有处理程序。
答案 2 :(得分:1)
只需针对当前保存的索引检查新索引:
private void cmbSubjects_SelectedIndexChanged(object sender, EventArgs e)
{
if (pnlAvailable.Controls.Count != 0)
{
if (cmbSubjects.SelectedIndex != cmbIndex)
{
DialogResult res = MessageBox.Show("There are still available questions. Are you sure you want to change the subject?", "Changing subject...", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
if (res == DialogResult.Yes)
{
cmbIndex = cmbSubjects.SelectedIndex;
switch (cmbSubjects.SelectedIndex)
{
case 0:
subject = "life";
break;
case 1:
subject = "math";
break;
case 2:
subject = "physical";
break;
case 3:
subject = "technology";
break;
case 4:
subject = "vocational";
break;
}
GenQstBtns();
}
else if (res == DialogResult.No)
{
cmbSubjects.SelectedIndex = cmbIndex;
}
}
}
}
答案 3 :(得分:1)
手动更改时,需要禁止触发IndexChanged事件。
您可以停用该活动:
else if (res == DialogResult.No)
{
countMsg = 1;
cmbSubjects.SelectedIndexChanged -= cmbSubjects_SelectedIndexChanged;
cmbSubjects.SelectedIndex = cmbIndex;
cmbSubjects.SelectedIndexChanged += cmbSubjects_SelectedIndexChanged;
fireEvent = true;
}
答案 4 :(得分:0)
如果我正确理解你要做什么,最简单的事情可能是添加一个bool变量executionSelectedIndexChanged初始化为false。在您的方法中,您可以检查此bool,如果为false,则将其设置为true并打开您的MessageBox。如果答案是肯定的话,你再次将它设置为假。如果在方法开头bool为yes,只需将其设置为false并返回:
private bool executingSelectedIndexChanged = false;
private void cmbSubjects_SelectedIndexChanged(object sender, EventArgs e)
{
// NEW CODE HERE
if (executingSelectedIndexChanged)
{
executingSelectedIndexChanged = false;
return;
}
if (pnlAvailable.Controls.Count != 0)
{
if (countMsg < 1)
{
// NEW CODE HERE
executingSelectedIndexChanged = true;
DialogResult res = MessageBox.Show("There are still available questions. Are you sure you want to change the subject?", "Changing subject...", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
if (res == DialogResult.Yes)
{
// NEW CODE HERE
executingSelectedIndexChanged = false;
cmbIndex = cmbSubjects.SelectedIndex;
countMsg = 0;
switch (cmbSubjects.SelectedIndex)
{
case 0:
subject = "life";
break;
case 1:
subject = "math";
break;
case 2:
subject = "physical";
break;
case 3:
subject = "technology";
break;
case 4:
subject = "vocational";
break;
}
GenQstBtns();
}
else if (res == DialogResult.No)
{
countMsg = 1;
cmbSubjects.SelectedIndex = cmbIndex;
}
}
}
}