Board index » delphi » Listbox with items from a table

Listbox with items from a table

Hello,

I'm looking for an easy way to show items from a table in a listbox or
combobox, but if you choose an item, I want to be able to use the value of
the primary key, not what is shown in the listbox.
Example : you have this table with number as primary key.

number    colour
1            red
2            green
3            orange
4            black
5            yellow

What I would like is that the colours are the items from the listbox or
combobox, but if you choose one of the colours, I would like to know what
it's number is.

Thanks in advance for any suggestion

 

Re:Listbox with items from a table


You have few ways to do it.
If you use order that is written
then use selected property of ListBox to obtain index number.
In this case RED must always be 1, Green 2 and so on.

Other way is to associate object with item. This is a bit more
complicated then above, but order is irrelevant. Look TListBox's
AddObject method in Help. You can add TColor to along with text/item.

Third method is to get the text selected and make a CASE statement
Case List1.items[i] of
'Red':MyColor=clRed
end;

In article <3b056e74$0$43048$456d7...@news.skynet.be>, j...@hotmail.com
says...

Quote
> Hello,

> I'm looking for an easy way to show items from a table in a listbox or
> combobox, but if you choose an item, I want to be able to use the value of
> the primary key, not what is shown in the listbox.
> Example : you have this table with number as primary key.

> number    colour
> 1            red
> 2            green
> 3            orange
> 4            black
> 5            yellow

> What I would like is that the colours are the items from the listbox or
> combobox, but if you choose one of the colours, I would like to know what
> it's number is.

> Thanks in advance for any suggestion

Re:Listbox with items from a table


Quote
"Johan" <j...@hotmail.com> wrote in message

news:3b056e74$0$43048$456d72a3@news.skynet.be...

Quote
> Hello,

> I'm looking for an easy way to show items from a table in a listbox or
> combobox, but if you choose an item, I want to be able to use the value of
> the primary key, not what is shown in the listbox.

You might want to consider using a tDBLookupComboBox. They are expensive in
terms of Windows resources but if you don't use a lot of them you won't have
a problem.

Re:Listbox with items from a table


I use dblookupcomboboxes if I want to use the key in another table, but here
I don't want to do that.  Is it possible to use dblookupcomboboxes in that
way ?
If it is possible I can put the tablename in ListSource, the field Colour in
ListField and Number in Keyfield.  But how can I get access to the value of
Keyfield when a user chose a colour in the dblookupcombobox ?

Thanks in advance,
Johan

Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message

news:_2nN6.28616$TW.136253@tor-nn1.netcom.ca...
Quote

> "Johan" <j...@hotmail.com> wrote in message
> news:3b056e74$0$43048$456d72a3@news.skynet.be...
> > Hello,

> > I'm looking for an easy way to show items from a table in a listbox or
> > combobox, but if you choose an item, I want to be able to use the value
of
> > the primary key, not what is shown in the listbox.

> You might want to consider using a tDBLookupComboBox. They are expensive
in
> terms of Windows resources but if you don't use a lot of them you won't
have
> a problem.

Re:Listbox with items from a table


Quote
"Johan" <j...@hotmail.com> wrote in message

news:3b064874$0$3123$456d72a3@news.skynet.be...

Quote
> I use dblookupcomboboxes if I want to use the key in another table, but
here
> I don't want to do that.  Is it possible to use dblookupcomboboxes in that
> way ?
> If it is possible I can put the tablename in ListSource, the field Colour
in
> ListField and Number in Keyfield.  But how can I get access to the value
of
> Keyfield when a user chose a colour in the dblookupcombobox ?

It is possible, just don't specify a DataSource. To get the picked value use
the KeyValue property - its a variant.

Re:Listbox with items from a table


Thanks, it works.

Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message

news:99sN6.28640$TW.136660@tor-nn1.netcom.ca...
Quote

> "Johan" <j...@hotmail.com> wrote in message
> news:3b064874$0$3123$456d72a3@news.skynet.be...
> > I use dblookupcomboboxes if I want to use the key in another table, but
> here
> > I don't want to do that.  Is it possible to use dblookupcomboboxes in
that
> > way ?
> > If it is possible I can put the tablename in ListSource, the field
Colour
> in
> > ListField and Number in Keyfield.  But how can I get access to the value
> of
> > Keyfield when a user chose a colour in the dblookupcombobox ?

> It is possible, just don't specify a DataSource. To get the picked value
use
> the KeyValue property - its a variant.

Other Threads