JavaScript Editor js editor     Web development 



Main Page

When you want to populate a ListBox or ComboBox control with items or values, set the control's RowSource property to specify the source of items or values to fill the control and set the RowSourceType property to identify the source type, such as an array or table.

Tip:
It is recommended that you set the RowSourceType property before setting RowSource to determine the source that can be used. To ensure that the control displays the most recent items or values, use the Requery method in the Init method, for example, This.Requery().

Note:
When you set the RowSource property in code, you must enclose the value with quotation marks ("").

When the ColumnCount property is set to 0 or 1, the control displays the first item or value only in a single column. Otherwise, the control displays each item or value in a column up to the number of columns specified by ColumnCount. You might also need to set the ColumnWidths property to expand the width of the control so the items or values within display properly.

This topic contains examples that illustrate ways you can set RowSourceType and RowSource depending on the source of values you choose. The following is a list displaying the source types available:

For details about source types, the sources you can specify, and how ColumnCount affects population of the control for each source type, see RowSourceType Property and RowSource Property.

For more information, Visual FoxPro provides the following samples that use the RowSourceType and RowSource properties:

No Source Type

You can populate a list box or combo box without specifying a source or source type.

To populate the control without specifying a source

  1. Set RowSourceType to 0 (None).

    None is the default setting and does not populate the control automatically.

  2. Use the AddItem method, AddListItem method, List property, or ListItem property to add values or items to the control.

For example, the following lines of code set RowSourceType to 0 to indicate no source type and uses the AddItem method to add three items to the list box lstMyListBox on the form frmForm1:

В Copy Code
frmForm1.lstMyListBox.RowSourceType = 0
frmForm1.lstMyListBox.AddItem("First Item")
frmForm1.lstMyListBox.AddItem("Second Item")
frmForm1.lstMyListBox.AddItem("Third Item")

The list box displays the items, "First Item", "Second Item", and "Third Item". For more information, see AddItem Method (Visual FoxPro), AddListItem Method, List Property, and ListItem Property.

To remove items from the list, use the RemoveItem method. For example, the following line of code removes the second item, "Second Item", from the list box:

В Copy Code
frmForm1.lstMyListBox.RemoveItem(2)

For more information, see RemoveItem Method.

Value Source Type

You can populate a list box or combo box with a list of individual values.

To populate the control with a list of individual values

  1. Set RowSourceType to 1 (Value).

  2. Set RowSource by specifying the values in a comma-delimited list, using no spaces between the values and the commas.

For example, the following lines of code set RowSourceType to 1 to indicate that the source is a comma-delimited list of values and specifies a comma-delimited list in quotation marks ("") for RowSource:

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

Alias Source Type

You can populate a list box or combo box with values from one or more fields in an open table by specifying the table's alias.

To populate the control with values from one or more fields using a table alias

  1. Set RowSourceType to 2 (Table alias).

  2. Set RowSource by specifying a table alias.

For example, the following lines of code set RowSourcetype to 2 to indicate that the source is a table identified by the table alias, Customers, and specifies "Customers" as the table alias for RowSource:

Tip:
Make sure the table is opened first before the code executes.

В Copy Code
OPEN DATABASE (HOME(2)+"Northwind\Northwind")
Form1.lstMyList.RowSourceType = 2
Form1.lstMyList.RowSource = "Customers"

The ColumnCount property determines the number of fields from which values are used to populate the control. When ColumnCount set to 0 or 1, the control displays values from the first field in the table. When ColumnCount set to a value greater than 1, the control displays values from fields in the table up to the number specified by ColumnCount. For example, if ColumnCount is set to 3, the control displays values from the first three fields in the table.

Note:
When RowSourceType is 2 (Table alias) or 6 (Fields), the table record pointer moves to the record containing the value that the user chooses in the list.

SQL Statement Source Type

You can populate a list box or combo box with values a cursor or table created by a SQL statement, such as SQL SELECT.

To populate the control with values from a SQL statement

  1. Set RowSourceType to 3 (SQL statement).

  2. Set RowSource by specifying a SQL SELECT statement.

Tip:
By default in Visual FoxPro, SQL SELECT statements without INTO clauses display the resulting cursor immediately in a browse window. For more information, see Browse Window. It is suggested that you include an INTO CURSOR clause in your SQL SELECT statement to avoid displaying the browse window.

For example, the following lines of code set RowSourceType to 3 to indicate that the source is a cursor or table created from a SQL statement and specifies a SQL SELECT statement for RowSource that selects all fields and all records from the Customers table, located in the sample Northwind database in the directory, ..\Samples\Northwind, into a cursor named myCursor:

В Copy Code
OPEN DATABASE (HOME(2)+"Northwind\Northwind")
Form1.lstMyList.RowSourceType = 3
Form1.lstMyList.RowSource = "SELECT * FROM Customers INTO CURSOR myCursor"

For more information about SQL SELECT statements, see SELECT - SQL Command.

Query Source Type

You can populate a list box or combo box with results from a query that you created using the Query Designer and saved in a query (.qpr) file. For more information about queries, see Query and View Designers and How to: Create Queries (Visual FoxPro).

To populate the control with values from a query

  1. Set RowSourceType to 4 (Query file).

  2. Set RowSource by specifying the name of a .qpr file, including a path if needed.

    Note:
    Make sure to include the .qpr file name extension. However, if you do not specify a file extension, Visual FoxPro assumes the file extension is .qpr.

For example, the following lines of code set RowSourceType to 4 to indicate that the source is a .qpr file and specifies the name of a .qpr file for RowSource:

