/// <summary> /// Displays the in-place edit control for a given list view subitem. /// It is assumed that the in-place edit control was previously created /// and assigned to the proper column, by means of the SetEditControl /// method. /// </summary> /// <param name="editedItem">The item to be edited.</param> /// <param name="editedSubItem">The subitem to be edited.</param> public void StartEditing(ListViewItem editedItem, ListViewItem.ListViewSubItem editedSubItem) { if (row < 0 || column < 0 || editedSubItem == null) { return; } // Check if event handler available and if positive, raise the event Control editControl; editControls.TryGetValue(column, out editControl); // Override the editable control in the custom subitem, if such an item is used. EditableListViewSubItem customSubItem = editedSubItem as EditableListViewSubItem; if (customSubItem != null) { if (customSubItem.ReadOnly) { // non-editable item return; } else { // override the edit control editControl = customSubItem.EditControl; } } ListViewSubItemEventArgs args = new ListViewSubItemEventArgs(editControl, editedItem, editedSubItem, column); if (SubItemEditing != null && editControl != null) { SubItemEditing(this, args); } // Check if the event was handled - thus the in-place edit controls // displayed. if (!args.Handled) { // Display edit control and also set text. DisplayEditControl(false, editControl, editedSubItem); } Form frm = FindForm(); if (frm != null && frm.KeyPreview) { frm.KeyPreview = false; _restoreKeyPreview = true; } }
/// <summary> /// End the in-place editing action. It results in "hiding" the /// in-place edit control. /// </summary> /// <param name="acceptChanges">Set to true to accept the new value, /// that is resulting after the editing action.</param> private void EndEditing(bool acceptChanges) { Form frm = FindForm(); if (frm != null && _restoreKeyPreview) { frm.KeyPreview = true; _restoreKeyPreview = false; } // Check the row bounds. if (row < 0 || row >= this.Items.Count) return; // Check the column bounds. if (column < 0 || column >= this.Columns.Count) return; if (!activeEditControl.Visible) return; ListViewItem editedItem = this.Items[row]; ListViewItem.ListViewSubItem editedSubItem = editedItem.SubItems[column]; if (acceptChanges) { // Editing results should be handled differently // for each type of supported in-place edit control. EditableListViewSubItem customSubItem = editedSubItem as EditableListViewSubItem; if (customSubItem != null) { customSubItem.Text = activeEditControl.Text; } else { editedSubItem.Text = activeEditControl.Text; } // Check if event handler available and if positive, raise the event if (SubItemEdited != null) { ListViewSubItemEventArgs args = new ListViewSubItemEventArgs(activeEditControl, editedItem, editedSubItem, column); SubItemEdited(this, args); // Check if the event was handled - thus the in-place edit controls // displayed. if (args.Handled) { return; } } } if (SubItemEndEditing != null) { ListViewSubItemEventArgs args = new ListViewSubItemEventArgs(activeEditControl, editedItem, editedSubItem, column); SubItemEndEditing(this, args); } // Nothing is edited right now. row = column = -1; // Disable the control and make it invisible ("hide" it). //>>>> FIXUP for nasty bug // for some strange reason (which we don't have time to investigate) // the SubItemEdited event f***s up the activeEditControl of the list. // So we put an extra protection here to prevent exceptions popping up if (activeEditControl != null) { activeEditControl.Hide(); activeEditControl.Enabled = false; activeEditControl.BringToFront(); // Unsubscribe for the event handlers. activeEditControl.Leave -= new EventHandler(OnEditorLeave); activeEditControl.KeyDown -= new KeyEventHandler(OnEditorKeyDown); // Set activeEditControl as null activeEditControl = null; } // Focus back to the list. Focus(); }
/// <summary> /// Displays the in-place edit control for a given list view subitem. /// It is assumed that the in-place edit control was previously created /// and assigned to the proper column, by means of the SetEditControl /// method. /// </summary> /// <param name="editedItem">The item to be edited.</param> /// <param name="editedSubItem">The subitem to be edited.</param> public void StartEditing(ListViewItem editedItem, ListViewItem.ListViewSubItem editedSubItem) { if (row < 0 || column < 0 || editedSubItem == null) return; // Check if event handler available and if positive, raise the event Control editControl; editControls.TryGetValue(column, out editControl); // Override the editable control in the custom subitem, if such an item is used. EditableListViewSubItem customSubItem = editedSubItem as EditableListViewSubItem; if (customSubItem != null) { if (customSubItem.ReadOnly) { // non-editable item return; } else { // override the edit control editControl = customSubItem.EditControl; } } ListViewSubItemEventArgs args = new ListViewSubItemEventArgs(editControl, editedItem, editedSubItem, column); if (SubItemEditing != null && editControl != null) { SubItemEditing(this, args); } // Check if the event was handled - thus the in-place edit controls // displayed. if (!args.Handled) { // Display edit control and also set text. DisplayEditControl(false, editControl, editedSubItem); } Form frm = FindForm(); if (frm != null && frm.KeyPreview) { frm.KeyPreview = false; _restoreKeyPreview = true; } }
/// <summary> /// End the in-place editing action. It results in "hiding" the /// in-place edit control. /// </summary> /// <param name="acceptChanges">Set to true to accept the new value, /// that is resulting after the editing action.</param> private void EndEditing(bool acceptChanges) { Form frm = FindForm(); if (frm != null && _restoreKeyPreview) { frm.KeyPreview = true; _restoreKeyPreview = false; } // Check the row bounds. if (row < 0 || row >= this.Items.Count) { return; } // Check the column bounds. if (column < 0 || column >= this.Columns.Count) { return; } if (!activeEditControl.Visible) { return; } ListViewItem editedItem = this.Items[row]; ListViewItem.ListViewSubItem editedSubItem = editedItem.SubItems[column]; if (acceptChanges) { // Editing results should be handled differently // for each type of supported in-place edit control. EditableListViewSubItem customSubItem = editedSubItem as EditableListViewSubItem; if (customSubItem != null) { customSubItem.Text = activeEditControl.Text; } else { editedSubItem.Text = activeEditControl.Text; } // Check if event handler available and if positive, raise the event if (SubItemEdited != null) { ListViewSubItemEventArgs args = new ListViewSubItemEventArgs(activeEditControl, editedItem, editedSubItem, column); SubItemEdited(this, args); // Check if the event was handled - thus the in-place edit controls // displayed. if (args.Handled) { return; } } } if (SubItemEndEditing != null) { ListViewSubItemEventArgs args = new ListViewSubItemEventArgs(activeEditControl, editedItem, editedSubItem, column); SubItemEndEditing(this, args); } // Nothing is edited right now. row = column = -1; // Disable the control and make it invisible ("hide" it). //>>>> FIXUP for nasty bug // for some strange reason (which we don't have time to investigate) // the SubItemEdited event f***s up the activeEditControl of the list. // So we put an extra protection here to prevent exceptions popping up if (activeEditControl != null) { activeEditControl.Hide(); activeEditControl.Enabled = false; activeEditControl.BringToFront(); // Unsubscribe for the event handlers. activeEditControl.Leave -= new EventHandler(OnEditorLeave); activeEditControl.KeyDown -= new KeyEventHandler(OnEditorKeyDown); // Set activeEditControl as null activeEditControl = null; } // Focus back to the list. Focus(); }