Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox with "key value"

Status
Not open for further replies.

imox

Programmer
May 13, 2013
37
DE
Hello,

I tried to create a ListBox with a Key, Value pair but it will be not work ;( Can somebody give me an example for it?

I want set only text and I want see only the value in the list.
 
So I add the one item to the listbox and this works well ;) But I need a key for each value ;(

Code:
SELECT * FROM table INTO CURSOR src
thisform.list1.RowSourceType = 1
SCAN 
   thisform.list1.AddItem(src.column)
ENDSCAN
 
0 = none Ok but how it could help me to get a key?
 
Another way of doing it would be to set RowSourceType to 6, and to set RowSource to "src.column" (assuming "column" is the name of the field). You don't need any other code in that case.

The advantage of this approach is that, as the user moves the highlight, VFP will automatically move the record pointer in the cursor to stay in sync. If that's not what you want, use your original code with the RowSourceType set to 0.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Code:
SELECT * FROM table INTO CURSOR src
thisform.list1.RowSourceType = 6
thisform.list1.RowSource = src.column

did not work ;( and still I need a key ;)
 
What do you mean by a key? What do you want the listbox to actually show?

In both examples, you are displaying the contents of the "column" field in your cursor? What else do you want to dispay?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I need a key for each item. e.g.

Item 1 (10)
Item 2 (11)
Item 3 (12)

When I get "Item 2" I want get "11" But in the list should only appears the Items. It is not normal to use a key value pair in foxpro?
 
Where do the numbers 10, 11 and 12 come from. Are they in your src cursor?

If so, then the easiest solution is to put both fields into the listbox, in two separate columns (Item 1, etc. in one col, and 10, etc. in the other). You can do that by setting the RowSourceType to 6, the ColumnCount to 2, and the RowSource to "src.column,key" (assuming "key" is the name of the key field within the cursor).

You say you only want one column to be visible. To achieve that, simply set the width of the other column to 0. You can do that by setting the ListBox's ColumnWidth property to, for example, "150,0".

Does that us any closer?

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ok thanks but this does not work.

Code:
SELECT * FROM table INTO CURSOR src
thisform.list1.RowSourceType = 6
thisform.list1.RowSource = src.column

and yes the key is another column from the cursor.

thanks

imox
 
But you also need to set .ColumnCount to 2, and RowSourceType to "src.colum, key" where key is the name of the other field.

Also, you need to put the field details quotes. In other words, instead of this:

Code:
thisform.list1.RowSource = src.column

you need this:

Code:
thisform.list1.RowSource = [b]"[/b]src.column[b]"[/b]

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
ahhh ok thanks ;)

but this does not work

Code:
thisform.list1.RowSource = "src.column, src.column2"
 
Code:
thisform.list1.RowSource = "src.column", "src.column2"

that does not work too ;(
 
thisform.list1.RowSource = "src.column, column2"

But you don't need to put the id into the litbox, you can read the id of a selected listbox item from the current record, with rowsourcetypes table, alias, or fields.

Bye, Olaf.
 
But it's much nice to put the ID value in the last, hidden column, because then you can set BoundColumn to that column and set a ControlSource for the list.

Code:
RowSourceType = 6
RowSource =  "src.column, column2"
ColumnCount = 2
BoundColumn = 2
BoundTo = .T. && in case your key value is numeric
ColumnWidths = "150, 0" && or whatever gives you enough width for the data value

Tamar
 
thisform.list1.RowSource = "src.column", "src.column2"

That won't work, because you need to put both field names in the same string, with a comma between. In other words:

Code:
thisform.list1.RowSource = "src.columnm, column2"

Note also that this is wrong:

Code:
thisform.list1.RowSource = "src.columnm, [b]src.[/b]column2"

If you place the alias with the second field like that, you get a "field phrase not found" message.

Mike









__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike,

thanks it works perfect ;) But how can I get the second column from the list?
 
By setting boundcolumn=2 the second field is bound to the value proeprty. Also, you can simply address src.column2 because the recordpointer in the src alias is the selected record. There is no need to read this from the control, VFP controls are easily bound to data and you work on the data, the controls work on the data and so once you've set up things correctly, you most seldom need to address the controls, you address your table or cursor.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top