Problem combining context menus

4 posts, 1 answers
  1. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 30 Jun 2010 Link to this post

    Hello,

    there is a strange problem adding some RadContextMenuItems to the context menu from a GridView.

    It works fine to catch the ContextMenuOpening event and add the Items (3 Separators and 4 MenuItems in this case) from a RadContextMenu on the form by the following code snippet:

     
            Private Sub DataGridViewMain_ContextMenuOpening(ByVal sender As System.ObjectByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles DataGridViewMain.ContextMenuOpening  
                Try 
                    e.ContextMenu.Items.AddRange(RadContextMenuGridView.Items)  
                Catch 
                End Try 
            End Sub 
     

    But sometimes after 3, sometimes after 10 or later tries the AddRange throws an exception:
        "Das angegebene Element kann nicht entfernt werden, da es nicht in der Auflistung gefunden wurde."

    The context menu shows without the custom added items. The problem is not the contextmenu but the items collection that should be added. It seems that it is blocked by the old context menu instance and not released properly.
    I could not workaround this. I have to create a new instance of the form to get it work again.
    Is there a fix for this or is it possible to duplicate the Item Collection so that there is no blocking?

    Thank you!

    Bernd


  2. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 01 Jul 2010 Link to this post

     
            Private Sub DataGridViewMain_ContextMenuOpening(ByVal sender As System.ObjectByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles DataGridViewMain.ContextMenuOpening  
                Try 
                    For Each itemContext In e.ContextMenu.Items  
                        For Each itemMenu In RadContextMenuGridView.Items  
                            If itemMenu.Text.Equals(itemContext.Text) Then 
                                RadContextMenuGridView.Items.Remove(itemMenu)  
                                Exit For 
                            End If 
                        Next 
                    Next 
                    For i As Integer = 0 To e.ContextMenu.Items.Count - 1  
                        RadContextMenuGridView.Items.Insert(i, e.ContextMenu.Items(i))  
                    Next 
                    e.Cancel = True 
                    RadContextMenuGridView.Show(sender.MousePosition)  
                Catch 
                End Try 
            End Sub 
     
     

    Okay, i had to use this workaround for the moment. Now i add the items from the normal context menu to my own menu not the other way. First i delete the old entries from the last time then the items from the official context menu are added to my own context menu. Then cancel the context menu and show your own.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Georgi
    Admin
    Georgi avatar
    296 posts

    Posted 06 Jul 2010 Link to this post

    Hello Bernd Müller,

    Thank you for your question.

    Context menus are completely dynamic in RadGridView and hence their items are created, displayed and disposed on the fly. Since you are using static items, at a certain point they are disposed by a disposing menu and that is why you cannot access them. As Dispose process is controlled by the Garbage Collector, it may occur at any time during application's lifetime.

    You have two options here:

    - Create dynamic context menu items and pass new instances on each menu opening
    - Remove your static items upon each menu closing

    I hope this information is useful. Let me know whether you need further assistance.

     

    All the best,
    Georgi
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 06 Jul 2010 Link to this post

     
            Private Sub DataGridViewMain_ContextMenuOpening(ByVal sender As System.ObjectByVal e As Telerik.WinControls.UI.ContextMenuOpeningEventArgs) Handles DataGridViewMain.ContextMenuOpening  
                Try 
                    For i As Integer = RadContextMenuGridView.Items.Count - 1 To 0 Step -1  
                        If RadContextMenuGridView.Items(i).Tag = "RAD" Then 
                            RadContextMenuGridView.Items.Remove(RadContextMenuGridView.Items(i))  
                        End If 
                    Next 
                    For i As Integer = 0 To e.ContextMenu.Items.Count - 1  
                        RadContextMenuGridView.Items.Insert(i, e.ContextMenu.Items(i))  
                        RadContextMenuGridView.Items(i).Tag = "RAD" 
                    Next 
                    e.Cancel = True 
                    RadContextMenuGridView.Show(sender.MousePosition)  
                Catch 
                End Try 
            End Sub 
     

    Okay, thanks for the info. For now i stay with my static menu and just add the default items from the grid context menu. Just for the record here is my current version that tags all default items with "RAD" to better remove them.
Back to Top