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. The result is a highly configurable ComboBox. I put some additional effort to make it cross platform. Feel free to modify the code for your own use. Just rename .b4xlib to .zip and extract module and layout files.




Installation Instructions
  1. Download and put in your additional libraries folder - ComboBoxPlusL.b4xlib
  2. It should appear as one of your libraries - Check it
  3. Add these additional libraries
    • B4J - ByteConverter, XUI Views
    • B4A - ByteConverter, Reflection, xCustomListView
    • B4I - 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


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
        ComboBoxPlusL1.AddItems(“text here”, “colorkey here”, "enabled" - true,false)
Next


ComboBoxPlusL
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).
    • opened() - triggers when user opens combobox
  • 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
    • SetEnable (Index as int, enabled as boolean) - sets enabled of an item
    • 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
    • GetEnable (Index as int) as boolean - Gets enabled of an item
    • GetVersion as String - Gets the version
    • Visible as Boolean - Get or setsthe visible state
    • GetItems As List - Gets all items
    • ExpandedHeight As Float - Gets or sets height when expanded
    • FontSize as Float - Gets or sets Fontsize
    • Close - forces the Combobox to close
    • AutoClose as Boolean - Gets or sets whether combobox will close after selection (default is true)
    • Enabled as Boolean - Gets or sets enable of combobox
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).
  • 8.00 - fixed button size scaling; in B4i, allow only graying box (no gray text) when disabled; added GetExpandedHeight and SetExpandedHeight
  • 9.00 - fixed fontscaling to respond to "Autoscale All", added Get/Set Fontsize
  • 10.00 - fixed a minor bug related to label BBLabel size
  • 15.00 - added close and autoclose feature
  • 16.00 - Speed up optimizations (removed BBLabel, CLV item layout), Font type by list selection
  • 17.00 - Event Opened, added enabled feature on Combobox
  • 18.00 - Fixed Error if Event Open not defined in code
  • 19.00 - Fixed fontsize issue in B4a
  • 20.00 - Fixed error occuring if you don't define callback
Example

Follow this link for a full example. ComboBoxPlusL is a module in the example so you can see/modify the code if you like. To use library version, you will need download the library below and place it in your library folder. Then, select the library to include it and delete the ComboBoxPlusL module.

https://www.dropbox.com/scl/fi/3q1jkmif2qjozkot2mfgn/ComboBoxLibraryLite-20240224v20.zip?rlkey=p7nd4xc1ijkhpnpizwrsdkj2c&dl=0

You will need to enable following libraries:

B4J.DependsOn=ByteConverter, XUI Views
B4A.DependsOn=ByteConverter, xCustomListView
B4i.DependsOn=iRandomAccessFile, XUI Views

Special Note: It is highly advised to load the ComboBoxPlusL in the B4Xpages_Appear subroutine. For B4i, this is especially important with the "Autoscale All" engaged. This is due to the fact that the size changes from "Autoscale All" do not register until the Appear occurs. For B4a, if you load in B4Xpages_Create, you need to put a sleep(0) before loading. B4j does not seem to have these issues so you can load in B4Xpages_Create or B4X_Appear without a sleep(0).
 

Attachments

  • ComboBoxPlusL.b4xlib
    9.8 KB · Views: 331
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 

gregchao

Active Member
Licensed User
Longtime 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.
 

mauriegio

Member
Licensed User
Longtime User
hello gregchao,

thank for comboBoxPlus is very usefull ....

I found a problem if i put two combobox one under other the first combo when open the list go in background
and i can't see them element in the list.

Thank you very much
 

gregchao

Active Member
Licensed User
Longtime User
hello gregchao,

thank for comboBoxPlus is very usefull ....

I found a problem if i put two combobox one under other the first combo when open the list go in background
and i can't see them element in the list.

Thank you very much
I posted an update with some fixes. See if you still have the problem. Let me know if you are running B4a, B4i, or B4j
 

mauriegio

Member
Licensed User
Longtime User
Hello gregchao,

thank you for your quickly response.

I tested new version on B4a, but i have the same problem.

Another problem is very slow to additem , is normal or not ?

best regard
 

gregchao

Active Member
Licensed User
Longtime User
Hello gregchao,

thank you for your quickly response.

I tested new version on B4a, but i have the same problem.

Another problem is very slow to additem , is normal or not ?

best regard
I put a link to a full example so you can study it (see original write up above under "example"). You should be able to unzip and run it directly. In the example, you will see two ComboBoxes that overlap. Note that the ComboBoxPlus is a module, not a library.

I think the slow loading is due to the sleep(0) in the Create. You can eliminate this problem by loading in the Appear routine.
 
Last edited:

Jerryk

Member
Licensed User
Longtime User
I try it used in my large project:
B4XMainPage:
in Sub Class_Globals:
Private cbp As ComboBoxPlusL
...

Private Sub B4XPage_Appear
    For i = 0 To 5
        cbp.AddItems("aaaa", 0, True)
    Next
End Sub

error on first line in AddItems function:
ComboBoxPlusL:
Public Sub AddItems(cd As String, colorkey As Int, enabled As Boolean)
    Dim ItemWidth As Double =  mBase.width   'same as the CLV width
...


Error occurred on line: 614 (ComboBoxPlusL)
java.lang.NullPointerException: null receiver
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6176)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 

gregchao

Active Member
Licensed User
Longtime User
Did you try to download the example and run it? What platform are you using?
 

Jerryk

Member
Licensed User
Longtime User
Hi, I solved it:
I debugged the code in your library and found that in my large project B4XPage_Appear starts before your object is initialized. This causes AddItems to crash. I moved AddItems to B4XPage_Created and put Sleep(100) before this statement. I don't know if is the right way, but it works. It would probably be better to give someone a Wait For and wait for the initialization of the object, but I'll leave that to you to solve.
I soved
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…