How to Use Additem and AddListItem and How They Are DifferentLast reviewed: October 25, 1995Article ID: Q138498 |
The information in this article applies to:
SUMMARYBoth the AddItem and AddListItem methods add items to combo boxes or list boxes. This article highlights their functionality and the differences between the two methods. In particular, this article demonstrates why the AddListitem method might be a better way to populate a multiple-column combo box.
MORE INFORMATIONThe Additem and AddlistItem methods add items to a list and accept two optional parameters. The differences between the two methods are mirrored in the differences between their first parameter; AddItem accepts nListIndex as its first parameter, and AddListItem accepts nListItemId as its first parameter. There are two ways to identify an item in a list:
The Additem method inserts an item based on its relative position in the list. The syntax is:
Control.AddItem(cItem [, nIndex] [, nColumn])The AddListItem method allows you to create a unique id for the item, and uses nItemId. If the item already exists, the prompt is replaced. The syntax is:
Control.AddListItem(cItem [, nItemID] [, nColumn])The following example illustrates how to use the Additem and AddListItem methods. To run the example, copy and paste the code in a program file.
Sample CodeOmyform=CREATE('myform') Omyform.Show() READ EVENTS DEFINE Class myform AS Form ADD OBJECT list1 AS mylist ADD OBJECT list2 AS mylist2 ADD OBJECT cmd1 AS cmdquitENDDEFINE DEFINE Class cmdquit AS CommandButton Top=105 Left = 55 Caption="Quit" PROCEDURE Click CLEAR EVENTS RELEASE Thisform ENDPROCENDDEFINE DEFINE Class mylist AS ListBox && Defines a list filled with Additem Top = 3 Left=5 Width=90 Height=90 RowSourceType=0 PROCEDURE Init This.AddItem('Apples',1) This.AddItem('Oranges',2) This.AddItem('Lemons',2) && Element inserted before 'Oranges' ENDPROCENDDEFINE DEFINE Class mylist2 AS ListBox &&Defines a list filled with AddListitem Top = 3 Left=100 Width=90 Height=90 RowSourceType=0 PROCEDURE Init This.AddListItem('Apples',1) This.AddListItem('Oranges',2) This.AddListItem('Lemons',2) && Prompt overwrites 'Oranges' ENDPROCENDDEFINE
Multiple-Column List BoxesThe AddItem method might not yield expected results when it is used to populate a multiple-column list box. The following step-by-step example illustrates how the elements of a multiple-column list box are displayed when the AddItem method is used.
--------------------- | | Two | | | Four | | Three | | | One | | ---------------------This occurs because the AddItem inserts each item at the relative position specified by the nIndex integer. Use the ListItemId to populate a multiple-column combo box and add items on the same row. By using the example above and replacing AddItem with AddListItem, the list looks like this:
------------------------ | One | Two | | Three | Four | ------------------------For more information about the AddItem and AddListItem methods, search for "AddItem" or "AddListItem" in the Visual FoxPro Help file.
|
Additional reference words: 3.00 VFoxWin
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |