How can I handle ComboBox selected index changing?

前端 未结 10 1189
花落未央
花落未央 2020-12-21 06:10

I have a ComboBox that have a list of manufacturers. When a user selects a manufacturer, a grid below is populated with data for the chosen manufacturer. That data can be mo

相关标签:
10条回答
  • 2020-12-21 06:37

    If you wonder how you can receive notification when the selection changes, you can subscribe to the ComboBox.SelectedIndexChanged event.

    If you want to offer the user the option to save, only when something has changed and she has forgotten so save her changes, you need to keep track of when these other fields change. This could be accomplished by maintaining a boolean value which is set to true whenever the user edits any fields. When the mentioned event occurs, check this value before deciding whether to offer the option to save.

    0 讨论(0)
  • 2020-12-21 06:39

    Great job nightcoder. Your code Works perfectly.

    Thanks!

    For developers who write in VB.NET here you have translation:

    Imports System.ComponentModel
    
    Public Class ComboBoxEx
      Inherits ComboBox
    
      Private pLastAcceptedSelectedIndex As Integer
    
      Public Event SelectedIndexChanging As CancelEventHandler
    
      Public Property LastAcceptedSelectedIndex() As Integer
        Get
          Return pLastAcceptedSelectedIndex
        End Get
        Set(ByVal value As Integer)
          pLastAcceptedSelectedIndex = value
        End Set
      End Property
    
      Public Sub New()
        LastAcceptedSelectedIndex = -1
      End Sub
    
      Protected Sub OnSelectedIndexChanging(ByVal e As CancelEventArgs)
        RaiseEvent SelectedIndexChanging(Me, e)
      End Sub
    
      Protected Overrides Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
        If LastAcceptedSelectedIndex <> SelectedIndex Then
          Dim cancelEventArgs As CancelEventArgs
    
          cancelEventArgs = New CancelEventArgs()
          OnSelectedIndexChanging(cancelEventArgs)
    
          If Not cancelEventArgs.Cancel Then
            LastAcceptedSelectedIndex = SelectedIndex
            MyBase.OnSelectedIndexChanged(e)
          Else
            SelectedIndex = LastAcceptedSelectedIndex
          End If
        End If
      End Sub
    End Class
    
    0 讨论(0)
  • 2020-12-21 06:41

    Create 2 class level variables

    private bool selectionCancelled=false;
    private int lastSelectedIndex=-1;
    

    Then in SelectedIndex event you can write code as following

            if (!selectionCancelled)
            {
                if (MessageBox.Show("Are you sure you want to change the selection ?", this.Text, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    selectionCancelled = true;
                    comboBox.SelectedIndex = lastSelectedIndex;
                    return;
                }
    
                lastSelectedIndex = comboBox.SelectedIndex;
                // Normal code of the event handler
    
            }
            else
            {
    
                selectionCancelled = false;
            }
    
    0 讨论(0)
  • 2020-12-21 06:41

    An easy way to keep track of unsaved changes.

    After loading any original values, disable the "Save" button.

    When the user attempts to leave, check to see if the "Save" button is enabled.

    Enable or disable the "Save" button as required.

    0 讨论(0)
  • 2020-12-21 06:47

    The ComboBox provides an event called SelectedIndexChanged. This event raises whenever the Property SelectedIndex changes, so you have to handle the event for, Whenever the user wants to change the index of the combo, if the user has not saved the changes, Ask him to do so.

    0 讨论(0)
  • 2020-12-21 06:49

    Night Coder's solution is elegant and concise. I have packaged it in a dll.
    (I call it CustomControls.) To do this create a new class library and add the first few statements to Night Coder's solution (copied here as a convenience).

    Once you have compiled the code, you can add it as a reference. I actually loaded the dll into my Visual Studio Tools pane. That way I can drag the control onto my form at design time. Conveniently, the new event shows up in the properties list.

    use System.ComponentModel;
    
    use System.Windows.Forms; //this will need to be added as a reference
    
    //your namespace will name your dll call it what you will
    
    namespace CustomControls
    

    Night Coder's solution follows:

    public class ComboBoxEx : ComboBox
    {
            public event CancelEventHandler SelectedIndexChanging;
    
    
        [Browsable(false)]
        public int LastAcceptedSelectedIndex { get; private set; }
    
        public ComboBoxEx()
        {
                LastAcceptedSelectedIndex = -1;
        }
    
        protected void OnSelectedIndexChanging(CancelEventArgs e)
        {
                var selectedIndexChanging = SelectedIndexChanging;
                if (selectedIndexChanging != null)
                        selectedIndexChanging(this, e);
        }
    
    
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
                if (LastAcceptedSelectedIndex != SelectedIndex)
                {
                        var cancelEventArgs = new CancelEventArgs();
                        OnSelectedIndexChanging(cancelEventArgs);
    
                        if (!cancelEventArgs.Cancel)
                        {
                                LastAcceptedSelectedIndex = SelectedIndex;
                                base.OnSelectedIndexChanged(e);
                        }
                        else
                                SelectedIndex = LastAcceptedSelectedIndex;
                }
        }
    }
    
    0 讨论(0)
提交回复
热议问题