Put data in a TWebBrowser component of Delphi

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
LVL 1
LeTayAsked:
Who is Participating?
 
jimyXCommented:
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

0
 
jimyXCommented:
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.
0
 
LeTayAuthor Commented:
Okay if it is a file
But if it is from a string variable (data comes from database) ?
Is a temporary file necessary ?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jimyXCommented:
> 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."
0
 
Duy PhamFreelance IT ConsultantCommented:
There's a good and detailed article about customizing TWebBrowser here: http://delphidabbler.com/articles?article=14.
0
 
LeTayAuthor Commented:
Sorry ...
0
 
jimyXCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.