B4A Library [Library] ComboBox

This is a windows/Linux/Mac style combobox with a drop down item list. It is highly customizable and easy to use. The package includes a demo project and documentation on how to use each method. Below is a list of methods in the library.

==== NOTE ====
This is version 1.27 of the combobox. See the 1.27 changes at the end of this post. The class source is now included in the zip file.

The zip file includes a test project with the class source of the combobox.

MLComboBox
Author:
Jem Miller
Version: 1.27
  • MLComboBox
    Events:
    • ItemClick (Position As Int, Value As String)
    • ItemLongClick (Position As Int, Value As String)
    • TextChanged (Old As String, New As String)
    Methods:
    • IsInitialized As Boolean
      Tests whether the object has been initialized.
    • Additem (Item As String, Img As BitmapWrapper) As String
      Adds an item to the dropdown list. With or without a bitmap.
      Item - the text to add
      Img - The bitmap to add. Pass Null for no bitmap.
    • Additemat (Position As Int, Item As String, Img As BitmapWrapper) As String
      Adds an item to the dropdown list at a specified position.
      Position - the position to insert the item. Must be 0 to Size - 1
      Item - the text to add
      Img - The bitmap to add. Pass Null for no bitmap.
    • Asview As PanelWrapper
      Pass this to the AddView call of the Activity or Panel after you initialize the combobox.

      Example:
      cb.Initialize(Activity, Me, Panel1, "cb")
      Panel1.AddView(cb.AsView,0 ,0, 150dip, 40dip)
    • Buttoncolor (fgColor As Int, bgColor As Int) As String
      Changes the button foreground and background colors.
    • Buttonicon (Bmp As BitmapDrawable) As String
      Sets a bitmap to the button.
      The default V will NOT be visible
      The Button Is 30dip wide by Height tall, and will fill the Button centered.
    • Clear As String
      Clears all entries of the combobox and dropdown list.
    • Color (bgColor As Int) As String
      Sets the color of the combobox
    • Comboedit_textchanged (Old As String, New As String) As String
    • Dropclose As String
      Closes the dropdown list.
    • Dropcolor (newColor As Int) As String
      Sets the color of the dropdown list.
    • Dropopen As String
      Opens the dropdown list. Can be used for Menu key press.
      Self adjusts for the Activity height so the drop down does not go past the bottom of the screen.
      Also resizes itself for long lists.
    • Dropshowing As Boolean
      Returns True if the drop down box is being shown.
    • Droptextcolor (newColor As Int) As String
      Sets the text size of the dropdown list.
    • Droptextsize (newSize As Int) As String
      Sets the text size of the dropdown list.
    • Getitem (Position As Int) As String
      Returns the text at Position number given
    • Gettext As String
      Gets or sets the value of the combo box.
    • Indexof (Txt As String) As Int
      Returns the position number of the text item given in Txt.
    • Initialize (Owner As ActivityWrapper, Module As Object, Parent As ConcreteViewWrapper, Event As String) As String
      Initializes the object.
      Parent - The calling Activity.
      Caller - Module name. Must be Me
      Parent - The parent panel. Pass null if the combobox is not being added to a panel.
      Event - Name of your ItemSelected(Position as Int, Value as String) event. Use "" if no event is needed.

      Example:
      cb.Initialize(Activity, Me, Null, "cb")
    • Linecolor (newColor As Int) As String
      Changes the Line color of Borders and lines between items
    • Removeitemat (Position As Int) As Boolean
      Removes an item from the dropdown list at a specified position.
    • Setbuttonwidth (newWidth As Int) As String
      Sets the width of the drop down button. Default width is 30dip.
      The button height is the same as the component Height - 2Dip.
    • Setdefault (Position As Int) As String
      Sets the item to show in the combobox.
    • Sethint (Txt As String) As String
      Sets the hint displayed if the combo text is empty.
      Default is Touch to display list.
    • Size As Int
      Returns the number of items in the dropdown list.
    • Txtcolor (TextColor As Int) As String
      Sets the text color of the combobox.
    • Txtsize (TextSize As Int) As String
      Sets the text size of the combobox.
    Properties:
    • Buttonwidth As Int
      Sets the width of the drop down button. Default width is 30dip.
      The button height is the same as the component Height - 2Dip.
    • Default As Int
      Sets the item to show in the combobox.
    • Height As Int
      Gets or sets the Height.
    • Hint As String
      Sets the hint displayed if the combo text is empty.
      Default is Touch to display list.
    • Left As Int
      Gets or sets the left position of the combobox.
    • Readonly As Boolean
      Get or Set the text box as read only or editable
      Pass True for read only mode or False if the user can enter a value not in the Dropdown list
    • Text As String
      Gets or sets the value of the combo box.
    • Top As Int
      Gets or sets the Top position of the combobox.
    • Width As Int
      Gets or sets the Width.



Note that in b4a 2.7x and up the properties appear as normal properties and not as individual items. For example setHeight and getHeight will not appear in the IDE. Instead a single Height property will set or get the value. This is true for Top, Left, Width, Text, and others as well.

I'm sure there are going to be some issues or requests, so don't hesitate to report something.

--- Jem


- Version 1.00 - Initial release.

- Version 1.20 - Many fixes and additions. Added the AsView method to allow for use in a panel or activity (see the docs inside the .zip file). Includes an updated demo project. Added the ability to add bitmaps to the drop down items. If a bitmap is not needed, pass Null to the Img parameter of AddItem or AddItemAt. Bitmap items will display along with non bitmap items in the same drop down list. Changed the display of the component a bit. Added setHint to display a hint in the text box if it is empty (defaults to Touch for list). Read the included MLComboBox.txt file for more instructions and examples.

