开发者

Messagebox appearing twice C#

I have a combobox named cmbSubjects. It's purpose in my project is to change the subject in a quiz competition. I want it that when its selected item is changed, a message box should ask the quiz master to confirm if they will like to continue if there are unanswered questions on the subject in the combobox before the change. If they answer NO, the combobox should select its previously selected item. I got this code to do it but the problem is the messagebox appears twice if the quix master chooses NO on the messagebox. I found out the reason (after stepping into the code from a breakpoint) was that when the messagebox returns NO, my code changes the selected value of the combobox to its previous value, which triggers the cmbSubjects_SelectedIndexChanged event. I tried modifying the code by adding a variable to count the number of times the messagebox has appeared to prevent it. The problem is that it only appears after the combobox's value is first changed. The code below is actually the modified one. Can anyone help me with this? Thanks in advance. (I'm using 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;
                        ca开发者_如何学Cse 4:
                            subject = "vocational";
                            break;
                    }

                    GenQstBtns();
                }
                else if (res == DialogResult.No)
                {
                    countMsg = 1;
                    cmbSubjects.SelectedIndex = cmbIndex;
                }
            }
        }
    }


You can use SelectionChangeCommitted event.

Do not use SelectedIndexChanged or SelectedValueChanged to capture user changes, because those events are also raised when the selection changes programmatically.

So if you use this event after you change selected item programmatically this event wont be fired again


Is it possible you have more than one handler hooked up the SelectedIndexChanged event? You can look at all the handlers attached to the event in the debugger.


Simply check the new index against the current saved index:

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;
            }
        }
    }
}


You need to suppress the IndexChanged event from firing when you change it manually.

You can disable the event :

            else if (res == DialogResult.No)
            {
                countMsg = 1;

                cmbSubjects.SelectedIndexChanged -= cmbSubjects_SelectedIndexChanged;
                cmbSubjects.SelectedIndex = cmbIndex;
                cmbSubjects.SelectedIndexChanged += cmbSubjects_SelectedIndexChanged;

                fireEvent = true;
            }


If I correctly understand what you are trying to do, the simplest thing would probably be to add a bool variable executingSelectedIndexChanged initialized to false. In your method you can then check this bool, and if it is false set it to true and open your MessageBox. Then you set it to false again if the answer is yes. If the bool was yes at the beginning of your method, simply set it to false and return:

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;
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