private void lvCollection_DoubleClick(object sender, EventArgs e) { if (lvCollection.SelectedItems.Count > 0) { ListViewItem lvItem = lvCollection.SelectedItems[0]; string key = lvItem.Text; string value = lvItem.SubItems[1].Text; if (KeyValueItemForm.Edit(this, ref key, ref value)) { if (lvCollection.SelectedItems[0].Text != key && lvCollection.Items.ContainsKey(key)) { MessageBox.Show(this, string.Format("Параметр \"{0}\" уже имеется в списке", key), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (lvCollection.SelectedItems[0].Text != key) { lvCollection.Items.Remove(lvItem); lvItem = lvCollection.Items.Add(key, key, 0); lvItem.SubItems.Add(value); } else { lvItem.SubItems[1].Text = value; } } } }
internal static bool Edit(IWin32Window owner, ref string key, ref string value) { using (KeyValueItemForm dlgForm = new KeyValueItemForm()) { dlgForm.Text = string.Format("{0}: {1}", key, value); dlgForm.tbKey.Text = key; dlgForm.tbValue.Text = value; if (dlgForm.ShowDialog(owner) == DialogResult.OK) { key = dlgForm.tbKey.Text; value = dlgForm.tbValue.Text; return(true); } return(false); } }