[Gambas-user] Combobox returns item and item_key

Hugo Chillon hugo at ...2169...
Tue Aug 11 04:49:10 CEST 2009


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
-- 
View this message in context: http://www.nabble.com/Combobox-returns-item-and-item_key-tp24910878p24910878.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list