We help IT Professionals succeed at work.

Put data in a TWebBrowser component of Delphi

LeTay
LeTay asked
on
How can I put data in a TWebBrowser component "directly"
I mean, i have some html data in a "string".
How can i show it in that component without using the navigate method ?
Thanks
Comment
Watch Question

Top Expert 2011

Commented:
If you have the HTML as file on disk, then you can navigate to it:
Webbrowser1.Navigate('file:///' + The_path_to_your_HTML_File);

Open in new window

To load from string variable, there is an example at About.com.

Author

Commented:
Okay if it is a file
But if it is from a string variable (data comes from database) ?
Is a temporary file necessary ?
Top Expert 2011

Commented:
> But if it is from a string variable (data comes from database) ?
Is a temporary file necessary ?

Not it is not necessary. You missed the other half of the comment.
"To load from string variable, there is an example at About.com."
Top Expert 2011
Commented:
Making the code available in case the link goes down:
procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
   sl: TStringList;
   ms: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank') ;
   while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
    Application.ProcessMessages;

   if Assigned(WebBrowser.Document) then
   begin
     sl := TStringList.Create;
     try
       ms := TMemoryStream.Create;
       try
         sl.Text := HTMLCode;
         sl.SaveToStream(ms) ;
         ms.Seek(0, 0) ;
         (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
       finally
         ms.Free;
       end;
     finally
       sl.Free;
     end;
   end;
end;

procedure TForm1.FormCreate(Sender: TObject) ; // or at ButtonClick
var
  sHTML : string;
begin
  {sHTML := '<a href="http://delphi.about.com">GOTO</a
           '<b>About Delphi Programming</b>';}

  sHTML:= <<Load you data from DB or any other source.

  WBLoadHTML(WebBrowser1, sHTML) ;
end;

Open in new window

Duy PhamFreelance IT Consultant

Commented:
There's a good and detailed article about customizing TWebBrowser here: http://delphidabbler.com/articles?article=14.

Author

Commented:
Sorry ...
Top Expert 2011

Commented:
Forget to mention you need to include ActiveX in the uses clause.
Complete code:
Uses ActiveX;

procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
   sl: TStringList;
   ms: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank') ;
   while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
    Application.ProcessMessages;

   if Assigned(WebBrowser.Document) then
   begin
     sl := TStringList.Create;
     try
       ms := TMemoryStream.Create;
       try
         sl.Text := HTMLCode;
         sl.SaveToStream(ms) ;
         ms.Seek(0, 0) ;
         (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
       finally
         ms.Free;
       end;
     finally
       sl.Free;
     end;
   end;
end;

procedure TForm1.FormCreate(Sender: TObject) ; // or at ButtonClick
var
  sHTML : string;
begin
  {sHTML := '<a href="http://delphi.about.com">GOTO</a'+
           '<b>About Delphi Programming</b>';}

  sHTML:= <<Load you data from DB or any other source.

  WBLoadHTML(WebBrowser1, sHTML) ;
end;

Open in new window