简体   繁体   English

如何防止组合框SelectedIndexChanged触发?

[英]How to prevent combobox SelectedIndexChanged to trigger?

In two words I want to return the previous selecteditem without selecting it again or without triggering the SelectedIndexChanged method. 我想用两个词返回上一个selecteditem,而无需再次选择它或不触发SelectedIndexChanged方法。

This is the situation: I have combo box and when a item is selected, datagridview is filled with rows. 这是这种情况:我有一个组合框,当选择一个项目时,datagridview充满行。 You can edit things in the datagridview and save them to a file. 您可以在datagridview中编辑内容并将其保存到文件中。 If you don't save the changes and try to change the item in the combo box it tells you that all the changes will be lost. 如果您不保存更改并尝试更改组合框中的项目,则会告诉您所有更改都将丢失。 I make a messagebox that make you choose between yes(to change) and no(not to change). 我创建一个消息框,使您可以在是(更改)和否(不更改)之间进行选择。 If you click yes everything is OK, but if you click NO then I have to return the previous selected item. 如果单击“是”,则一切正常,但如果单击“否”,那么我必须返回先前选择的项目。 When I do so I trigger SelectedIndexChanged and that makes the datagridview load again and delete the changes. 当我这样做时,我会触发SelectedIndexChanged,这将再次加载datagridview并删除更改。 Here is my code in the SelectedIndexChanged method: 这是我在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.
            }

Sorry for my english. 对不起我的英语不好。

As I see it you want to change data only when user changes combo box. 如我所见,您只想在用户更改组合框时更改数据。 For this scenario SelectionChangeCommitted is perfect because it only triggers when user makes changes to combobox . 对于这种情况, 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.
        }
    }
}

More info on MSDN 有关MSDN的更多信息

The way I do this: 我这样做的方式:

private void ComboBoxOnChange(...)
{
    if (!shouldTrigger)
        return;

    shouldTrigger = true;
    // Here goes your code
    should trigger = false;
}
  this.comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM