PerfectScript Journal

September 1998: How to get multiple list items from a list

If you've ever used a multiple selection list box in a WordPerfect macro
dialog, you have probably found that it is not the most intuitive
process when it comes to getting the selected items from that list.

You create a multiple selection list box the same way as you would any
other list box, but you specify the MultipleSelection! style to enable
selection of more than one item. Here's an example of a dialog with
a multiple-selection list box.

In WPWin 6 and 7, when you dismiss the dialog, the list box variable only
contains one selected item rather than a string of all selected items.
This forces you to use a callback for the dialog to get the list of
all the selected list items. Then, once you have the list, you must
write a convoluted routine to parse each item from the list. Whew.
Your code would look something like this:


Application(A1; "WordPerfect"; default; "US")
DialogDefine ("ListBox"; 50; 50; 200; 150; Percent!; "List Box")
DialogAddListBox ("ListBox"; "ListBx1"; 10; 10; 130; 120;
  WPChars!+Sorted!+MultipleSelection!; ListBx1)
	DialogAddListItem ("ListBox"; "ListBx1"; "Cats")
	DialogAddListItem ("ListBox"; "ListBx1"; "Dogs")
	DialogAddListItem ("ListBox"; "ListBx1"; "Horses")
	DialogAddListItem ("ListBox"; "ListBx1"; "Cattle")
DialogAddPushButton ("ListBox"; "OKBttn"; 150; 10; 40; 14;
   OKBttn!; "OK")
DialogAddPushButton ("ListBox"; "CancelBttn"; 150; 30; 40; 14;
   CancelBttn!; "Cancel")
DialogShow("ListBox"; "WordPerfect"; CB)
CallbackWait
DialogDestroy("ListBox") 
Type ("The entire list: "+vList)
HardReturn Hardreturn
Type("The individual items: ")
HardReturn

vParseList:=vList
vLen:=StrLen(vParseList)
Repeat
vLen:=StrLen(vParseList)
vPos:=StrPos(vParseList; ";")		// get position of separator
Type(Substr(vParseList; 1; vPos-1))	// Get substring up to separator
vParseList:=Substr(vParseList; vPos+1; vLen) 
    // recreate string from separator to HardReturn
Until (vPos <=0)
Quit


Label(CB)
If(CB[5]=274 OR CB[3]="CancelBttn")
	Quit
Endif

If(CB[3]="OKBttn")
	vList:=RegionGetSelectedText("ListBox.ListBx1")
	CallbackResume
Endif
Return
           

WPWin 8 makes some great improvements when it comes to processing
selected items in a multiple selection list box. The list box variable now
returns all the selected items in one string, and a new command
(STRParseList) has been added to help expedite the parsing of the
items from the list.

A WPWin 8 macro that does the same thing will look like this:



Application(A1; "WordPerfect"; default; "EN")
DialogDefine ("ListBox"; 50; 50; 200; 150; Percent!; "List Box")
DialogAddListBox ("ListBox"; "ListBx1"; 10; 10; 130; 120; 
  WPChars!+Sorted!+MultipleSelection!; vList)
DialogAddListItem ("ListBox"; "ListBx1"; {"Cats"; 
  "Dogs"; "Horses"; "Cattle"})
DialogAddPushButton ("ListBox"; "OKBttn"; 150; 10; 40; 14;
   OKBttn!; "OK")
DialogAddPushButton ("ListBox"; "CancelBttn"; 150; 30; 40; 14;
   CancelBttn!; "Cancel")
DialogShow("ListBox"; "WordPerfect")

Type ("The entire list: "+vList)
HardReturn Hardreturn
Type("The individual items: ")
HardReturn
vArray[]:=StrParseList(vList)
Fornext(x; 1; vArray[0])
	Type(vArray[x])
	HardReturn
Endfor
           

NOTE: You'll also note an enhancement in the WPWin 8 code above that
allows you to addmultiple items to a list with one DialogAddListItem command.

In WPWin 8, each item that was selected in the list is assigned to an array
variable element. In WPWin 7 each item becomes a separate string. (You can write your own code to assign the items to an array yourself, if you choose.)  In either case, once the items are isolated from each other, you can type  them into the document, use them as expressions in other commands, or just  about anything else you need to do with them.

Next month: Processing a group of files with a macro

For more information on this topic, get a copy of my book.
My WPWin 8 macro book should be out sometime in mid-October.

For information on ordering my book see my web page.

 

Copyright Notice: The information included in the PerfectScript Journal is protected by US Copyright. The author grants you the right to use the routines in your own macros as needed. You may not sell, distribute, or publish them in any form.
If you choose to use the information here, you do so entirely at your own risk. No representations are made regarding the fitness of this information for your   particular purpose, or for your ability or inability to use the information. You   are advised to make backups of all relevant files before implementing any suggestion  or technique.
© Copyright 1998 by J. Jeppson.