Example 13 : Combining TAdvStringGrid and TWebData to build a quote download applicationThis project shows how to load data from the Web into a grid.
This demo uses the TMS components TAdvStringGrid and TWebUpdate to extract stock quotes from a website. The grid is used to list and display the stock quote values. Entering stock quote symbols can be done in the first editable column. New rows can be inserted with the INS key (grid.Navigation.AllowInsertRow = true) or can be deleted with the DEL key (grid.Navigation.AllowDeleteRow = true). The TWebData component has a collection or hyperlinks where to get the stock quote value from. In this sample we use the website http://moneycentral.msn.com where a typical stock quote page looks like: http://moneycentral.msn.com/scripts/webquote.dll?iPage=lqd&Symbol=DELL :
To extract the stock quote value from the page, TWebData can be instructed with the first word to search for in the HTML of the web page. This is set with the property ScanFirst. From this first word, it will extract the data between text set via ScanFrom, ScanTo.
This code snippet constructs the TWebData item to retrieve for a single quote:
ScanFirst := 'quotes delayed 15 min'; ScanFrom:=''; ScanTo:=''; URL:='http://moneycentral.msn.com/scripts/webquote.dll?iPage=lqd&Symbol='+qgrid.cells[1,i];
Once the collection is filled with URLs and search specifiers, it can be started to collect the information with or without a thread:
if checkbox1.checked then webdata1.ThreadCollectData else webdata1.CollectData;
Different TWebData events are used to update the user interface:
procedure WebData1Data(sender: TObject; iItem: Integer; data: String);
This event is triggered after each value is retrieved, it fills the grid cell with the value retrieved.
procedure WebData1Progress(sender: TObject; iItem: Integer);
This event is triggered after a value is retrieved and can be used to drive a progress bar.
procedure WebData1CollectDone(sender: TObject);
This event is triggered when all items have been retrieved.
As demonstrated in this sample, TAdvStringGrid & TWebData together allow to create a stock quote collecting application with minimal effort.
Delphi project & source files for downloading included in the main demos distribution for Delphi.