KS_Mis
asked on
combobox prevent index change
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry this is windows not asp.
here is a sample code
======================
Dim s As Integer
Dim changed As Boolean = False
Private Sub ComboBox1_SelectedIndexCha nged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexCha nged
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
======================
Dim s As Integer
Dim changed As Boolean = False
Private Sub ComboBox1_SelectedIndexCha
If Not changed Then
If Not (MessageBox.Show("Are u sure", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult
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
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;
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;
ASKER
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.
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;
}
try this as well:
You can notify user on this event as well:
comboBox1_SelectedValueCha nged
This event fires before comboBox1_SelectedIndexCha nged event
You can notify user on this event as well:
comboBox1_SelectedValueCha
This event fires before comboBox1_SelectedIndexCha
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;
}
There is another event handler:
private void YourComboBox_MouseWheel(ob ject 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.
private void YourComboBox_MouseWheel(ob
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.
ASKER
Thank you for the suggestions, but I don't see a MouseWheel event.
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
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
Hello KS_Mis,
Are the above comments useful. We would be happy to help you further if you need...
Thanks
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.