Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Go to URLs in Memo? 3

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
0
0
US
Hello,

I'm making a page viewing program that views URLs in a list it downloads from the Internet. I have it downloading the list and then loading it into a memo but I'm struggling on how to get all the URLs off of it and view them each individually in a TWebBrowser. Anyone know how to do this? Here's my source code if it helps:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WinInet, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    List: TMemo;
    DownloadL: TButton;
    Status: TLabel;
    View: TButton;
    procedure DownloadLClick(Sender: TObject);
    procedure ViewClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetInetFile (const fileURL, FileName: String): boolean;
 const
   BufferSize = 1024;
 var
   hSession, hURL: HInternet;
   Buffer: array[1..BufferSize] of Byte;
   BufferLen: DWORD;
   f: File;
   sAppName: string;
 begin
  result := false;
  sAppName := ExtractFileName(Application.ExeName) ;
  hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
  try
   hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ;
   try
    AssignFile(f, FileName) ;
    Rewrite(f,1) ;
    repeat
     InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
     BlockWrite(f, Buffer, BufferLen)
    until BufferLen = 0;
    CloseFile(f) ;
    result := True;
   finally
    InternetCloseHandle(hURL)
   end
  finally
   InternetCloseHandle(hSession)
  end
 end;

procedure TForm1.DownloadLClick(Sender: TObject);
var
   internetFile,
   localFileName: string;
 begin
  Status.Caption:='Downloading...';
  internetFile := '[URL unfurl="true"]http://skysthelimitco.webs.com/URLList.txt';[/URL]
  localFileName := 'URLList.txt';

  if GetInetFile(internetFile, localFileName) then begin
    ShowMessage('Download successful.');
    List.Lines.LoadFromFile(localFileName);
    View.Enabled:=True;
    Status.Caption:='Idle...';
    end
  else
    ShowMessage('Error in file download.') ;
    Status.Caption:='Idle...';
end;

procedure TForm1.ViewClick(Sender: TObject);
begin

end;

end.
 
Not sure if this will help. THttpDownload is a component I have for such things (actually something I'm working on). I'm using the IWebBrowser encapsulation instead of TWebBrowser VCL, but same basic idea.

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, http_download, OleCtrls, SHDocVw_TLB;

type
  TForm1 = class(TForm)
    Downloader: THttpDownload;
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    WebBrowser1: TWebBrowser;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    currline: integer;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Downloader.URL := '[URL unfurl="true"]http://skysthelimitco.webs.com/URLList.txt';[/URL]
  Downloader.DownloadFile;
  while Downloader.ProcStatus <> TPS_Complete do
    begin
      sleep(20);
      application.processmessages;
    end;
  if Downloader.StatusCode <> 200 then
    ShowMessage('Download Failed: ' + IntToStr(Downloader.StatusCode) + #13#10 +
    Downloader.StatusText)
  else
    begin
      Memo1.Lines.Clear;
      Memo1.Lines.LoadFromFile(Downloader.SaveFilePath + '\' + 'URLList.txt');
      currline := 0;
    end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i: integer;
  empty: OleVariant;
begin
  if currline < Memo1.Lines.Count then
    begin
      while Copy(Memo1.Lines.Strings[currline], 1, 7) <> '[URL unfurl="true"]http://'[/URL] do
        inc(currline);
      label1.caption := 'URL ' + IntToStr(currline) + ': ' + Memo1.Lines.Strings[currline];
      Empty := '';
      WebBrowser1.Navigate(WideString(Memo1.Lines.Strings[currline]), Empty, Empty, Empty, Empty);
      inc(currline);
    end;
end;

end.

HTH.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Thanks for the code, I will try it later. But, I will be updating the list with new URLs every so often. I also want the URLs viewed 10 times which is why I asked if there was a way I could get the URLs so I could do the rest instead of making someone else do it. So, is there a way to this with the stuff I already have? If not, is there a way I can copy the lines the URLs are on to the clipboard and just paste them into the TWebBrowser?
 
This is probably the easy way out, but do they have to be in a memo? I would just simply throw them into a list (actually a TListView) and from there, you can make them look like links from a web page, even with the underline when you hover over them.

JD Solutions
 
I much prefer the Memo as I am a lot more familiar with it. After thinking a while, I have a workaround. Is there a way to navigate to the URL from a line in a Memo? Something like:

begin
WebBrowser1.Navigate(Memo1.Line2);
 
Sure is:

Code:
procedure TfrmMain.LogClick(Sender: TObject);
begin
  ShowMessage(IntToStr(Log.CaretPos.Y));
end;

You can also read the X value to know which character position from the left the caret is in.


JD Solutions
 
No I mean how would I get my TWebBrowser to navigate to the URL on line 2 for example? How would I get the URL from line 2 on my memo into this code: WebBrowser1.Navigate(); I apologize if I'm not making much sense.
 
ug505,

Glenn9999 already showed you how to navigate all lines in the TMemo??

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Never mind! Thanks everyone for their valuable post! I finally found out how to do it by messing around with code. Here's my code:

Code:
procedure TForm1.ViewClick(Sender: TObject);
begin
WebBrowser1.Navigate(List.Lines[1]);
end;

I didn't realize how simple it was. Using this code it took me to Google which was the URL on line 1 in my memo. Thanks for everyone's post.
 
Just to be safe, you may want to trim the value before you call it...

Code:
procedure TForm1.ViewClick(Sender: TObject);
begin
  WebBrowser1.Navigate(Trim(List.Lines[1]));
end;

This is just to make sure you're not passing any empty spaces before/after the URL in the memo. Shouldn't have to worry about it, but just an extra layer of precaution.


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top