How to Customize Built-in Context Menus
- 5 minutes to read
A Spreadsheet control has two popup menus:
A worksheet tab context menu.
A worksheet context menu.
Worksheet Tab Context Menu
The TdxSpreadSheetBuiltInPageControlTabPopupMenu class implements a worksheet tab context menu. You can right-click on a worksheet tab header to invoke this menu. Handle the control’s OnPageControlContextPopup event to customize menu items or display a custom menu.
Worksheet Context Menu
The TdxSpreadSheetBuiltInTableViewPopupMenu class implements a worksheet context menu. You should right-click on a cell, floating container, or a row or column header to invoke this menu. Handle the control’s OnTableViewContextPopup event to customize menu items or display a custom menu.
How to Disable or Replace a Built-in Menu
Handle the OnPageControlContextPopup or OnTableViewContextPopup event and assign True to the AHandled parameter to disable the built-in context menu. Use the P parameter to obtain mouse pointer coordinates and display a custom menu at these coordinates.
procedure TMyForm.dxSpreadSheet1PageControlContextPopup(ASpreadSheet: TdxCustomSpreadSheet; const P: TPoint; APopupMenu: TPopupMenu; var AHandled: Boolean);
begin
MyPopupMenu.Popup(P.X, P.Y); // Invokes the custom popup menu at the mouse pointer position
AHandled := True; // Disables the built-in popup menu
end;
How to Remove Items from Built-in Menus
Handle the OnPageControlContextPopup or OnTableViewContextPopup event. Use the APopupMenu parameter to access a context menu. Use the Tag property to identify the target menu item.
The following code removes the “Hide“ and “Unhide…“ items from the worksheet tab menu:
procedure TMyForm.dxSpreadSheet1PageControlContextPopup(ASpreadSheet: TdxCustomSpreadSheet; const P: TPoint; APopupMenu: TPopupMenu; var AHandled: Boolean);
var
I: Integer;
begin
for APopupMenu.Items.Count - 1 downto 0 do // Iterates through all menu items
// If the current item is "Hide" or "Unhide..."
if ((APopupMenu.Items[I].Tag = TdxSpreadSheetBuiltInPageControlTabPopupMenu.cidHide) or (APopupMenu.Items[I].Tag = TdxSpreadSheetBuiltInPageControlTabPopupMenu.cidUnhide)) APopupMenu.Items.Delete(I); // Deletes a menu item
end;
How to Add Custom Menu Items
Handle the OnPageControlContextPopup or OnTableViewContextPopup event to add custom items to a context menu. Create a new pop-up menu item, handle its OnClick event and use the APopupMenu parameter to add the item to an existing menu.
The following code inserts the “Mark Cells” menu item after “Format Cells…“.
procedure TMyForm.dxSpreadSheet1TableViewContext(Sender: TdxSpreadSheetTableView; const P: TPoint; APopupMenu: TPopupMenu; var AHandled: Boolean);
var
AItem: TMenuItem;
I: Integer;
begin
if (not Sender.HitTest.HitAtColumnHeader and
not Sender.HitTest.HitAtRowHeader and
not Sender.HitTest.HitAtContainer) then // Ensures that the new item ends up in only in the popup menu is invoked for a cell or cell selection
for I := 0 to APopupMenu.Items.Count - 1 do // Iterates through all menu items to find a position to insert a new item
begin
if APopupMenu.Items[I].Tag = TdxSpreadSheetBuiltInTableViewPopupMenu.cidFormatCells then // If the current menu item is "Format Cells"
begin
AItem := TMenuItem.Create(APopupMenu); // Creates a new menu item
AItem.Caption := 'Mark Cells'; // Defines the menu item's caption
AItem.OnClick := MarkCellsMenuItemClick; // Assigns an implemented handler to the menu item's OnClick event
APopupMenu.Items.Insert(I, AItem); // Inserts the menu item after "Format Cells"
Break; // Exits the cycle
end;
end;
The following code example implements the “Mark Cells” item functionality in a new menu item’s OnClick event handler.
procedure TMyForm.MarkCellsMenuItemClick(Sender: TObject);
var
I, J, K: Integer;
ACell: TdxSpreadSheetCell;
ATableView: TdxSpreadSheetTableView;
begin
ATableView := dxSpreadSheet1.ActiveSheetAsTable;
ATableView.BeginUpdate; // Stops all worksheet update operations until all selected cells change their visual style
for I := 0 to ATableView.Selection.Count - 1 do // Iterates through all selected cell ranges
for J := ATableView.Selection.Items[I].Left to ATableView.Selection.Items[I].Right do // Iterates through all columns within a selected cell range
for K := ATableView.Selection.Items[I].Top to ATableView.Selection.Items[I].Bottom do // Iterates through all cells within a selected column
begin
ACell := ATableView.CreateCell(K, J); // Obtains a cell object to change its appearance
ACell.Style.Brush.Style := sscfsSolid; // Changes a selected cell's background style to solid color fill
ACell.Style.Brush.BackgroundColor := clMoneyGreen; // Uses the clMoneyGreen color to fill a cell's background
end;
ATableView.EndUpdate; // Resumes worksheet update operations and applies all pending changes made to cell style settings
end;