简体   繁体   中英

how to add existing items into collection in c#

I am developing a common smartTag panel and deriving the base smartTag form my project. I want to add the existing action items of base smarttag in derived smart tag. I want to add the items of base panel below the items of derived panel. Is there any easy way to add the base items without foreach directly below the action items?

public override DesignerActionItemCollection GetSortedActionItems()
{
    DesignerActionItemCollection actionItems = new DesignerActionItemCollection();

    //adds the new smart tag action items.
    actionItems.Add(new DesignerActionHeaderItem("MySmartTag Support"));
    actionItems.Add(new DesignerActionPropertyItem("BackColor", "Back Color"));
    actionItems.Add(new DesignerActionPropertyItem("ForeColor", "Fore Color"));

    //adds the action items from base smart tag.
    foreach (DesignerActionItem baseItem in base.GetSortedActionItems())
    {
        actionItems.Add(baseItem);
    }
    return actionItems;
}

I am adding the base action items below the new action items with for loop, is there any way to avoid the loop and minimize the code?

I have found the answer, inserting is the best option.

public override DesignerActionItemCollection GetSortedActionItems()
{
   DesignerActionItemCollection actionItems = base.GetSortedActionItems();

   //inserts the new smart tag action items.
   actionItems.Insert(0, new DesignerActionHeaderItem("MySmartTag Support"));
   actionItems.Insert(1, new DesignerActionPropertyItem("BackColor", "Back Color"));
   actionItems.Insert(2, new DesignerActionPropertyItem("ForeColor", "Fore Color"));

   return actionItems;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM