TreeNode LabelEdit and Ctrl-C shortcut

My form has a main menu that contains a Copy menuItem with an associated Ctrl-C shortcut. This copies the selected TreeNode. I've also implemented in-place rename of TreeNodes using LabelEdit. Once TreeNode.BeginEdit() has been called and the node label is being edited, how do I direct a Ctrl-C keystroke to copy the text within the label to the clipboard and not fire the OnMenuCopyClick event handler

Thanks



Answer this question

TreeNode LabelEdit and Ctrl-C shortcut

  • jonathanVerrier

    You can see the desired behavior in the Windows Explorer. Select a folder in the TreeView and right click for Rename. This is the state after TreeNode.BeginEdit() has been called. Now type Ctrl-C. It copies the selected label text to the clipboard. It does not copy the folder.

    I believe your suggestion is to have the OnMenuCopyClick handler (equivalent to the code to copy a folder in the example above) manage both the rename label text copy and the TreeNode copy by using a flag to indicate that rename is in progress How will I retrieve the TreeNode label text This would be available in the NodeLabelEditEventArgs label property for the BeforeLabelEdit or AfterLabelEdit events. But the editing is in progress.

    By the way, the TreeNode default context menu handles this correctly. Once TreeNode.BeginEdit() has been called, if you right click for the context menu, you can use the Copy there to copy the selected label text. Just not the Ctrl-C shortcut for copy.

    After posting my question yesterday, I continued searching and found this discussion:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=371646&SiteID=1

    which suggests overriding the ProcessCmdKey method. But I don't understand what should be done in the override code.


  • Patrick Boyd

    Hi,

    If you have Copy menuItem with an associated Ctrl-C shortcut. That is to say, if we press Ctrl-C, the behavior is just as we click the Copy Menu, so the OnMenuCopyClick will fire.

    So it seems to be contradictive scenario.

    So far I think we can use a flag, I am not sure about your concrete scenario about when you need to fire the OnMenuCopyClick related code.

    In the OnMenuCopyClick we can have a if statement to evaluate the flag to copy the lable onto clipboard directly or go the original OnMenuCopyClick code.

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang



  • Chris_Jose

    Thanks for your help. Here's what I ended up with. I added this to the form class.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

    {

    if (keyData == (Keys.Control | Keys.C) && renameFlag)

    {

    // Do not handle here, pass the keystroke along to other controls

    return false;

    }

    return base.ProcessCmdKey(ref msg, keyData);

    }


  • Sebastien St-Laurent

    Hi,

    Based on my research, you may try to add the code line in the Form, so that when we press Ctrl+C, both our Menu_click code will be called and the Lable text will be copied onto the clipboard.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    base.ProcessCmdKey(ref msg, keyData);
    return false;
    }

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang



  • TreeNode LabelEdit and Ctrl-C shortcut