B4A Library [B4X] ComboBoxPlus - ComboBox with individual color configurable items

I wanted to be able to change the color of individual items in the Combobox list (playing card application) so I created my own ComboBox based on xCustomlistview and BCTextEngine. The result is a highly configurable ComboBox due to the versatility of BCTextEngine. I put some additional effort to make it cross platform. Feel free to modify the code for your own use. BBTextEngine allows you to make configurable other text attributes such as bold, underline, inserting inline images, etc.. Just rename .b4xlib to .zip and extract module and layout files.

ComboBoxPlusExample1.gif



Installation Instructions
  1. Download and put in your additional libraries folder - ComboBoxPlus.b4xlib
  2. It should appear as one of your libraries - Check it
  3. Add these additional libraries
    • B4J - BCTextEngine, ByteConverter, XUI Views
    • B4A - BCTextEngine, ByteConverter, Reflection, xCustomListView
    • B4I - BCTextEngine, iRandomAccessFile, iXUI Views

Setting Colors for ComboBox Items
  1. In Designer View, you should be able to access under Add View>CustomView>ComboBoxPlus and place it on you layout.
  2. You can set the colorkey colors. You have 6 configurable choices with index = 0 to 5
Capture.PNG


Loading ComboBoxPlus
  1. Use the additems function. For each item, you can now assign a text and color (colorkey = 0-5).
.
B4X:
For x =  0  To  10
        ComboBoxPlus1.AddItems(“text here”, “colorkey here”, "enabled" - true,false)
Next


ComboBoxPlus
Author: Gregory Chao

  • Events
    • selected (index as int, value As string) - triggers when user makes selection
    • changed (index as int, value As string) - triggers when user changes selection or selection is changed through code (I.e. SetSelected).
  • Functions/Properties
    • AddItems (cd As String, colorkey As Int, enabled as boolean) - Adds item to ComboBox along with color defintion
    • AddItemAt (Position As Int, cd As String, colorkey As Int, enabled as boolean) - Inserts an item to ComboBox
    • RemoveItemAt (Position As Int) - Removes an item from ComboBox
    • Clear - Clears all items from ComboBox
    • SetSelected (Index As Int) - Sets the selected item.
    • SetItemColor (Index as Int, colorkey as int) - Changes color of an item
    • SetEnabled (Index as int, enabled as boolean)
    • GetItemColorkey (Index as int) as int - Gets colorkey of an item
    • GetSelectedIndex As Int - Gets the selected index
    • GetSelectedValue As String - Gets the selected item
    • GetValue (Index As Int) As String - Gets an item given its index
    • GetIndex (Value As String) As Int - Gets an index given an item
    • GetEnabled (Index as int) as boolean
    • GetVersion as String - Gets the version
    • IsVisible as Boolean - Get the visible state
    • Show - Make visible
    • Hide - Make invisible
    • GetItems As List - Gets all items
Changelog
  • 1.00 - Release
  • 2.00 - Added enabled feature
  • 3.00 - Added Show, Hide, IsVisible, and GetItems
  • 4.00 - Fixed Library reference in B4i version. Incorrect reference to iXUI and ByteConverter, Fixed B4i pull down visibility
  • 5.00 - Added GetItemColorkey feature
  • 6.00 - Disable can now be signified by graying entry string or background (use Designer to choose). Designer size is now compact size, you must enter expanded height in Designer (in line with standard practice).
Example

I was unable to upload my sample project due to size limitations. Instead, Create a B4xpages project, follow install steps. Add ComboBoxPlus1 in Designer. Copy following code into your B4XMainPage (B4j).

Special Note: In B4a and B4j, you will need to insert a "Sleep(0)" before loading (see code below). Since I load layouts in the Class Module, they cannot be done at the same time. The Sleep(0) prevents this from happening.



