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
    • GetExpandedHeight As Float - Gets height when expanded
    • SetExpandedHeight As Float - Sets height when expanded
    • GetFontSize as Float - Gets Fontsize
    • SetFontSize as Float - Sets Fontsize
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
Example

Follow this link for a full example. ComboBoxPlus 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 ComboBoxPlus module.


You will need to enable following libraries:

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

Special Note: It is highly advised to load the ComboBoxPlus 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 and it will cause a noticeable delay. B4j does not seem to have these issues so you can load in B4Xpages_Create or B4X_Appear without a sleep(0).
 

Attachments

  • ComboBoxPlus.b4xlib
    16.6 KB · Views: 80
Last edited:

Erel

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

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

Member
Licensed 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

Member
Licensed 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:
Top