DesktopListBox subclass

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

You need to add a DesktopListbox to the project, but turn off the “Implicit Instance” flag. Then create your new instance based on that added control.

Add a named control DTListbox
Change your code to:

Var cpListBox As New DTListBox
...

Thanks for your quick answer,
I’m not sure I understand, I did add the class to the project:

#tag Class
Protected Class CPListBox
Inherits DesktopListBox

#tag ViewBehavior
...
#tag EndViewBehavior
End Class
#tag EndClass

What is a DTListBox ?
I’m not familiar with implicit instance, I did turn it off on the window, no change.

@Tim_Jones wrote:

You need to add a DesktopListbox to the project, but turn off the “Implicit Instance” flag. Then create your new instance based on that added control.

Add a named control DTListbox

The added DesktopListbox to the project is named DTListbox. To create a instance of that object, you need to use New as shown:

Var cpListBox As New DTListBox

Hope that helps !

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.

How do you add a DesktopListBox to the project to obtain a DTListBox ?
I’m confused, following this tutorial:

It seems it should work the same with DesktopListBox

I told you:

  1. Add a generic class from the Library to your project

  2. Change its Super to be DesktopListbox (its icon will change, too)

  3. 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).

You just need to set the DefaultRowHeight property:

Var cpListBox As New CPListBox

cpListBox.FontName = "System"
cpListBox.FontSize = 12

cpListBox.DefaultRowHeight = 22

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 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.

3 Likes

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.

1 Like

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 :slight_smile:

1 Like

Happy to help. Don’t forget to mark a solution so it’s easy to find for users who may encounter the same issue in the future.

1 Like