Creating an RSS Reader - Navigation Code
(Page 4 of 4 )
Next we need to write the navigation code for the TWebBrowser component. Double click on the "back" button and add the following code:
procedure TForm1.navbackClick(Sender: TObject);
begin
wb.GoBack;
end;
This code enables the browser to go back to whatever page it was on before. Now double click on the "forward" button and add the following code:
procedure TForm1.navforwardClick(Sender: TObject);
begin
wb.GoForward;
end;
This code just tells the browser to go to the next page. Now, double click on the "Home" button and add the following code:
procedure TForm1.wbHomeClick(Sender: TObject);
begin
wb.GoHome;
end;
This code takes the browser to the next page. Now, double click on the "Stop" button and add the following code:
procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
wb.Stop;
end;
This code stops the browser from executing. Finally double click on the "Refresh" button and add the following code:
procedure TForm1.wbRefreshClick(Sender: TObject);
begin
wb.Refresh;
end;
This code refreshes a page to get its updated content. That’s it, we now have a fully functioning web browser that can load, update and display web pages.
Now we need some way of adding a new RSS link to our links file, so add a new form to the application and save the unit as “addlink.” Add one TEdit and a button. Double click on the button and add the following code:
procedure TForm2.btnAddFeedClick(Sender: TObject);
var
SomeTxtFile : TextFile;
begin
if Fileexists('links.txt') then begin
AssignFile(SomeTxtFile, 'links.txt');
Append(SomeTxtFile);
WriteLn(SomeTxtFile, addfeed.text);
CloseFile(SomeTxtFile);
if MessageDlg('The link - ' + addfeed.text + ' - has now been added. Would you like to add another link?', mtInformation,[mbYes, mbNo], 0) = mrYes then begin
addfeed.Clear;
form1.tv1.Items.Clear;
form1.Refresh2;
end
else begin
form1.tv1.Items.Clear;
form1.Refresh2;
close;
end;
end
else begin
AssignFile(SomeTxtFile, 'links.txt');
Rewrite(SomeTxtFile);
WriteLn(SomeTxtFile, addfeed.text);
CloseFile(SomeTxtFile);
if MessageDlg('The link ' + addfeed.text + ' has now been added. ', mtInformation,[mbYes, mbNo], 0) = mrYes then begin
addfeed.Clear;
form1.tv1.Items.Clear;
form1.Refresh2;
end
else begin
form1.tv1.Items.Clear;
form1.Refresh2;
close;
end;
end;
end;
This piece of code just writes the RSS link location to the links file. The very first line after the begin statement checks to see if the links file exists, and if it does, it adds the link that you want to the file. Then you are given the option to add another link. The second section of the code gets executed only if the links file does not exist.
Now you must be wondering what the Refresh2 procedure does. Well, it reloads the treeview component and updates the links on it. Here’s the code:
procedure TForm1.refresh2;
var
SomeTxtFile : TextFile;
buffer : string;
begin
form1.tv1.Items.Clear;
//read links from links file ***********************************
with form1.tv1.Items.AddFirst( nil, 'RSS Links' ) do
begin
Selected := true;
end;
if fileexists('links.txt') then begin
AssignFile(SomeTxtFile, 'links.txt');
Reset(SomeTxtFile);
while not EOF(SomeTxtFile) do begin
ReadLn(SomeTxtFile, buffer);
with form1.tv1.Items.AddChildFirst( form1.tv1.Selected, buffer) do
begin
MakeVisible;
end;
end;
CloseFile(SomeTxtFile);
end;
end;
Conclusion
Here's a suggestion: because a text file is limit in how much data it can store, it would probably be better to store your links in a database, since you will want to add and remove links; also, you might just have a lot of links that you want to store. The text file does not offer the same flexibility as a database would insofar as storage and ease of use is concerned. That’s it for now. The next part will deal with downloading RSS files from the Internet, so that you can view them on your own time. Till then have fun!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |