Spread Windows Forms 18
Spread Windows Forms 18 Product Documentation / Developer's Guide / Customizing Interaction in Cells / Displaying Context Menu at Runtime
In This Topic
    Displaying Context Menu at Runtime
    In This Topic

    Spread allows you to display the context menu as in Spread Designer at runtime. Hence, removing the need to attach it with ribbon control. This can be done by setting Spread’s ContextMenuStrip property with a new instance of the SpreadContextMenuStrip.  Displaying the context menu at runtime works only when LegacyBehaviors.Style is excluded from LegacyBehaviors enum.  

    There is no default context menu in Spread.  

    The table below depicts different context menu displayed on right clicking specific parts of Spread.

    Area Clicked Context Menu Items
    Cell
    Row Header
    Column Header
    Sheet Corner

    Use the following code to display context menu in Spread at runtime.

    C#
    Copy Code
    // Adding context menu
    fpSpread1.ContextMenuStrip = new SpreadContextMenuStrip(); 
    
    Visual Basic
    Copy Code
    ' Adding context menu
    FpSpread1.ContextMenuStrip = New SpreadContextMenuStrip()
    

    You can also display context menu like design mode on a Cell and Column Header as displayed below.

    Area Clicked Design Mode Context Menu Items
    Cell
    Column Header

    The following code displays the context menu as that of design mode in Spread at runtime.

    C#
    Copy Code
    fpSpread1.BeforeRightClick += FpSpread1_BeforeRightClick;  
    // For changing ContextMenu as design mode ContextMenu
    private void FpSpread1_BeforeRightClick(object sender, BeforeRightClickEventArgs e)
    {
        if (e.ContextMenuStrip is SpreadContextMenuStrip spreadContext)
        {
            ContextMenuType contextMenuType = e.ContextMenuType;
            spreadContext.GenerateMenuItems(fpSpread1, contextMenuType, true);
        }
    }
    
    Visual Basic
    Copy Code
    AddHandler FpSpread1.BeforeRightClick, AddressOf FpSpread1_BeforeRightClick
    ' For changing ContextMenu as design mode ContextMenu
    Private Sub FpSpread1_BeforeRightClick(ByVal sender As Object, ByVal e As BeforeRightClickEventArgs) Handles FpSpread1.BeforeRightClick
         Dim spreadContext As SpreadContextMenuStrip = TryCast(e.ContextMenuStrip, SpreadContextMenuStrip)
         If spreadContext IsNot Nothing Then
             Dim contextMenuType As ContextMenuType = e.ContextMenuType
             spreadContext.GenerateMenuItems(FpSpread1, contextMenuType, True)
         End If
    End Sub
    

    Although, context menu does not appear automatically, but if used then you can remove it later by following code.

    C#
    Copy Code
    // Context menu will not be shown  
    fpSpread1.ContextMenuStrip = null;   
    
    Visual Basic
    Copy Code
    ' Context menu will not be shown
    FpSpread1.ContextMenuStrip = Nothing
    

    Limitation

    Spread does not support built-in context menu during design time.

    See Also