1
votes

I am writing on c# with visual interface. Part of the logic consists of a set of values changing depended on which one of them (sets) is selected in the combo box.

Changes in the value sets can be saved or not. There is a need to offer an opportunity for user to save unsaved changes or reject them when he chooses a different item (set) in the combo box. It is imperative that when the message box with yes/no is presented combo box still displayed the old value, and only after that, depending on user's choice displayed new or old.

The sequence should be:

user uses keys or drop-down to select new item -> event is fired and form stops all of its processing -> my code cancels the change or lets it go through -> (if not cancelled) combo box is redrawn with new value.

N.B. Following events were tried and proved not to be adequate:

  • SelectedIndexChanged
  • SelectedValueChanged
  • SelectionChangeCommitted
  • Validating
  • DropDownClosed
2
I have listed the events i have tried handling that proved to be not up to their job. I can re-list them, sure.Srv19
After they key/select an item in the drop-down, do you want them to receive a message box asking if they want to save it? I am still unclear of the order you want things.Taryn
You are correct. The important thing here is that the item they are being suggested to save was still displayed when the message box was shown.Srv19

2 Answers

1
votes

I think this is what you are after - whenever the value is changed the user is prompted OK/Cancel. The only limitation with this is that the combobox shows the new value while the message box is displayed and until the user clicks Cancel. Maybe by intercepting the paint messages you can prevent this.

class MyCombo : ComboBox
    {
        // Keep track of the previous value
        int previousIndex = 0;

        // Determines whether the OnSelectedIndexChanged is ignored
        bool ignoreChangedEvent = false;

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.ComboBox.SelectedIndexChanged"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            if (!ignoreChangedEvent)
            {
                // Prompt the user to see if they really want to change.
                if (MessageBox.Show("Change value?", Application.ProductName, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    ignoreChangedEvent = true;
                    base.SelectedIndex = previousIndex;
                }
                else
                {
                    previousIndex = base.SelectedIndex;
                }
            }
            else
            {
                ignoreChangedEvent = false;
            }

            base.OnSelectedIndexChanged(e);
        }
    }
1
votes

I have found somewhat acceptable solution, although not directly corresponding to the verbatium of the question asked.

One possible solution, though, sadly, for some reason deleted by his suggestor, was to use message filters. That, however, led to the path of manually calculating where the mouse click went and essentially substituting the winforms capabilities of translating the mouse events to the process of changing selected item in dropped list of the combo box with some crude crutches on my own. This is the path i shied from.

In the end i settled on "cosmetic" solution, with the idea being substitution of displayed text in combo box for the duration of user's decision-making on the subject of whether or not cancel the change.

So, in the SelectedIndexChanged event i've put the follofing code:

try
{
    if (MyDataSets.Current.HasChanges() && !MyDataSets.Current.Name.Equals(cbChosenDataSet.Value))
    {
        cbChosenDataSet.DropDownStyle = ComboBoxStyle.DropDown;
        cbChosenDataSet.Text = MyDataSets.Current.Name + ' ';
        Application.DoEvents();
    }
    else return;
    /*
     * UserChoseToCancel is set according to user's choice
     */
    if (UserChoseToCancel)
        cbChosenDataSet.Value = MyDataSets.Current.Name;
    else
        MyDataSets.SetCurrent(cbChosenDataSet.Value);
    /*
     * other things
     */
}
catch(Exception e) {/* handling */}
finally
{
    cbChosenDataSet.DropDownStyle = ComboBoxStyle.DropDownList;
}

The gist of the idea is this: in DropDown style ComboBox' text can be changed as needed. However, when set to one of the items in the list, a change of selection will occur. To avoid that unneededly, a space is added to the temporary text.

In case cancelling does not occurs, restoring of the style to DropDownList forces the text to be changed to the actual chosen value (which has remained the same).

In case user cancels the change, value of the combo box is set back to the old one. A check in the beginning of the handler stops the event generated by that from being processed further.