B4J Question Combobox - Item is an object (class)

ranul

Member
Licensed User
Longtime User
Hi,

My combobox items are actually objects (a class I wrote).

This is the class:

Sub Class_Globals
Private Code As Int
Private Description As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(aCode As Int, aDesc As String)
setCode(aCode)
setDescription(aDesc)
End Sub
public Sub setCode(aCode As Int)
Code = aCode
End Sub
public Sub getCode() As Int
Return Code
End Sub
public Sub setDescription(aDesc As String)
Description = aDesc
End Sub
public Sub getDescription() As String
Return Description
End Sub

In the main module I initialize the combobox and I add items.

Dim c as MyClass
c.initialize(1,"One")
combo.items.add(c)

How do I tell the combobox to display the string (Description)?

Thanks.
 

Daestrum

Expert
Licensed User
Longtime User
Probably the easiest way is to add a routine in your class that returns a pane with the info as labels, then add that to the combobox.

example

In your Class add
B4X:
Sub getAsPane As Pane
 Dim p As Pane
 p.Initialize("")
 Dim l As Label
 l.Initialize("")
 l.Text = code
 Dim ll As Label
 ll.Initialize("")
 ll.Text = description
 p.AddNode(l,0,0,30,30)         ' change these to suit how it looks
 p.AddNode(ll,20,0,100,30)
 Return p
End Sub


Where you add to combobox
B4X:
Dim c as MyClass
c.initialize(1,"One")
combo.items.add(c.AsPane)  ' although sub is called getAsPane B4j will remove the get

If you just want to display the description without the code in the combobox
B4X:
combo.items.add(c.Description)
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…