We help IT Professionals succeed at work.

combobox prevent index change

KS_Mis
KS_Mis asked
on
1,026 Views
Last Modified: 2012-05-09
On change of index/value of a combobox I want to ask the user if they are sure and if they say no i want to go back to the previously selected index/value.  I thought I could use drop down to store the previous index/value but that doens't work if they use the mouse scroll or arrow keys.  I need a beforeindexchange event.
Comment
Watch Question

Senior .Net Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
you can use customvalidator and a hiddenfield to do this.

I think you should not validate this at indexchange because it will irritate the user. You should check it at the time of submit of the page.

Author

Commented:
Sorry this is windows not asp.
Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover.

Commented:
here is a sample code
======================


Dim s As Integer

Dim changed As Boolean = False

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If Not changed Then

            If Not (MessageBox.Show("Are u sure", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then

                changed = True

                ComboBox1.SelectedIndex = s

            End If

        End If

        changed = True

    End Sub

    Private Sub ComboBox1_DropDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown

        s = ComboBox1.SelectedIndex

        changed = False

    End Sub
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
KS:

The static variable will work for you in that case.

static int oldIndex = null;

if (oldIndex != null)
{
   if(displayAlert()) //save new selection
     oldindex = combobox1.SelectedIndex;
   else  //revert back
     ComboBox1.SelectedIndex = oldindex;
}
else  // handles first time through, just save index as old index
   oldIndex = ComboBox1.SelectedIndex;



Author

Commented:
There is no "changed" event in windows and as I said in my original post I have the code in the drop down but that will not catch when users use the mouse scroll or arrow keys.
Top Expert 2010

Commented:

The best method is to inherit ComboBox in a new class and override 'OnSelectedIndexChanged' method in which you have complete control...
An inherited class is available here => http://stackoverflow.com/questions/782313/how-can-i-handle-combobox-selected-index-changing

OR if this is too much for your code, then you can still use the SelectedIndexChanged event as shown in the code below..
// _canceling variable is used to prevent the messagebox again display when we set the value back to the previous value.

private int _currentIndex = -1;
        private bool _canceling = false;

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_canceling)
            {
                DialogResult res = MessageBox.Show(this, "Want to change?", "", MessageBoxButtons.YesNo);
                if (res == DialogResult.No)
                {
                    
                        try
                        {
                            _canceling = true;

                            comboBox1.SelectedIndex = _currentIndex;
                        }
                        finally
                        {
                            _canceling = false;
                        }
                    
                }
            }

            _currentIndex = comboBox1.SelectedIndex;
        }

Open in new window

Shahan AyyubStaff Software Engineer
CERTIFIED EXPERT

Commented:
try this as well:

You can notify user on this event as well:
comboBox1_SelectedValueChanged

This event fires before comboBox1_SelectedIndexChanged event

int index = 0; Boolean ShowMessage=true; 
         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ShowMessage)
            {
                if (MessageBox.Show("Are you sure to change selection?", "Notification", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    return;
                ShowMessage = false; comboBox1.SelectedItem = comboBox1.Items[index]; 
            }
        }

        private void comboBox1_MouseEnter(object sender, EventArgs e)
        {
            index = comboBox1.SelectedIndex; ShowMessage=true;
        }

Open in new window

Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
There is another event handler:

private void YourComboBox_MouseWheel(object sender, MouseEventArgs e)

you can call make your own function, then point to that function to the alert.

Another way to do this is to disable the control, with a button to unlock it.  Then put the confirmation in the click event.

Author

Commented:
Thank you for the suggestions, but I don't see a MouseWheel event.
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
What .Net Version?

Also it looks like you can create your own to inherit it:
http://stackoverflow.com/questions/2968189/disable-mouse-scroll-wheel-in-combobox-vb-net
Top Expert 2010

Commented:

Hello KS_Mis,

Are the above comments useful. We would be happy to help you further if you need...

Thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.