Searching a list view

At some point, you may want to search a list view field to find out whether it contains a specific item. There are numerous ways to search a list view field, and a complete discussion of them is beyond the scope of this manual. The following code is for the ListView_Search() function. You can use this code as a starting point for searching list view fields.

function returns long item_index;

inout anonymous field listview_field;   {The list to search.}
in string search_string; 			{The string to search for.}
in long start_item; 				 {Where to begin searching.}

local long current_item, item_count, i;
local boolean found;
local integer view_mode, column_count, subitem, j;

{Count the number of items to search.}
item_count = ListView_ItemCount(listview_field);

{Find out the view mode.}
view_mode = ListView_GetView(listview_field);

{Count the number of columns defined.}
column_count = ListView_ColumnCount(listview_field);

{Look through each item.}
found = false;
current_item = start_item;
while (current_item <= item_count) and (found = false) do
	{Look at the current item.}
	if view_mode = LV_REPORT_VIEW then
		{Have to look through the column items as well.}
		for j = 1 to column_count do
			subitem = ListView_ColumnGetSubitem(listview_field, j);
			if pos(ListView_ItemGetSubitem(listview_field, current_item, subitem), search_string, 1) <> 0 then
				{The item was found.}
				found = true;
				item_index = current_item;
				exit for;
			end if;
		end for;
	else
		{Only have to look at the item.}
		if pos(ListView_ItemGetLabel(listview_field, current_item), search_string, 1) <> 0 then
			{The item was found.}
			found = true;
			item_index = current_item;
		end if;
	end if;
	current_item = current_item + 1;
end while;

if found = false then
	{The item was not found.}
	item_index = LV_INVALID;
end if;

The ListView_Search() function begins searching the list at the item specified. If an item containing the specified text is found, that item’s index is returned. Otherwise, the constant LV_INVALID is returned.

The following example uses the ListView_Search() function to search the Explorer List list view field for the item containing the text “Bancroft”, beginning with the first item.

local long item_index;
local boolean result;

item_index = ListView_Search('Explorer List', "Bancroft", 1);

{If an item was found, select it and make it visible.}
if item_index <> LV_INVALID then
	result = ListView_SelectionSet('Explorer List', item_index);
	result = ListView_ItemMakeVisible('Explorer List', item_index);
end if;

 


Documentation Feedback