$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
TreeView Control for Windows Forms
Build complex navigation systems and display complex hierarchical structures
Home > WinForms UI Controls > User Manual > Customize Context Menu in C#

C# Customizing Context Menu of KetticTreeView

KetticTreeView control expands the functionality through adding context menus to any nodes, as well as adding different context menus to each node in the tree view. Moreover, this control also supports the customization of the context menu and allows the users to modify the style and appearance of a single context menu. The following C# tutorial shows how to create custom context menu for KetticTreeView.

C# API for Customizing Context Menu of KetticTreeView

KetticTreeView contains the C# API for the developers to easily customize the context menu of nodes. This is achieved by using the OpeningContextMenu event. The following are the properties of this event for customizing the context menu in C# Windows Forms application.
  • TreeView, this is the tree view of fired OpeningContextMenu
  • TreeViewElement, this is an element of type KetticTreeViewElement and the main element of the control
  • Menu, we can use this property to return the default context menu showing on node
  • Node, this is the data node of type KetticTreeNode for the context menu. It includes Text, Level, and DataBoundItem properties. The Text property is able to return the node text for changing the context menu, the Level property is able to return an integer for indicating the level of the node in hierarchy, and the DataBoundItem is the object of data for the created KetticTreeView and is associated with the data node of the bound KetticTreeView.
  • Cancel, this property is used to hide the context menu when it is true

Access Context Menu Items for Customization

KetticTreeView control allows the users to display, hide, enable, and disable the items of the context menu in C#.NET. This is achieved through the Name property, which is capable of applying the custom TreeView. The following are the items of the context menu and the name values.

C# Remove Items based on Nodes Level and DataBoundItem

In the following example, we are going to hide the Delete menu item and New menu item for the nodes of KetticTreeView bound to a DataTable where there are the ID, Name, IsSystemItem and ParendID fields. The records in the table refer to each other according to the ID-ParentID relation and the KetticTreeView node will display the Name property of the records. The C# code below demonstrates how to implement the customization for context menu item of node in TreeView.

void ketticTreeView1_ContextMenuOpening1(object sender, Kettic.WinForms.UI.TreeViewContextMenuOpeningEventArgs e)
{
DataRowView rowView = (DataRowView)e.Node.DataBoundItem;
DataRow row = rowView.Row;

for (int i = e.Menu.Items.Count - 1; i >= 0; i--)
{
if (e.Menu.Items[i].Name == "Delete")
{
if ((bool)row.ItemArray[2] == true)
{
e.Menu.Items.Remove(e.Menu.Items[i]);
}
}

if (e.Menu.Items[i].Name == "New")
{
if (e.Node.Level == 0)
{
e.Menu.Items.Remove(e.Menu.Items[i]);
}
}
}
}
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls