We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Removing an Item from context menu throws Object not set on 2nd call

I was attempting to use the context menu functionality in the spreadsheet component.    I used an example to add a menu item but wanted to remove other items for certain columns/row.   So attempted to remove a menu item.   This worked the 1st time the method is called but upon using the context menu it throws an object not set exception.

1. Create a windows form application with spreadsheet control on a form

2. Add the following code.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler Spreadsheet1.ActiveGrid.CellContextMenuOpening, AddressOf ActiveGrid_CellContextMenuOpening
        Spreadsheet1.IsCustomTabItemContextMenuEnabled = False
    End Sub

    Private Sub ActiveGrid_CellContextMenuOpening(sender As Object, e As CellContextMenuOpeningEventArgs)
            If Spreadsheet1.ActiveGrid.CellContextMenu.Items.ContainsKey("PasteSpecial") = False Then
                '//Adding Customized Menu item
                Dim PasteSpecial = New ToolStripMenuItem() With {.BackColor = Color.White, .Name = "PasteSpecial"}
                PasteSpecial.Text = "PasteSpecial"

                AddHandler PasteSpecial.Click, AddressOf PasteSpecial_Click
            End If

            If Spreadsheet1.ActiveGrid.CellContextMenu.Items.ContainsKey("InsertCommentMenuItem") Then
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub PasteSpecial_Click(sender As Object, e As EventArgs)

    End Sub

3. Run the application 
4. Right click to display the context menu.    (The new pasteSpecial item is present and the Insert Comment has been removed.   )
5. Move to another cell and Right click to display the context menu (EXCEPTION IS THROWN)

Same as 1st call context menu display

Null Reference exception thrown

  Message=Object reference not set to an instance of an object.
   at Syncfusion.Windows.Forms.Spreadsheet.SpreadsheetGrid.RefreshCellContextMenu()
   at Syncfusion.Windows.Forms.Spreadsheet.SpreadsheetGrid.OnCellContextMenuOpening(CellContextMenuOpeningEventArgs e)
   at Syncfusion.Windows.Forms.CellGrid.SfCellGrid.ContextMenuStripOpening(RowColumnIndex rowcolumnindex)
   at Syncfusion.Windows.Forms.Spreadsheet.VisualContainerExt.OnMouseDown(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at Syncfusion.Windows.Forms.CellGrid.VisualContainer.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at WindowsApp2.My.MyApplication.Main(String[] Args) in :line 81

3 Replies

AA Arulraj A Syncfusion Team January 24, 2019 05:44 AM UTC

Thanks for your update. 
To remove the InsertCommentMenuItem from CellContexMenu, you could use the Visible property to hide this menu item from the CellContextMenu. Please refer the following code example. 
If Me.spreadsheet.ActiveGrid.CellContextMenu.Items.ContainsKey("InsertCommentMenuItem") Then 
    Me.spreadsheet.ActiveGrid.CellContextMenu.Items("InsertCommentMenuItem").Visible = False 
    'To hide the separator of InsertCommentMenuItem 
    Me.spreadsheet.ActiveGrid.CellContextMenu.Items("Separator2").Visible = False 
End If 
Some settings implemented for all menu items in our source. So, If you remove any menu item from default CellContextMenu which will throw the null exception so you should not remove any item from the default CellContextMenu. 
Arulraj A 

SP Spotty January 24, 2019 03:46 PM UTC

Using visible property will work but perhaps if there are items that cannot be removed without problems, then preventing removal of these items in the 1st place would be a better course of action and documenting which items cannot be removed and suggesting visibility as a workaround - especially in the samples/user guide relating to context menus - detailing which ones we are talking about specifically.

In this case I wanted to remove insert comments as it was not relevant to anything I wanted to enable.   These context menu's are contextual by definition and hence "items which cannot be removed" may differ in the context menu generated.   

Is there any reason why the removal was OK on the 1st call - but subsequent calls it wasn't ?    It would appear that the functionality is partially working.    

AA Arulraj A Syncfusion Team January 25, 2019 12:06 PM UTC

Hi Spotty, 
Thanks for your feedback. 
We have considered this case. We have logged a bug report for this issue “Null reference exception occurs when remove the menu item from default cell context menu”. It will be available on our next main release 2019 Volume1 which will expected at first week of March 2019. 

Arulraj A 

Live Chat Icon For mobile
Up arrow icon