B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
 
    Private ComboBoxPlus1 As ComboBoxPlus
    Private ckey, ccard As List
 
    Private Spade As String = Chr(9824)
    Private Heart As String = Chr(9829)
    Private Diamond As String = Chr(9830)
    Private Club As String = Chr(9827)
    Private ButtonClear As Button
    Private ButtonGet As Button
    Private ButtonInsert As Button
    Private ButtonRemove As Button
    Private ButtonLoad As Button
 
End Sub

Public Sub Initialize
 
End Sub

'This event will be called once, before the page becocmes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    'set up mapping
    Dim SuitMapping As Map = CreateMap(1:Spade, 2:Heart, 3:Club, 4:Diamond)
    Dim CardMapping As Map = CreateMap(1:"A", 2:"K", 3:"Q", 4:"J", 5:"T", 6:"9", 7:"8", _
                                       8:"7", 9:"6", 10:"5", 11:"4", 12:"3", 13:"2")                                           
    ckey.Initialize
    ccard.initialize
    For SuitNo = 1 To 4
        For CardNo = 1 To 13
            If SuitMapping.Get(SuitNo) = Spade Or SuitMapping.Get(SuitNo)= Club Then
                ckey.add(0)
            Else
                ckey.Add(1)
            End If
            ccard.Add(SuitMapping.Get(SuitNo) & " " & CardMapping.Get(CardNo))
        Next
    Next

    'load a CLV
#if b4a
    Sleep(0)
#else if B4i
    Sleep(0)
#End If
    For x =  0 To ckey.Size - 1
        ComboBoxPlus1.AddItems(ccard.Get(x), ckey.Get(x), True)
    Next
    ComboBoxPlus1.SetSelected(2)
    ComboBoxPlus1.SetItemColor(3,5)
    ComboBoxPlus1.SetItemColor(4,3)
    ComboBoxPlus1.SetItemColor(5,1)
    ComboBoxPlus1.SetItemColor(14,0)
    ComboBoxPlus1.SetEnable(1, False)
    Log(ComboBoxPlus1.GetEnable(1))
 
    'ComboBoxPlus1.RemoveItemAt(5)
    'ComboBoxPlus1.AddItemAt(5,Spade&" "&Diamond& " Hello",1, true)
    'ComboBoxPlus1.clear
 
    Log(ComboBoxPlus1.GetSelectedIndex)
    Log(ComboBoxPlus1.GetSelectedValue)
    Log(ComboBoxPlus1.GetValue(10))
    Log(ComboBoxPlus1.GetIndex(Spade & " " & "9"))
    Log(ComboBoxPlus1.GetVersion)

    Log("Start")
    Dim AllItems As List = ComboBoxPlus1.GetItems
    For Each a As String In AllItems
        Log(a)
    Next
    Log("End")
End Sub

Private Sub ComboBoxPlus1_Selected (index As Int, value As String)
    Log($"${index}, ${value}"$)
End Sub

Private Sub ComboBoxPlus1_Changed (index As Int, value As String)
    Log($"${index}, ${value}"$)
End Sub
 

Attachments

  • ComboBoxPlus.b4xlib
    16.1 KB · Views: 34
Last edited:

Erel

Administrator
Staff member
Licensed User
.b4xlib Version
  1. Download and put in your additional libraries folder - ComboBoxPlus.b4xlib
  2. It should appear as one of your libraries - Check it
  3. Add these additional libraries
    • B4J - BCTextEngine, ByteConverter, XUI Views
    • B4A - BCTextEngine, ByteConverter, Reflection, xCustomListView
    • B4I - BCTextEngine, ByteConverter, iRandomAccessFile, iXUI Views
Class Module Version
1. No need to add the libraries as they are already declared in the b4xlib manifest file.
2. No reason to include the separate modules. It is just more work for you. You can say that the source is included in the b4xlib and you allow to modify it.
 

gregchao

Member
Licensed User
1. No need to add the libraries as they are already declared in the b4xlib manifest file.
2. No reason to include the separate modules. It is just more work for you. You can say that the source is included in the b4xlib and you allow to modify it.
Thanks for the tip. Done.
 
Top