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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create and manage a listbox 2

Status
Not open for further replies.

german12

Programmer
Nov 12, 2001
563
0
16
DE
Remark:
The following question has already been asked in thread 184-1829008 - but I was recommended to ask this question in a separate thread - so that other members of this forum can also follow this discussion better:

I have created a *.dbf file (corplook1.dbf) which has a character-field "company".
The file is not yet indexed.
Now I created a form with a listbox control - and filled that listbox (List1) on the form with properties as follows:

RowSource:
Select distinct company from corplook1 order by company
RowSourceType:
3 - SQL Statement

That works - Listbox1 is filled with the extracted companies in alphabetical order.
Now I can click....but how can I use that clicked name of the company to proceed (eg. to find more about it in a memofield?)

Possible ?, or do I have to transfer the *.dbf into an array/cursor to identify the contents by item before?



Peace worldwide - it starts here...
 
Short answer I already gave, the Listbox shows the picked item number in its ListIndex property.

And also no matter what rowsourcetype you use, the value is in the Listbox.ListItems array at the index, so the company name picked is Listbox.ListItems[Listbox.ListIndex]

Chriss
 
One more point. When you use SQL Statement you better use a select statement ending with INTO CURSOR Companies. The name of coursor is your choice, of course, but without an INTO CURSOR clause the query will create a cursor named QUERY (or QUERY2, QUERY3, etc.) and you'll also see a browse of the result, which is unnecessary and avoided.

In consequence the rowsourcetype is the same as if you'd first do the query in code anywhere beforehand and use the RowSourceType 2 - Alias, which is recommended as the better choice, even if you create a cursor or table finally available with an alias, you always have more comfort in writing it out in code then in a property, so RowSourceType 3 - SQL Statement is always the less good choice.

And besides not getting a browse of the result, you also will find the selected item in the currently pointed at record in the alias name you give with the query, so in my example of the name Companies the selected company is Companies.company. Not specifing the name yourself is bad, because as I hinted above, the name then is not known to you. For example imagine you have two or three such listsboxes on your form, you don't only need to close three browse windows that pop up when every listbox executes the query, you also have query, query2, query3,... alias names and don't know which is used in which listbox.

Chriss
 
Klaus,

You might want to consider this alternative approach:

First, create a separate cursor from Corplook1. You might do this in the listbox's Init:

[tt]SELECT DISTINCT company FROM corplook1 ORDER BY company [highlight #FCE94F]INTO CURSOR csrCorp[/highlight][/tt]

Then set the RowSourceType to 6, and the RowSource to the name of the cursor (csrCorp in this example).

Now, whenever the users clicks on a row in the listbox, the record pointer in csrCorp will automatically move to the corresponding record. So all you need to do is to look at the current record in the cursor and you will see which listbox row has been selected.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike & Chriss
Thank you for your precise explanations.
I am sure that I can and will use it next time.

Klaus




Peace worldwide - it starts here...
 
Klaus,

A small correction to my above post: I said to set the RecordSource to the cursor name. In fact, you also have to specify the field name. So that would be [tt]csrCorp.Company[/tt] in this case.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ok,Mike. Thank you

Peace worldwide - it starts here...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top