Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy TreeView Node

I am trying to copy the selected treeview node to the clip board so I can paste it in notepad. Here is my code but it doesn't work.

    TreeNode selNode = (TreeNode)this.treeView1.SelectedNode;
    Clipboard.SetData("TreeNode", selNode);
like image 494
Jeremy Avatar asked Nov 29 '22 05:11

Jeremy


1 Answers

Notepad doesn't know anything about the Winforms TreeNode class. Use Clipboard.SetText() instead:

    private void treeView1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == (Keys.Control | Keys.C)) {
            if (treeView1.SelectedNode != null) {
                Clipboard.SetText(treeView1.SelectedNode.Text);
            }
            e.SuppressKeyPress = true;
        }
    }
like image 126
Hans Passant Avatar answered Jan 18 '23 20:01

Hans Passant