Add Items in ComboBox "WinForms"

Hi everyone,

I’m trying to make a winform that let a user select a Level (keys and values) via a dropdown menu.

I’ve created the form and the ComboBox and now I tried to insert the list of level names (keys) in the dropdown menu.

Unfortunaley, when I run the form, I got an “IronPython,List.Run” in my dropdown.

2020-05-26 12_33_25-Window
I tried to flatten my keys list but it didn’t work either… I’m pretty sure that I’m calling it wrong.

Here is the python code (it’s all in one python node btw) :

#Import boiler plate
import clr

clr.AddReference(“System.IO”)
clr.AddReference(“System.Drawing”)
clr.AddReference(“System.Reflection”)
clr.AddReference(“System.Threading”)
clr.AddReference(“System.Windows.Forms”)
clr.AddReference(“RevitAPI”)
clr.AddReference(“RevitServices”)

import System
import System.IO
import System.Drawing
import System.Reflection
import System.Windows.Forms
from System.Threading import ApartmentState, Thread, ThreadStart
from Autodesk.Revit.DB import FilteredElementCollector, Level
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

values = FilteredElementCollector(doc).OfClass(Level).ToElements()
keys =

for value in values:
keys.append(value.Name)

def Flatten(keys):
listout =
for x in keys:
for y in x:
listout.append(y)
return listout

Create Aplication Object

class SelectLevelForm(System.Windows.Forms.Form):

def __init__(self):
	
	#Define control container
	self.components = System.ComponentModel.Container()
	
	# Define the caption text
	self.Text = "Test"
	
	# Define the color of our form
	self.Backcolor = System.Drawing.Color.FromArgb(238,238,238)
	
	# Define te size of my form
	self.ClientSize = System.Drawing.Size (600,600)

	# Create a new combobox in the form
	self.TypesComboBox = System.Windows.Forms.ComboBox()
	
	#Define the size and location of the combobox
	self.TypesComboBox.Location = System.Drawing.Point(30,100)
	self.TypesComboBox.Size = System.Drawing.Size(540,100)
	
	# Change the Dropdown Height
	
	self.TypesComboBox.DropDownHeight = 50
	
	#Add items to ComboBox
	
	self.TypesComboBox.Items.Insert(0, Flatten(keys))
	
	"""
	ADD Controls to our form
	"""
	self.Controls.Add(self.TypesComboBox)
	
	
def dispose(self):
	
	#Dispose of components
	self.components.Dispose()
	System.Windows.Forms.Form.Dispose(self)

System.Windows.Forms.Application.Run(SelectLevelForm())

if you have any idea or suggestion let me know

Thank you in advance ! Stay safe!

Hi,

Try:

self.TypesComboBox.Items.AddRange(keys)

Where keys is a List with strings.

Hi,

Unfortunately, I got an error saying that it need an Array[Object], got List Instead …
2020-05-27 10_56_37-Window script code section

But I really appreciate your answer :smiley:

Oh I did it!

I needed to do a for loop with the keys :

  for key in keys:
  	self.TypesComboBox.Items.Add(key)

2020-05-27 11_23_01-Window

Aaah okay true.

First add this to your “Boilerplate” part:

clr.AddReference("System");
from System import *
clr.AddReference("System.Collections")
from System.Collections.Generic import *

Next, you need to convert your Python List, to an Array.

array = Array[Object](keys) #Convert Python list to a .Net Array
self.TypesComboBox.Items.AddRange(array) #Add the Array to the Combobox

Another way is with an extra step

list = List[Object](keys) #Convert  Python list to a .Net List
array = list.ToArray() #Convert .Net List to an Array
self.TypesComboBox.Items.AddRange(array) #Add the Array to the Combobox
2 Likes

Ok I’ll try this to. Really appreciate your help :smiley: Have a good day!

Sure anytime, let me know if it works!