Get a flattened list of all RadMenuItems in...

Thread is closed for posting
2 posts, 0 answers
  1. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 16 Oct 2010 Link to this post

    Requirements

    RadControls for WinForms

    Q2 2010 

    .NET version

    Any

    Visual Studio version

    Any

    programming language

    C# and VB.NET


    PROJECT DESCRIPTION 
    I put together this little function for something that wasn't required in the end but thought it could be quite useful sometimes. 

    This code returns a collection of RadMenuItemBase objects from a given control that contains RadMenuItemBase objects 

    For example: Use it to get back a collection of all RadMenuItemBase objects in RadMenu, RadApplicationMenu, RadSplitButton 
    VB:
    Public Shared Function GetRadMenuItems(ByVal items As Telerik.WinControls.RadItemCollection) As ReadOnlyCollection(Of RadMenuItemBase)
        Dim returnItems As New List(Of RadMenuItemBase)()
        For Each item As RadMenuItemBase In items
            returnItems.Add(item)
            Dim subItems As ReadOnlyCollection(Of RadMenuItemBase) = GetRadMenuItems(item.Items)
            returnItems.AddRange(subItems)
        Next
        Dim readOnlyItems As New ReadOnlyCollection(Of RadMenuItemBase)(returnItems)
        Return readOnlyItems
    End Function
    C#: 
    public static ReadOnlyCollection<RadMenuItemBase> GetRadMenuItems(Telerik.WinControls.RadItemCollection items)
    {
        List<RadMenuItemBase> returnItems = new List<RadMenuItemBase>();
        foreach (RadMenuItemBase item in items) {
            returnItems.Add(item);
            ReadOnlyCollection<RadMenuItemBase> subItems = GetRadMenuItems(item.Items);
            returnItems.AddRange(subItems);
        }
        ReadOnlyCollection<RadMenuItemBase> readOnlyItems = new ReadOnlyCollection<RadMenuItemBase>(returnItems);
        return readOnlyItems;
    }

    The code can easily be adapted too to get back a flattened collection of items from other types,
    For example: Adapt it and use it to get back a collection of RadtreeNode objects in a RadTreeView
    VB:
    Public Shared Function GetRadTreeNodes(ByVal nodes As Telerik.WinControls.UI.RadTreeNodeCollection) As ReadOnlyCollection(Of RadTreeNode)
        Dim returnNodes As New List(Of RadTreeNode)()
        For Each node As RadTreeNode In nodes
            returnNodes.Add(node)
            Dim subNodes As ReadOnlyCollection(Of RadTreeNode) = GetRadTreeNodes(node.Nodes)
            returnNodes.AddRange(subNodes)
        Next
        Dim readOnlyNodes As New ReadOnlyCollection(Of RadTreeNode)(returnNodes)
        Return readOnlyNodes
    End Function
    C#:
    public static ReadOnlyCollection<RadTreeNode> GetRadTreeNodes(Telerik.WinControls.UI.RadTreeNodeCollection nodes)
    {
        List<RadTreeNode> returnNodes = new List<RadTreeNode>();
        foreach (RadTreeNode node in nodes) {
            returnNodes.Add(node);
            ReadOnlyCollection<RadTreeNode> subNodes = GetRadTreeNodes(node.Nodes);
            returnNodes.AddRange(subNodes);
        }
        ReadOnlyCollection<RadTreeNode> readOnlyNodes = new ReadOnlyCollection<RadTreeNode>(returnNodes);
        return readOnlyNodes;
    }

    Regards,
    Richard
  2. Nikolay
    Admin
    Nikolay avatar
    1805 posts

    Posted 21 Oct 2010 Link to this post

    Hello Richard,

    Thank you for sharing these code snippets. I have updated your Telerik points accordingly.

    Regards,
    Nikolay
    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
Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.