SimpleList documentationThis article forms the official documentation for our SimpleList control. It will tell you everything you need to know about how to use the control, with full details of all its exposed properties and methods. The article is a longer version of the article which appears on the SimpleList web page, at www.ml-consult.co.uk/foxst-28.htm. SimpleList is currently at version 1.1. Please see below for a summary of the new features in this version. For information about updates, please keep an eye on the above web page. The SimpleList control and its accompanying documentation are the copyright of Mike Lewis Consultants Ltd. SimpleList is completely free, and you are welcome to give copies to friends and colleagues. We ask only that you do not remove our copyright notices or disclaimers. We have tested SimpleList in our own applications, but we make no guarantees that it will work in yours, so be sure to test it thoroughly before relying on the results. About SimpleListSimpleList is a Visual FoxPro class that acts as a wrapper for the Microsoft ListView ActiveX control. Its aim is to provide a simple cursor-based method of populating a ListView and of obtaining useful information from it. In fact, once you have a cursor in place, you only need three lines of code to make your list appear. SimpleList has many features which will help you exploit the ListView to the full. However, most of these are optional. You can safely ignore any features that you are not interested in. RequirementsSimpleList requires Visual FoxPro 7.0 or later. What exactly is a ListView?To see a ListView in action, you need look no further than the right-hand pane in Windows Explorer. It is the ListView that provides the list of files and folders, with its familiar large icon, small icon, list and detail views. The ListView ActiveX control can provide that same kind of functionality in your own applications. With typical databased applications, you'll probably only be interested in displaying your list in detail view (also knows as report view), as this provides the most intuitive way of viewing tabular data. However, the ListView does support the other three views as well. Figure 1 shows an example of detail view, while Figure 2 shows large icon view. Figure 1: A ListView in detail view Figure 2: The same list in large icon view As well as providing four different views, the ListView control offers several other benefits:
LimitationsAlthough SimpleList supports most of the ListView's features, it does have some limitations:
Getting startedIf you have not already done so, go ahead and download your copy of SimpleList now - see How to download SimpleList, below. The rest of this article will tell you how to put SimpleList to work. Please don't be put off by the length of the article or the number of properties and methods described. SimpleList really is simple, and most of the information that follows is completely optional. You can get started with SimpleList with just a few lines of code. The least you need to knowTo put SimpleList to work, follow these steps: 1. Create a cursor to hold the data that you want to display in the list (although we will use the term cursor throughout this article, it could just as well be a table or a view). As a minimum, the cursor must contain a single character field; it is the data from this field that will be used to populate the list. 2. Drop the SimpleList on a form. 3. To populate the list, write code similar to the following (this would typically go in the control's Init event):
That's all there is to it. As an example, if the cursor contains the data shown below, your form will look something like Figure 3.
Figure 3: A one-column list in detail view. Let's just recap the above properties and methods. cAlias cData You can optionally list more fields in the cData property. If you do, their contents will show up as separate columns in detail view, but they will be ignored in the other three views. These additional fields can be of any data type. PopulateList RefreshList As another example, consider this cursor: Company City Country Amount ------- ---- ------- ------ Alfreds Futterkiste Berlin Germany 6300 Ana Trujillo Emparedado.. México D.F.Mexico 3500 Antonio Moreno Taquería México D.F.Mexico 8500 Around the Horn London UK 17100 Blauer See Delikatessen Mannheim Germany 5000 Blondel père et fils Strasbourg France 22205 Bon app' Marseille France 27100 Bottom-Dollar Markets Tsawassen Canada 27800 B's Beverages London UK 11000 Centro comercial Moctez.. México D.F.Mexico 1000 Chop-suey Chinese Bern Switzer.. 17696 Ernst Handel Graz Austria 122900 The following code will produce the list shown in Figure 1 (but without the icons):
Choosing the viewBy default, PopulateList displays the list in detail view. The following settings are available: nDefaultView ChangeView nCurrentView If you want to give the user a way of switching the view, you could put the following code in a button on the form. Each time the button is clicked, the next view in sequence will be displayed.
More about columnsBy default, the columns headers in detail view will show the field names from the cursor. The columns will be left-aligned. If there is more than one column, they will start out with equal widths. You can vary that behaviour as follows: cColumnHeaders cColumnWidths cColumnAlignments The above three properties are only applied when the list is initially populated, not when it is refreshed. lHideColumnHeaders cAdjustedColumnWidths IconsIf you plan to display your list in large icon or small icon view, you will probably want to assign icons to the list items - after all, that is the whole point of those views. If you do assign icons, they will also show up in list and detail views. But it's your choice. If you are not interested in icons, you can ignore this entire section. cIconField cDefaultIcon The image file can be a BMP, GIF, JPG or ICO file. It is your responsibility to ensure that the file can be found at run time (SimpleList does not check for this). Unlike with the underlying ListView control, SimpleList does not let you assign different sizes of icons for the different views. Whatever image you specify, it will be scaled to either 48 x 48 pixels (for large icon view) or 16 x 16 pixels (for the other three views). For the best results, use an ICO file that contains two images, one at each of the required sizes. TooltipsUse the following properties if you want a tooltip to appear against each item. The text of the tooltip can be different for each item. This is useful if you want to let the user see subsidiary information that would not otherwise fit in the list. These tooltips only appear in detail view, and only when the mouse is over the left-most column. A tooltip is limited to approximately 75 characters. cTooltipField lShowTooltips SortingThese properties let you control the way that the list is sorted. They apply in detail view only. lSorted nInitialSortColumn nInitialSortDirection cSortColumns cSortColumns should contain a comma-delimited list of integers, one per column, in the sequence that the columns are listed in cData. If a given integer is 0, then clicking on its column header will sort the list on that column. If it contains a positive value, then the integer represents the number of the column to be sorted. Consider this example:
This will produce a list with four columns, containing respectively the order ID, order date (in the current SET DATE format), order amount, and order date (in YYYYMMDD format). The last of these columns will be invisible. The list will initially be in order ID sequence. When the user clicks on the second column header, the list will be properly sorted to date sequence. CheckboxesIf you have ever wanted to give your users a scrolling list of checkboxes, you will find this feature indispensable. Essentially, it lets you display a checkbox against each item in the list. This provides an intuitive way of making multiple selections, for example, to let the user choose the customers who are to appear in a report, or the reports that you want to send to a batch-printing process (see Figure 4 for an example). Figure 4: Displaying checkboxes in a list. The following properties are relevant to checkboxes: lCheckBoxes cCheckField lUpdateCheckbox To determine if the checkbox for a given item is checked, call the GetChecked method (see Retrieving information, below). Editing an item's textBy default, your list will be read-only. However, you can optionally give the user the ability to edit an item's text. This applies in all four views, but in detail view only the left-most column can be edited. To edit an item's text, the user simply clicks once on the text. To enable editing, use these properties: lReadOnly lUpdate If lReadOnly and lUpdate are both .F., the user will still be able to edit the item, but the edits will not be processed in any way. In that case, you can write your own code to process the edits in the native AfterLabelEdit event of the ListView control. Drag and dropSimpleList supports drag-and-drop. However, this is not enabled by default. If you choose to enable it, the user will be able to drag an item out of the list and drop it in any control or application that knows how to receive it. In each case, the target control or application will receive a text string. SimpleList cannot itself act as a drop target. In other words, you can drag items from the list, but you cannot drag anything to it. To use drag-and-drop, set the following properties: lDragEnabled nDragText
If you want a native Visual FoxPro control to receive the dragged text, set that control's oleDropMode property to 1. Cosmetic settingsThese settings can be used to modify the appearance of your list: lFullRowSelect lGridLines lHotTracking The above setting take effect straight away (there is no need to call PopulateList, RefreshList or RedrawList, as in the previous version). Retrieving informationSo far, we have seen how to populate the list and customise its appearance. Let's now see how to get information about what's in the list and which item the user has selected. cSelectedText nSelectedIndex nSelectedRecno nCount Note that the above four properties are read-only. SelectItem GetText GetRecno GetChecked Those last three methods allow you to loop through the items in the list, performing some action on each of them. For example, given a list like the one in Figure 4 above, the following code would print the selected reports:
Synchronising the cursorBy default, there is no connection between the highlight within the list and record pointer in the underlying cursor. However, you can change that behaviour by setting this property: lLink Event TrappingSimpleList provides three events for trapping the user's mouse and keyboard actions: Click DblClick Unfortunately, there is no way of distinguishing between left- and right-clicks or between left- and right-double-clicks. InteractiveChange Things that can go wrongSimpleList does some basic error-checking on the values that you place in its properties. If all is well, PopulateList and RefreshList will each return zero. If either of these methods detects an error, their behaviour will depend on the setting of the following property: lError The error codes returned by PopulateList and RefreshList are as follows:
Lifting the lidIf you are interested in learning about SimpleList's internal workings, you can open it in Visual FoxPro's class designer and study its code. You will notice that the SimpleList control is in fact a VFP container, which in turn contains the ListView ActiveX control. All the properties and methods that we have described in this article belong to the container rather than the ListView. The reason for this approach is simple. If we ever decide to use a different type of control to display our lists, we could remove the ListView from the container and put the new control in its place. It's true that we might have to rewrite most or all of the SimpleList code, but the point is that we could do so without changing the external workings of the control - its public interface. This is a good example of separating the interface of a component from its implementation - always a desirable goal. It means that however much we might change SimpleList's internals, it won't affect any of the programs which use it. Distributing your applicationIn order to install SimpleList with your application, be sure to distribute the Microsoft ListView ActiveX control, which is MSCOMCTL.OCX. This should be installed in the user's System folder and registered as an ActiveX control. If you are not sure how to do that, check the Help for InstallShield Express. New features in Version 1.1Here is a summary of the new features in SimpleList Version 1.1. These are all documented in the main part of this article.
How to download SimpleListIf you do not already have a copy of SimpleList, you can download it now, from www.ml-consult.co.uk/foxst-28.htm. This page will always contain the latest copy of SimpleList. The download file is named SIMPLELIST.ZIP. This zip file contains a class library, which in turn contains the SimpleList control. It also contains this document in both HTML and text format. We hope you find SimpleList as useful in your applications as we do in ours. As always, we greatly welcome your feedback (for contact details, please see www.ml-consult.co.uk/contact.htm). Mike Lewis Consultants Ltd. September 2002. Revised March 2003. |
FOXSTUFF Advice, tips, techniques and downloads for Visual FoxPro developers Brought to you by Mike Lewis Consultants Ltd FoxStuff is maintained by Mike Lewis Consultants Ltd as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).
© Copyright Mike Lewis Consultants Ltd |