How can I edit the selected ComboBox index?

I have a ComboBox that has a list of manufacturers. When the user selects a manufacturer, the grid below is filled with data for the selected manufacturer. This data is subject to change. The user must click the "Save" button after making all the changes.

But the user may forget to click "Save" and select another manufacturer from ComboBox, and the grid will be filled with other data, so previous changes will be lost.

Therefore, I need to ask the user if he wants to save the changes before choosing another manufacturer.

How can i do this? Or maybe you offer a different way to solve my problem (looking from a different angle)?

+3
source share
10 answers

You must handle the ComboBox.SelectedIndexChanged event. Sort of:

this.ComboBox1.SelectedIndexChanged += new system.EventHandler(ComboBox1_SelectedIndexChanged);

Then it ComboBox1_SelectedIndexChanged()will be called whenever it changes, and you can update your manufacturer information in this function. Save old information before filling in new information. Or ask the user if they really want to change it before saving.

+3
source

Here's how we can subclass ComboBox to introduce a new SelectedIndexChangingEvent with the ability to undo the change:

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

}
+9
source

2

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

SelectedIndex

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

Night Coder . dll.
( CustomControls.) Night Coder ( ).

. dll Visual Studio. , . , .

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

:

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

.

"" .

, , "" .

"" .

+1

, , , , . , . IndexChanging - ComboBox, .

@AftabAhmedKalhoro @jeffamaphone, Tag.

ComboBox - , . Tag, , ( VB6).

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ComboBox1.Items.Add("Item1")
    ComboBox1.Items.Add("Item2")
    ComboBox1.Items.Add("Item3")
    ComboBox1.Items.Add("Item4")
    ' Load Value from database or whatever and set the value or index.
    ComboBox1.SelectedIndex = 0
    ComboBox1.Tag = ComboBox1.SelectedIndex

    ' I add the handler at the end because I don't want it to fire during loading the form.
    AddHandler ComboBox1.SelectedIndexChanged, New EventHandler(AddressOf ComboBox1_SelectedIndexChanged)
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If (ComboBox1.Tag <> ComboBox1.SelectedIndex) Then
        If MessageBox.Show("Warning! You are changing the index." & vbCrLf & _
                           "Do you wish to continue?", _
                           "Changing Index", _
                           MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
            ComboBox1.Tag = ComboBox1.SelectedIndex
            ' Do Something.
        Else
            ComboBox1.SelectedIndex = ComboBox1.Tag
        End If
    End If
End Sub

, SelectedIndex , :

ComboBox1.SelectedIndex = ComboBox1.Tag
+1

, , ComboBox.SelectedIndexChanged .

, - , , , , . , true, . , , , .

0

, ComboBox ( ), ( - DataSet, ..) . , ComboBox, , , . ( SelectionChangeCommited , , true, , .) , , .

0

. .

!

, VB.NET , :

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
source

ComboBox provides an event called SelectedIndexChanged. This event occurs whenever the SelectedIndex property changes, so you need to handle the event when the user wants to change the combo index, if the user has not saved the change, ask him to do it.

-2
source

Source: https://habr.com/ru/post/1706976/


All Articles