В Copy Code
Form1.lstMyList.RowSourceType = 4
Form1.lstMyList.RowSource = "MyQuery.qpr"

Array Source Type

You can populate a list box or combo box with items or values stored in an array. You use an array created elsewhere in your application or create an array property of the form or form set to specify for RowSource. For more information about arrays, see Arrays. For more information about creating array properties, see Creating Forms.

To populate the control from an array

  1. Set RowSourceType to 5 (Array).

  2. Set RowSource by specifying the name of the array.

For example, the following lines of code create an array with 10 elements, store values in the array, set RowSourceType to 5 to indicate the source is an array, and specifies the name of the array for RowSource:

В Copy Code
DIMENSION gaMyArray(10)
FOR gnCount = 1 to 10  
   STORE gnCount TO gaMyArray(gnCount)
NEXT 
Form1.lstMyList.RowSourceType = 5
Form1.lstMyList.RowSource = "gaMyArray"
Note:
The RowSource setting is evaluated by Visual FoxPro as needed in your application and is not confined only to the method in which you set RowSource. You need to keep this scope in mind.

For example, if you create a local array in a method, that array is 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 to an array property of a form or form set, you need to reference the property relative to the control, not relative to the method in which you set the property. As an example, suppose you have a form array property named arrayProp and the following lines of code in the form's Init event. The first line of code produces an error while the second line does not:

В Copy Code
THIS.lst1.RowSource = "THIS.arrayprop"   
THIS.lst1.RowSource = "THISFORM.arrayprop" 

Fields Source Type

You can populate a list box or combo box with values from fields from a single table. Unlike when RowSourceType is set to 2 (Table alias), setting RowSourceType to 6 (Fields) makes it possible to display fields independent of their actual positions in the table.

Tip:
If you want to populate the control with fields from multiple tables, set RowSourceType to 3 and specify a SQL SELECT statement for RowSource.

To populate the control from fields from a single table

  1. Set RowSourceType to 6 (Fields).

  2. Set RowSource by specifying a field or a comma-delimited list of fields.

    If the table is open, you can specify RowSource in the following ways:

    В Copy Code
    Form1.lstMyList.RowSource = field1
    Form1.lstMyList.RowSource = field2, field3, ...

    If the table is not open, precede the field or list of fields with a table alias and a period (.), for example:

    В Copy Code
    Form1.lstMyList.RowSource = TableAlias.field1
    Form1.lstMyList.RowSource = TableAlias.field1, field3, field2, ...

For example, the following lines of code open a table, set RowSourceType to 6 to indicate that the source type is fields, and specify fields from the Products table in the Northwind database, located in the directory ..\Samples\Northwind, for RowSource:

В Copy Code
OPEN DATABASE (HOME(2)+"Northwind\Northwind")
USE Products
Form1.lstMyList.RowSourceType = 6
Form1.lstMyList.RowSource = "productname, productid"

The following lines of code specify fields from the Customers table, which is not open, for RowSource:

В Copy Code
OPEN DATABASE (HOME(2)+"Northwind\Northwind")
Form1.lstMyList.RowSourceType = 6
Form1.lstMyList.RowSource = "Customers.companyname, customerid"

Files Source Type

You can populate a list box or combo box with the names of files from the current directory. The list also contains options for you to choose a different drive and directory to display file names from.

To populate the control with file names from a directory

  1. Set RowSourceType to 7 (Files).

  2. Set RowSource by specifying a file skeleton or mask, such as *.* or *.fileExt.

For example, the following lines of code set RowSourceType to 7 to indicate that the source type is files and specifies a file skeleton for RowSource:

В Copy Code
Form1.lstMyList.RowSourceType = 7
Form1.lstMyList.RowSource = "*.*"

Field Structure Source Type

You can populate a list box or combo box with the names of fields in a table. Displaying a list of field names is useful when the user wants to search a particular field for values or to organize those fields in the table.

To populate the control with field names from a table

  1. Set RowSourceType to 8 (Field structure).

  2. Set RowSource by specifying a table name or table alias.

For example, the following lines of code set RowSourceType to 8 to indicate that the source type is a field structure of a table and specifies a table name for RowSource:

В Copy Code
Form1.lstMyList.RowSourceType = 8
Form1.lstMyList.RowSource = "Customers"

Pop-Up Source Type

Included for backward compatibility, RowSourceType set to 9 (Pop-up) made it possible to populate a list box or combo box from a previously defined pop-up or menu.

To populate the control with a pop-up menu

  • Set RowSourceType to 9 (Pop-up).

Collection Source Type

You can populate a list box or combo box with members and property values of objects in a collection, or Collection object.

To populate the control with collection members

  1. Set RowSourceType to 10 (Collection).

  2. Set RowSource by specifying the name of a collection.

For example, the following lines of code create a collection and two forms, adds the forms and three strings to the collection, sets RowSourceType to 10 to indicate that source type is a Collection object, and specifies the collection name and properties of objects in the collection for RowSource:

В Copy Code
colMyCollection=CREATEOBJECT("Collection")
frm1=CREATEOBJECT("Form")
frm2=CREATEOBJECT("Form")
colMyCollection.Add(frm1)
colMyCollection.Add(frm2)
colMyCollection.Add("Item 1")
colMyCollection.Add("Item 2")
colMyCollection.Add("Item 3")
Form1.lstMyList.RowSourceType = 10
Form1.lstMyList.RowSource = "colMyCollection, Caption, Name"

Different Source Types

You can populate a list box or combo box with items and values from different sources.

To populate a control from different sources

See Also



JavaScript Editor js editor     Web development