DevComponents.com

How to find CrumBarItem in CrumbBar control by name

Here is code that shows how to enumerate all items in CrumbBar control and find one by name. You can modify it to search by any other criteria.

VB:
This is code that is part of the setup and used to test find routine:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim item As CrumbBarItem
        Dim childItem As CrumbBarItem
        item = New CrumbBarItem()
        item.Name = "item1"
        item.Text = "Item 1"
        CrumbBar1.Items.Add(item)
 
        childItem = New CrumbBarItem()
        childItem.Name = "item2"
        childItem.Text = "Child Item 1"
        item.SubItems.Add(childItem)
 
        childItem = New CrumbBarItem()
        childItem.Name = "item3"
        childItem.Text = "Child Item 2"
        item.SubItems.Add(childItem)
 
 
        Console.WriteLine(FindCrumbBarItem(CrumbBar1, "item1"))
        Console.WriteLine(FindCrumbBarItem(CrumbBar1, "item2"))
        Console.WriteLine(FindCrumbBarItem(CrumbBar1, "item3"))
 
    End Sub

This is code that performs actual lookup:

    Private Function FindCrumbBarItem(ByVal subitems As IList, ByVal name As String) As CrumbBarItem
        For Each item As CrumbBarItem In subitems
            If item.Name = Name Then
                Return item
            End If
            Dim childItem As CrumbBarItem = FindCrumbBarItem(item.SubItems, name)
            If Not childItem Is Nothing Then Return childItem
        Next
        Return Nothing
    End Function
    Private Function FindCrumbBarItem(ByVal crumbBar As CrumbBar, byval name as String) As CrumbBarItem
        Return FindCrumbBarItem(crumbBar.Items, name)
    End Function

C#:
This is code that is part of the setup and used to test find routine:
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    CrumbBarItem item = default(CrumbBarItem);
    CrumbBarItem childItem = default(CrumbBarItem);
    item = new CrumbBarItem();
    item.Name = "item1";
    item.Text = "Item 1";
    CrumbBar1.Items.Add(item);
 
    childItem = new CrumbBarItem();
    childItem.Name = "item2";
    childItem.Text = "Child Item 1";
    item.SubItems.Add(childItem);
 
    childItem = new CrumbBarItem();
    childItem.Name = "item3";
    childItem.Text = "Child Item 2";
    item.SubItems.Add(childItem);
 
 
    Console.WriteLine(FindCrumbBarItem(CrumbBar1, "item1"));
    Console.WriteLine(FindCrumbBarItem(CrumbBar1, "item2"));
 
    Console.WriteLine(FindCrumbBarItem(CrumbBar1, "item3"));
}

This is code that performs actual lookup:

private CrumbBarItem FindCrumbBarItem(IList subitems, string name)
{
    foreach (CrumbBarItem item in subitems)
    {
        if (item.Name == Name)
        {
            return item;
        }
        CrumbBarItem childItem = FindCrumbBarItem(item.SubItems, name);
        if ((childItem != null)) return childItem;
    }
    return null;
}
private CrumbBarItem FindCrumbBarItem(CrumbBar crumbBar, string name)
{
    return FindCrumbBarItem(crumbBar.Items, name);
}


Would you like to...

Print this page Print this page

Email this page Email this page

Post a comment Post a comment

Subscribe me

Add to favoritesAdd to favorites

User Opinions (21 votes)

100% thumbs up 0% thumbs down

How would you rate this answer?

Helpful
Not helpful
Thank you for rating this answer.

Related Questions

No related questions were found.

Attachments

No attachments were found.