总之,我想返回前面的选择项,而不再次选择它,也不触发SelectedIndexChanged方法。
情况是这样的:我有组合框,当选择一个项时,datagridview将填充行。您可以编辑datagridview中的内容并将其保存到文件中。如果不保存更改并尝试更改组合框中的项,则会通知您所有更改都将丢失。我制作了一个消息箱,让你在“是”(“要改变”)和“不”(不是“改变”)之间做出选择。如果您单击“是”,一切都可以,但如果您单击“否”,则我必须返回前面选定的项。当我这样做的时候,我触发了SelectedIndexChanged,这使得数据视图再次加载并删除更改。下面是我在SelectedIndexChanged方法中的代码:
if (!ChangeMade)
{
//#1 Some Code
}
else
{
DialogResult dialogResult =
MessageBox.Show("Are you sure you want to change the manifacturer?" +
"\n All the changes you have done will be lost.",
"Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//same code as #1
}
else
{
//Here must be the code that returns the previous
//item without selecting it.
}
对不起我的英语。
发布于 2013-11-26 10:56:19
在我看来,您只希望在用户更改组合框时才更改数据。对于这种情况,SelectionChangeCommitted
是完美的,因为它只在用户更改combobox
时触发。
private void TypeSelectionChangeCommitted(object sender, EventArgs e)
{
if (!ChangeMade)
{
//#1 Some Code
}
else
{
DialogResult dialogResult =
MessageBox.Show("Are you sure you want to change the manifacturer?" +
"\n All the changes you have done will be lost.",
"Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//same code as #1
}
else
{
//Here must be the code that returns the previous
//item without selecting it.
}
}
}
关于MSDN的更多信息
发布于 2013-11-26 10:58:15
我这么做的方式:
private void ComboBoxOnChange(...)
{
if (!shouldTrigger)
return;
shouldTrigger = true;
// Here goes your code
should trigger = false;
}
发布于 2013-11-26 10:53:54
this.comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
https://stackoverflow.com/questions/20215020
复制相似问题