Link to home
Start Free TrialLog in
Avatar of LeTay
LeTay

asked on

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
Avatar of jimyX
jimyX

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.
Avatar of LeTay

ASKER

Okay if it is a file
But if it is from a string variable (data comes from database) ?
Is a temporary file necessary ?
> 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."
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There's a good and detailed article about customizing TWebBrowser here: http://delphidabbler.com/articles?article=14.
Avatar of LeTay

ASKER

Sorry ...
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