Adding ICollection to comboBox

Adding ICollection to comboBox

Nathan.HilsonN67V6
Advocate Advocate
525 Views
5 Replies
Message 1 of 6

Adding ICollection to comboBox

Nathan.HilsonN67V6
Advocate
Advocate

Can someone help me with this? It's not throwing me any errors that I can see. Only thing I see in the debug is that the threads exit with code 0 when executing the add items method. still fairly new to Revit API. 

 

{          
            ICollection<Element> FabricationPipework =
                new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationPipework).ToElements();
            ICollection<Element> FabricationDuctwork =
                new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationDuctwork).ToElements();
            ICollection<Element> FabricationHangers =
                new FilteredElementCollector(Doc, Doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_FabricationHangers).ToElements();
            {
                foreach (Element element in FabricationPipework)
                    comboBox1.Items.Add(FabricationPipework);
            }
            {
                foreach (Element element in FabricationDuctwork)
                    comboBox1.Items.Add(FabricationDuctwork);
            }
            {
                foreach (Element element in FabricationHangers)
                    comboBox1.Items.Add(FabricationHangers);
            }
            { 
            }
            {
                comboBox1.BackColor = System.Drawing.Color.Orange;
            } 

Here is the code that I have for this specific box. What's shown below is what I added within the form. 

 

NathanHilsonN67V6_0-1653487622494.png

 

 

0 Likes
Accepted solutions (2)
526 Views
5 Replies
Replies (5)
Message 2 of 6

Nathan.HilsonN67V6
Advocate
Advocate

Update: Somewhat got it to see the collection by moving the collection methods to public but they all show as collection.

 

NathanHilsonN67V6_0-1653490940567.png

 

0 Likes
Message 3 of 6

RPTHOMAS108
Mentor
Mentor
Accepted solution

For each item in the collection you are adding the collection itself to the ComboBox e.g.:

 

 

{
    foreach (Element element in FabricationPipework)
     comboBox1.Items.Add(FabricationPipework);
}

//Should instead be:

{
    foreach (Element element in FabricationPipework)
     comboBox1.Items.Add(element);
}

 

 

That however isn't the end of your problems because then you'll have the following

 

(Element)

(Element)

(Element)...

 

You need to set the DisplayMember for the ListControl of which ComboBox inherits.

 

Personally I don't like adding API objects directly into container controls and if I were I'd opt for DataBinding rather than manually adding.

 

If I only care about the Name and the ElementId then I'll create a simple class object containing only such and put that in the collection. That then also allows .ToStrng to be Overridden and display something meaningful in the list (then there is no need to use DisplayMember). That is why you see '(Element)' instead of the Name of the Element (Value of Element.Name) because the Element class does not Override Object.ToString:

 

220525a.png

 

 

 

0 Likes
Message 4 of 6

Nathan.HilsonN67V6
Advocate
Advocate

I tried setting the display member but now I cant get the form to show & I get this message. 

 

NathanHilsonN67V6_0-1653528147408.png

 

Have no idea why I'm getting this. I've spent the last few hours searching for it but no luck. 

Message 5 of 6

dhanait_ajay13
Enthusiast
Enthusiast
Accepted solution

use Element.Name for add into combobox

Message 6 of 6

Nathan.HilsonN67V6
Advocate
Advocate

Ended up remaking the form from scratch & works just fine. Thanks for all the help! 

0 Likes