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.
REPRO
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)
Try
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
Spreadsheet1.ActiveGrid.CellContextMenu.Items.Add(PasteSpecial)
End If
If Spreadsheet1.ActiveGrid.CellContextMenu.Items.ContainsKey("InsertCommentMenuItem") Then
Spreadsheet1.ActiveGrid.CellContextMenu.Items.RemoveByKey("InsertCommentMenuItem")
End If
Catch ex As Exception
End Try
Spreadsheet1.ActiveGrid.Refresh()
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)
EXPECTED RESULT
Same as 1st call context menu display
ACTUAL RESULT
Null Reference exception thrown
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Syncfusion.Spreadsheet.Windows
StackTrace:
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