I’m new to Xojo,
I’m trying to add a custom DesktopListBox on a window programmatically.
When I do so (see simple code below), the items are not displayed, I just have an empty ListBox with white background:
Var cpListBox As CPListBox = New CPListBox
cpListBox.FontName = "System"
cpListBox.FontSize = 12
Self.AddControl(cpListBox)
cpListBox.AddRow("2023")
cpListBox.AddRow("2022")
cpListBox.AddRow("2021")
cpListBox.AddRow("2020")
cpListBox.Top = 10
cpListBox.Left = 10
cpListBox.Width = 200
cpListBox.Height = 200
I can only manage to view my custom DesktopListBox when I instanciate it through the IDE by dragging the class from the Library on my Window, then adding items on this instance.
What am I missing ?
Thank you
In these cases I tend to add a generic class from the Library to the project, then change its Super to DesktopListbox, then start adding any methods, properties, to it. That makes it appear in the Project Controls area of the Library in the IDE, and I can add instances of it to the layout in the Project as I need them. Or instantiate them in code.
Add a generic class from the Library to your project
Change its Super to be DesktopListbox (its icon will change, too)
Rename it to be DTListBox.
At this point, you have a new Project Control called DTListBox in the Project Controls area of the Library in the IDE, and you can drag/drop this to anywhere on your project layout in the IDE to create instances. You can also add methods and properties to it (not much point to the above process, if you don’t).
I would do this in the Constructor method or Opening event of the Subclass. There are other properties you’ll likely find that the framework doesn’t initialize for you when using AddControl. In this case, DefaultRowHeight is 0 unless you explicitly set it.
My question is to be able to add the control programmatically (AddControl), I said I had no problem doing it by dragging my control on the project layout.
Yes, you had everything right in my testing except the rows weren’t visible (as you said originally). The code I provided, which is only a slight modification of your original, adds the control to the window at the specified location with all of the added rows visible.
That was it !, thank you very much.
So for futur readers, this had nothing to do with naming the custom control with the specific name DTListBox, you can name it whatever you want, which is quite reassuring actually