Context menu
Flexible TreeView has only the columns and the summaries context menu support built-in because their structure and actions over them are well defined and known in advance.
The nodes context menu and its items, on the other hand, are specific to the particular project requirements. That is why the nodes context menu needs to be implemented manually.
To add context menu support to the treeview, the MouseDown treeview event needs to be handled, and a custom menu shown, as shown below.
Note
Pay attention that the code in the MouseDown event handler must be executed only when args.IsBeforeAction is false
which means that there is no 'standard' logic will be executed by the treeview after the event handler code.
ContextMenuStrip menu = new ContextMenuStrip();
ToolStripMenuItem showNodeNameItem = new ToolStripMenuItem("Show node name");
showNodeNameItem.Click += ShowNodeNameItem_Click;
menu.Items.Add(showNodeNameItem);
tree.MouseDown += tree_MouseDown;
void tree_MouseDown(FlexibleTreeView treeview, MouseActionArgs args)
{
if (!args.IsBeforeAction)
{
menu.Show(treeview, args.Location);
}
}
void ShowNodeNameItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
ContextMenuStrip menu = (ContextMenuStrip)item.Owner;
FlexibleTreeView treeview = (FlexibleTreeView)menu.SourceControl;
Node focusedNode = treeview.FocusedNode;
string nodeName;
if (focusedNode != null)
{
nodeName = "Node name is: " + focusedNode.Text;
}
else
{
nodeName = "Node is not selected";
}
MessageBox.Show(nodeName);
}
Dim menu As New ContextMenuStrip()
Dim showNodeNameItem As New ToolStripMenuItem("Show node name")
AddHandler showNodeNameItem.Click, AddressOf ShowNodeNameItem_Click
menu.Items.Add(showNodeNameItem)
AddHandler tree.MouseDown, AddressOf tree_MouseDown
Private Sub tree_MouseDown(treeview As FlexibleTreeView, args As MouseActionArgs)
If Not args.IsBeforeAction Then
menu.Show(treeview, args.Location)
End If
End Sub
Private Sub ShowNodeNameItem_Click(sender As Object, e As EventArgs)
Dim item As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Dim menu As ContextMenuStrip = DirectCast(item.Owner, ContextMenuStrip)
Dim treeview As FlexibleTreeView = DirectCast(menu.SourceControl, FlexibleTreeView)
Dim focusedNode As Node = treeview.FocusedNode
Dim nodeName As String
If focusedNode IsNot Nothing Then
nodeName = "Node name is: " + focusedNode.Text
Else
nodeName = "Node is not selected"
End If
MessageBox.Show(nodeName)
End Sub
In the example above, a custom context menu, using the ContextMenuStrip .NET class, is created in advance, with all its items and their click handlers, and then the menu is just shown in the MouseDown treeview event handler at the mouse cursor position.