views:

3721

answers:

6

Hello,

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 modified. The user has to press the Save button after all changes are done.

But the user can forget to press Save and select another manufacturer from the ComboBox and the grid will be populated with another data, so previous changes will be lost.

So I need to ask user if he wants to save changes before selecting another manufacturer.

How can I do this? Or maybe you offer another way of solving my task (looking from another angle)?

+3  A: 

You should handle the ComboBox.SelectedIndexChanged event. Something like:

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

Then ComboBox1_SelectedIndexChanged() will be called whenever it changes and you can update your manufacturer info in that function. Save the old info before populating the new info. Or prompt the user if they really want to change it before saving.

jeffamaphone
Anything like e.Cancel for Combobox selection change?
Ismail
A: 

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.

Jhonny D. Cano -Leftware-
A: 

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.

Tormod Fjeldskår
A: 

The best thing to do here is compare the data entered in the ComboBox (likewise with other fields) to that already stored (in whatever - the DataSet, list object, etc.) and check for any differences. This way, if the user selects another item from the ComboBox but then changes it back to the original one, the program recognises that the data has still not been modified. (Handling the SelectionChangeCommited event, for example, and setting a boolean to true, wouldn't allow this to be detected, and would additionally be marginally harder to implement.) In this situation, the simplest and most elegant approach would also seem to provide the best functionality.

Noldorin
+1  A: 

Here is how we can subclass ComboBox to introduce new SelectedIndexChangingEvent with a possibility to cancel the changing:

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

}
nightcoder
A: 

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