I am working on a Windows application that has a ListView containing a bunch of items. When the user clicks on an item, the application displays the details of the item. the user has the ability to edit this data. The user must click the "Save" button after each change, but, of course, this does not always happen.
If the user makes changes and does not click the "Save" button, the application displays a message asking if they want to save their changes. This box includes Cancel, and if they click Cancel, I would like to briefly close the selection of another item and save the user in the one they edited.
I cannot find a way to do this. I show the dialog from the itemselecedchanged event, if the item is changed and not saved, if the user clicks cancel, I remove my function from the event and manually change the selected item and after that I return the function to the event, but after that the event is called and the item that I select manually, not selected.
private bool EnsureSelected() { bool continue_ = true; if (_objectChange) { var res = MessageBox.Show("Do you want to save changes?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); switch (res) { case DialogResult.Cancel: if (!string.IsNullOrEmpty(_selectedKey)) { listView_Keys.ItemSelectionChanged -= listView_Keys_ItemSelectionChanged; listView_Keys.Focus(); listView_Keys.Items[_selectedKey].Selected = true; listView_Keys.ItemSelectionChanged += listView_Keys_ItemSelectionChanged; } continue_ = false; break; case DialogResult.Yes: button_Save.PerformClick(); _objectChange = false; break; case DialogResult.No: _objectChange = false; break; default: throw new ArgumentOutOfRangeException(); } } return continue_; }
UPDATE ::
I tried this solution:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ListViewItem currentSelection = null; private bool pending_changes = false; private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { if (e.Item == currentSelection) {
but this did not work, the first time the pending changes were correct, the message box was called up twice, and the second time nothing happened.
c # listview winforms
aviad facebook
source share