Clipboard
Flexible TreeView does not support the copy & paste operations out-of-box because of different ways the treeview data could be stored, but it can be easily implemented manually.
In most cases you need to know how you store the treeview data and its structure, to be able to retrieve it and put into the clipboard.
There are two most popular cases of data binding in Flexible TreeView; first, is when you create a Node class descendant and store your data there as properties, and second case is when you store your data somewhere outside, like in a System.Data.DataTable, and bind the table to the treeview.
Below are descriptions of how to deal with these both cases when retrieving the data source's data.
Prerequisites#
There are a lot of ways of how to provide your user the possibility to copy the treeview data to be pasted into some external application.
To make things easier to understand, we suppose that user has to select some treeview nodes and press Ctrl+C to copy the selected nodes content into clipboard.
The code to accomplish this will be the same for both below cases, and shown below.
Note
Below we check args.IsBeforeAction
property and if it is TRUE
then we just exit because we don't want to copy the content to the clipboard twice. It is related to two-steps actions behavior that is supported by all Flexible TreeView events.
tree.Options.Selection.AllowMultiSelectByMouse = true;
tree.KeyDown += tree_KeyDown;
void tree_KeyDown(FlexibleTreeView treeview, ARMSoft.FlexibleTreeView.Events.KeyEventArgsEx args)
{
if(args.IsBeforeAction || !args.Control || args.KeyCode != Keys.C)
return;
string clipboardContent = GetClipboardContent(); // GetClipboardContent method is shown below.
Clipboard.Clear();
if(!string.IsNullOrEmpty(clipboardContent))
{
Clipboard.SetText(clipboardContent);
}
}
tree.Options.Selection.AllowMultiSelectByMouse = True
tree.KeyDown += tree_KeyDown
Private Sub tree_KeyDown(treeview As FlexibleTreeView, args As ARMSoft.FlexibleTreeView.Events.KeyEventArgsEx)
If args.IsBeforeAction OrElse Not args.Control OrElse args.KeyCode <> Keys.C Then
Return
End If
Dim clipboardContent As String = GetClipboardContent() ' GetClipboardContent method is shown below.
Clipboard.Clear()
If Not String.IsNullOrEmpty(clipboardContent) Then
Clipboard.SetText(clipboardContent)
End If
End Sub
Firstly, to allow the user to select many nodes, we enable Options.Selection.AllowMultiSelectByMouse treeview property. Then, we handle the KeyDown treeview event, and if Ctrl+C are pressed we call the GetClipboardContent
method (will be shown below because it differs for each below case), and if it returns some content write it to the clipboard.
Say, your data, that is shown in the treeview, hold the data about users and consist of the Name (string), Phone (string) and IsAdmin (bool) columns, then the GetClipboardContent
method must return the selected nodes content in the following format:
Name\tPhone\tIsAdmin\nName\tPhone\tIsAdmin\n.....
where the data properties are in bold.
Copy data that is stored in a node class#
When you fill the treeview with data manually, in most cases you store this data in a node class that is inherited from Node class, like this:
Class UserNode
Inherits Node
Public Property Name() As String
Get
Return _name
End Get
Set
_name = Value
End Set
End Property
Private _name As String
Public Property Phone() As String
Get
Return _phone
End Get
Set
_phone = Value
End Set
End Property
Private _phone As String
Public Property IsAdmin() As Boolean
Get
Return _isAdmin
End Get
Set
_isAdmin = Value
End Set
End Property
Private _isAdmin As Boolean
End Class
Now, say you add some nodes to the treeview, user has selected some of them and pressed Ctrl+C. Then the GetClipboardContent
method, which retrieves the selected nodes content, will look like this:
string GetClipboardContent()
{
StringBuilder result = new StringBuilder();
foreach(Node selectedNode in tree.SelectedNodes)
{
UserNode node = (UserNode)selectedNode;
string nodeContent = string.Format("{0}\t{1}\t{2}", node.Name, node.Phone, node.IsAdmin);
result.AppendLine(nodeContent);
}
return result.ToString();
}
Private Function GetClipboardContent() As String
Dim result As New StringBuilder()
For Each selectedNode As Node In tree.SelectedNodes
Dim node As UserNode = DirectCast(selectedNode, UserNode)
Dim nodeContent As String = String.Format("{0}" & vbTab & "{1}" & vbTab & "{2}", node.Name, node.Phone, node.IsAdmin)
result.AppendLine(nodeContent)
Next
Return result.ToString()
End Function
Copy data that is stored in an external storage#
In case you bind a DataTable (for example) data source to the treeview, and this data table contain the Name, Phone and IsAdmin columns, then the GetClipboardContent
method, which retrieves the selected nodes content, will look like this:
string GetClipboardContent()
{
StringBuilder result = new StringBuilder();
foreach(Node selectedNode in tree.SelectedNodes)
{
BindableNode bindableNode = (BindableNode)selectedNode;
DataRowView row = (DataRowView)bindableNode.BoundObject;
string nodeContent = string.Format("{0}\t{1}\t{2}", row["Name"], row["Phone"], row["IsAdmin"]);
result.AppendLine(nodeContent);
}
return result.ToString();
}
Private Function GetClipboardContent() As String
Dim result As New StringBuilder()
For Each selectedNode As Node In tree.SelectedNodes
Dim bindableNode As BindableNode = DirectCast(selectedNode, BindableNode)
Dim row As DataRowView = DirectCast(bindableNode.BoundObject, DataRowView)
Dim nodeContent As String = String.Format("{0}" & vbTab & "{1}" & vbTab & "{2}", row("Name"), row("Phone"), row("IsAdmin"))
result.AppendLine(nodeContent)
Next
Return result.ToString()
End Function