[Gambas-user] Combobox returns item and item_key

Doriano Blengino doriano.blengino at ...1909...
Tue Aug 11 07:45:05 CEST 2009


Hugo Chillon ha scritto:
> 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 
>   

 >>>>>>>>>>> Should'nt it be "res.Count > 0"? What happens if a table 
contains just one row? Just a thought... :-)

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

Regards,

-- 
Doriano Blengino

"Listen twice before you speak.
This is why we have two ears, but only one mouth."





More information about the User mailing list