Stop Delete key from firing


https://www.nevron.com/Forum/Topic8589.aspx
Print Topic | Close Window

By Craig Browder - 10 Years Ago
I cannot stop the delete key from firing. can you please take a look at the code below and let me know what I am doing incorrectly?
I over-rode the ProcessCmdKey method because NodeKeyPress didn't detect the delete key being pressed.


protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Delete)
{
EventSinkService_NodeKeyPress(new NKeyPressEventArgs(this.nDrawingView1, (char)keyData));

keyData = Keys.Space;
}

return base.ProcessCmdKey(ref msg, keyData);
}

void EventSinkService_NodeKeyPress(NKeyPressEventArgs args)
{
if (this.ReadOnly == true)
{
if (args.KeyChar == (Char)Keys.Delete)
args.Handled = true;
}
}
By Nevron Support - 10 Years Ago

The code you have used to disable shape deleting with the “Delete” key is not correct. There are 2 better ways to achieve this functionality in Nevron Diagram For .NET.

1. The first way (which is the one I recommend) is to set “Delete” protection to all shapes you want to forbid removing of. The following code demonstrates how to set “Delete” protection to all shapes in the active layer of a drawing document:

NNodeList shapes = document.ActiveLayer.Descendants(NFilters.TypeNShape, -1);

for (int i = 0, count = shapes.Count; i < count; i++)

{

      NShape shape = (NShape)shapes[i];

      NAbilities protection = shape.Protection;

      protection.Delete = true;

      shape.Protection = protection;

}

Note that setting a “Delete” protection will show a message to the user when he tries to delete a shape with the “Delete” keyboard key. If you don’t want this message to be shown, you can set the ShowMessage property of the drawing view to false:

view.ShowMessages = false;

2. The second way to disable shape from deleting is to use the NodeRemoving event of the drawing document’s event sink service and set the Cancel property of the event arguments to true.

document.EventSinkService.NodeRemoving += OnNodeRemoving;

 

private void OnNodeRemoving(NChildNodeCancelEventArgs args)

{

      args.Cancel = true;

}

In general all the “...ing” events of the event sink service are cancellable, so you can cancel them in this way when necessary.