- Version 1.21 - Minor fixes and tweeks.

- Version 1.22 - The text box is now editable.

- Version 1.23 - This version adds a setText method to set the text of the text box. It also ensures that the dropdown displays on top of any other elements in your layout. The test project was also modified slightly.

- Version 1.25 - This version cures some problems mentioned in the comments and a few that I found as well. The dropdown is now free-floating so that it can resize or reposition itself as needed to keep the list on the screen.

- Version 1.26 - This version changes the way the dropdown is displayed if the parent is a panel (pass the panel as the Parent parameter in the initialize method or null if adding to an activity).

- Version 1.27 - Added the ReadOnly property. This property defaults to TRUE. Set this to FALSE if you want the user to be able to edit the text box or to add text without selecting an item from the dropdown box. This may require you to change your existing code!

- Version 1.28 - Added the ItemLongClick event. This will fire if an item in the drop down list is long pressed.

Thanks
 

Attachments

  • MLComboBox-1.28.zip
    25.3 KB · Views: 2,218
Last edited:

HotShoe

Well-Known Member
Licensed User
Longtime User
@peacemaker Try this version. It should now resize or reposition as needed.

Version 1.25 linked in first post.

--- Jem
 

lucdrb

Active Member
Licensed User
Longtime User
Hi,

I like the way you made your combobox but I don't understand how you retrieve the text entered by the user into the combobox.
I want to make a new list in adding the new text into it.

Thank in advance

Luc
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Luc,

It does not add anything automatically. You would have to use additem or additemat to add to the list.

--- Jem
 

lucdrb

Active Member
Licensed User
Longtime User
Ok thanks that what I guess but I can't see how you retreive the text entered by the user?
If it's simple, I usually don't see it :)

Luc
 

HotShoe

Well-Known Member
Licensed User
Longtime User
use the .text method.

--- Jem
 

lucdrb

Active Member
Licensed User
Longtime User
Hi Jem,

Thank you for your reply but the silly think is that I don't see in which event should I put the .text method to retrieve the text entered.

Thanks in advance
Luc
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Oh, I see what you are saying. Yeah, there is no event for the text box changing. I use a button or some other method on forms to save data and that's where I check the value. I will add an onchange type event to it. I had not really thought about using it to add new items, but that is not a bad use.

--- Jem
 

lucdrb

Active Member
Licensed User
Longtime User
Thanks for your reply and for your work on the combobox, I'll use it like you and wait for the new event.

Luc
 

lucdrb

Active Member
Licensed User
Longtime User
Hi Jem,

Strange behavior, I use a combobox who is linked into a panel which is linked into a tabhost who is into an activity.
When I open the combobox the list is not displayed under the textfield but over it (It's think that the coordinates(top,left) are reported on the activity in place of the panel).

Screenshot under (I think that a picture is more explanatory than my english :) ).

Regards
Luc

Screenshot_2014-05-07-17-44-22.jpg
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Thanks, I'll look at this later

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Lucdrb, try version 1.26 attached to the first post. the source and test project is now included in that file.

Thanks,

--- Jem
 

udg

Expert
Licensed User
Longtime User
Hi Jem,

thank you for this much needed component.
May I suggest the addition of a simple ReadOnly property? It should default to False so everything stays unchanged, but setting it to True causes the ComboBox to become a "selection" box.
My usage will be to load a few values in the dropbox list, select one as "default" (so it's displayed in the CB) and finally show the CB; this way the user is necessarily forced to select one of the proposed alternatives.
Functionally similar to a radiobox but CB-style.

Another point could be the use of a MaskEditText instead of an EditText, but this will probabily take us too far from this lib initial goal.

Umberto
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Version 1.27 has been added.

NOTE: This version WILL require you to change your code if you allow editing of the text box!

Added a ReadOnly property. This property defaults to TRUE, so if you need the user to be able to edit or type in the text box, set ReadOnly = False as soon as the ComboBox is initialized. If ReadOnly is not changed, or is set to True, the user MUST select an item from the dropdown list, and it cannot be changed unless they select another item from the list.

--- Jem
 

udg

Expert
Licensed User
Longtime User
Hi Jem,

thank you so much for the update.
The only thing I would have done differently is the ReadOnly property default value. Setting it as False as default would have had no impact on existing code. But you surely had your good reason to have done it the other way aroud, so don't bother with this.

Umberto
 

HotShoe

Well-Known Member
Licensed User
Longtime User
You are right, but in my mind at least, a combobox is for selections from the list and not as an active edittext. That said, since the source is included, all that need be done is to change :

ComboEdit.Enabled = False to ComboEdit.Enabled = True

in the Initialize method of the MLComboBox class. Or assign classname.ReadOnly = False after your layout is loaded or the cb is initialized. I hope it isn't too big a deal. If so I will change the default in an update.

--- Jem
 

udg

Expert
Licensed User
Longtime User
You are right, but in my mind at least, a combobox is for selections from the list and not as an active edittext.
--- Jem
That's the way I generally use a combobox too, so I wholeheartedly agree with you.
My suggestion for the ReadOnly initial state was due to my concern not to break existing users' code with the introduction of the new property.

Umberto
 

Myr0n

Active Member
Licensed User
Longtime User
HotShoe,

Thank you so much for this wonderful ComboBox.
 

stanks

Active Member
Licensed User
Longtime User
is there ItemLongClick Event or maybe one response from image view and another from label? what about visible or disabled property?

thanks
 
Last edited:

HotShoe

Well-Known Member
Licensed User
Longtime User
is there ItemLongClick Event or maybe one response from image view and another from label? what about visible or disabled property?

thanks

There is not, but I will add longclick event over the weekend. If the dropdown box is open then the DropShowing property will = True.

--- Jem
 
Top