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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.