Choosing the Type of Data for a List or Combo Box

The RowSourceType property determines what kind of source populates the list box or combo box — for example, an array or a table. When you have set the RowSourceType, specify the source of the list items by setting the RowSource property.

RowSourceType Source of the List Items
0 None. Programmatically add items to the list.
1 Value
2 Alias
3 SQL Statement
4 Query (.qpr)
5 Array
6 Fields
7 Files
8 Structure
9 Popup. Included for backward compatibility.

The following sections describe the various RowSourceType settings.

None   If you set the RowSourceType property to 0, the default, the list is not populated automatically. You can add items to the list by using the AddItem method:

frmForm1.lstMyList.RowSourceType = 0
frmForm1.lstMyList.AddItem("First Item")
frmForm1.lstMyList.AddItem("Second Item")
frmForm1.lstMyList.AddItem("Third Item")

The RemoveItem method makes it possible for you to remove items from the list. For example, the following line of code removes "Second Item" from the list:

frmForm1.lstMyList.RemoveItem(2)

Value   If you set the RowSourceType property to 1, you can specify multiple values in the RowSource property to be displayed in the list. If you set the RowSource property through the Properties window, include a comma-delimited list of items. If you set the RowSource programmatically, include the comma-delimited list in quotation marks:

Form1.lstMyList.RowSourceType = 1
Form1.lstMyList.RowSource = "one,two,three,four"

Alias   If you set the RowSourceType property to 2, you can include values from one or more fields in an open table.

If the ColumnCount property is 0 or 1, the list displays values in the first field of the table. If you set the ColumnCount property to 3, the list displays values in the first three fields of the table. To display fields in a different order than they are stored in the table, set the RowSourceType property to 3 - SQL Statement or 6 - Fields.

Note   If the RowSourceType is 2 - Alias or 6 - Fields, when a user chooses a new value in the list, the table record pointer moves to the record with the value of that item.

SQL Statement   If you set the RowSourceType property to 3 - SQL Statement, include a SELECT - SQL statement in the RowSource property. For example, the following statement selects all fields and all records from the Customer table into a cursor:

SELECT * FROM Customer INTO CURSOR mylist

If you set the RowSource programmatically, remember to enclose the SELECT statement in quotation marks.

Note   By default, Visual FoxPro SELECT statements without INTO clauses immediately display the resulting cursor in a Browse window. Because you rarely want this behavior in a RowSource SQL statement, include an INTO CURSOR clause in your SELECT statement.

Query   If you set the RowSourceType property to 4, you can populate your list box with the results of a query you designed in the Query Designer. When RowSourceType is set to 4, set RowSource to the .qpr file. For example, the following line of code sets the RowSource property of a list to a query.

THISFORM.List1.RowSource = "region.qpr"

If you do not specify a file extension, Visual FoxPro assumes an extension of .qpr.

Array   If you set the RowSourceType property to 5, the list is populated with the items in an array. You can create an array property of the form or form set for the RowSource or use an array created elsewhere in your application.

For information about creating array properties, see Creating Forms.

Tip   The RowSource setting of a list is evaluated by Visual FoxPro as needed in your application, not just in the method in which you set the RowSource. You need to keep this scope in mind. If you create a local array in a method, that array will be scoped to the method and will not be available in all cases when Visual FoxPro needs to evaluate the property setting. If you set the RowSource of a list to an array property of the form or form set, you need to reference the property relative to the list, not relative to the method in which you set the property. For example, if you have a form array property named arrayprop, the following lines of code in the Init of the form produce different results:

THIS.lst1.RowSource = "THIS.arrayprop"   && Error
THIS.lst1.RowSource = "THISFORM.arrayprop" && No error.

To populate a list with the elements in a multi-dimensional array

  1. Set the RowSourceType property to 5.

  2. Set the RowSource property to the multi-dimensional array.

  3. Set the ColumnCount property to the number of columns to display.

  4. Set the ColumnWidths property to the desired widths for each column.

  5. To make the List or Combo box display the entire array contents, call its Requery() method. Add code, such as the following, in the Init() of the ListBox or ComboBox:

    This.Requery()
    

Fields   If you set the RowSourceType property to 6, you can specify a field or comma-delimited list of fields to populate the list, such as:

contact,company,location

You can include the following types of information in the RowSource property of a list with a RowSourceType of 6 - Fields:

  • field
  • alias.field
  • alias.field, field, field, ...

If you want to have fields from multiple tables in the list, set the RowSourceType property to 3 - SQL Statement.

Unlike a RowSourceType of 2 - Alias, a RowSourceType of 6 - Fields makes it possible for you to display fields independent of their actual positions in the table.

Files   If you set the RowSourceType property to 7, the list is populated with files in the current directory. Additionally, options in the list make it possible for you to choose a different drive and directory for file names to be displayed in the list.

Set RowSource to the skeleton of the type of files you want to be displayed in the list. For example, to display Visual FoxPro tables in the list, set the RowSource property to *.dbf.

Structure   If you set the RowSourceType property to 8, the list is populated with the fields in the table that you specify when you set the RowSource property. This RowSourceType setting is useful if you want to present the user with a list of fields to search for values in or a list of fields to order a table by.

Popup   If you set the RowSourceType property to 9, you can fill the list from a previously defined popup. This option is included for backward compatibility.

See Also

Application of List Boxes and Drop-Down List Boxes | Creating Multicolumn List Boxes | Using Controls | Controls and Objects | RowSourceType | RowSource