[Gambas-user] Combobox returns item and item_key

richard terry rterry at ...1946...
Tue Aug 11 05:13:09 CEST 2009


On Tue, 11 Aug 2009 12:49:10 pm Hugo Chillon wrote:
> For people who looks for a combobox to show items from a database table
> column but need combobox returns item_key from another column, this is a
> class for them:
>
> ' Gambas class file
> '**************************************************************************
>* '*  ComboBoxPlus class
> '*
> '* Autor: Hugo Chillon Estefanell
> '* Date: 09/08/2009
> '*
> '* ComboBoxPlus is a class designed to allow a ComboBox works with 2
> '* columns from a database table. First column for the key_item, second
> '* column for the item. Combobox shows item and ComboBoxPlus.ItemId()
> '* returns key_item
>
> '* This class call a class (MysqlConnection) to access a database and
> obtain '* a Result object with data. You should use your own method to
> obtain the '* Result object
> '**************************************************************************
>
> PRIVATE cboBase AS NEW Object[]
> PRIVATE cbo AS ComboBox
>
>
> PUBLIC SUB _New(combo AS ComboBox)
>     cbo = combo
> END
>
>
> PUBLIC SUB Populate(strTable AS String, strIndex AS String, strValue AS
> String, strWhere AS String)
>
>     DIM conn AS MysqlConnection
>     DIM strSQL AS String
>     DIM res AS Result
>
>     strSQL = "SELECT " & strIndex & ", " & strValue & " FROM " & strTable &
> " " & strWhere
>
>     conn = NEW MysqlConnection
>     res = conn.RunDSQ(strSQL)
>     IF res.Count > 1 THEN
>         res.MoveFirst
>         cbo.Add("")
>         cboBase.Add(["", ""])
>         DO WHILE res.Available
>             cbo.Add(res[1])
>             cboBase.Add([res[0], res[1]])
>             res.MoveNext
>         LOOP
>     ENDIF
> END
>
>
> PUBLIC FUNCTION ItemId() AS Integer
>
>     DIM intValue AS Integer
>
>     intValue = cboBase[cbo.Index][0]
>     RETURN intValue
>
> END

Cool, I'll  have a look at this sometime as this lack of being able to save 
the index  has been one of my main gripes about combo-boxes.


Richard




More information about the User mailing list