RPC HELP Tutorial Step 7
Tutorial: Step 7 -- Associating IENs
When a user selects a terminal type entry in the list box, a typical action would be to retrieve the corresponding record and display its fields. The key to retrieving any VA FileMan record is knowing the IEN of the record. Thus, when a user selects an entry in the list box, you need to know the IEN of the corresponding VA FileMan entry. However, the list box items themselves only contain the name of each entry, not the IEN.
The subscripting of items in the list box still matches the original subscripting of items returned in brkrRPCBroker1's Results property, as performed by the following code in Button1Click event handler:
for i:=0 to (brkrRPCBroker1.Results.Count-1) do ListBox1.Items.Add(piece(brkrRPCBroker1.Results[i],'^',2));
If no further calls to brkrRPCBroker1 were made, you could simply refer back to brkrRPCBroker1's Results[x] item to obtain the matching IEN of a list boxes' Items[x] item. But, since brkrRPCBroker1 will be used again, the Results property will be cleared. So the results must be saved off in another location, if you want to be able to refer to them after other Broker calls are made.
To save off the Results to another location:
1. Create a variable named TermTypeList, of type TStringList. This is where brkrRPCBroker1.Results will be saved. Create the variable in the section of code where TForm1 is defined as a class:
type TForm1 = class(TForm) brkrRPCBroker1: TRPCBroker; ListBox1: TListBox; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private {Private declarations} public {Public declarations} // Added declaration of TermTypeList. TermTypeList: TStringList; end;
2. In Form1's OnCreate event handler, call the Create method to initialize the TermTypeList. Do this in the first line of code of the event handler:
TermTypeList:=TStringList.Create;
3. Create an event handler for Form1's OnDestroy event (select Form1, go to the Events tab of the Object Inspector, and double-click on the right-hand column for the OnDestroy event). In that event handler, add one line of code to call the Free method for TermTypeList. This frees the memory used by the list:
procedure TForm1.FormDestroy(Sender: TObject); begin TermTypeList.Free; end;
4. In Button1's OnClick event handler, add a line of code to populate TermTypeList with the list of terminal types returned in brkrRPCBroker1's Results property. This code will use the Add method of TStrings sequentially so that the subscripting of TermTypeList will match the subscripting of Results. The code for that event handler should then look like:
procedure TForm1.Button1Click(Sender: TObject); var i: integer; begin brkrRPCBroker1.RemoteProcedure:='Zxxx LIST'; try {call begin} begin brkrRPCBroker1.Call; for i:=0 to (brkrRPCBroker1.Results.Count-1) do begin {copy begin} ListBox1.Items.Add(piece(brkrRPCBroker1.Results[i],'^',2)); // Added line. TermTypeList.Add(brkrRPCBroker1.Results[i]); {copy end} end; end; {call end} except On EBrokerError do ShowMessage('A problem was encountered communicating with the server.'); end; {try end} end;
5. Then, to determine (and display) the IEN of the corresponding terminal type when a user selects an item in the list box:
- Create an OnClick event handler for ListBox1 by double-clicking on the list box.
- Add code to the new event handler that checks if an item is selected. If an item is selected in the list box, display the first piece of the corresponding item saved off in the TermTypeList array (the index subscripts of TermTypeList and of the list box match each other). This is the IEN of the corresponding VA FileMan entry.
procedure TForm1.ListBox1Click(Sender: TObject); var ien: String; begin if (ListBox1.ItemIndex <> -1) then begin {displayitem begin} ien:=piece(TermTypeList[ListBox1.ItemIndex],'^',1); ShowMessage(ien); end; {displayitem end} end;
6. Compile and run your application. When you click on an item in the list box, the IEN corresponding to that item should be displayed in a pop-up message window.
Now that you can determine the IEN of any entry the user selects in the list box, you can retrieve and display the corresponding VA FileMan record for any selected list box entry.