1. Home
  2. Computing & Technology
  3. Delphi Programming

How to enable editing of a document in TWebBrowser

By Zarko Gajic, About.com

If you need to enable a user to change the contents of a document (web page) displayed by the TWebBrowser component, use the following trick:

~~~~~~~~~~~~~~~~~~~~~~~~~
uses MSHTML;


//Drop a TWebBrowser (name: WebBrowser1) on a form...
procedure TForm1.FormCreate(Sender: TObject) ;
begin
  WebBrowser1.Navigate('http://delphi.about.com') ;
end;


//this handles the OnNavigateComplete event of WebBrowser1
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
   const pDisp: IDispatch; var URL: OleVariant) ;
begin
   ((Sender as TWebBrowser).Document as IHTMLDocument2).designMode := 'on';
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Note: The IHTMLDocument2's designMode property is used to put WebBrowser into a mode where a user can edit the current document.

While the browser is in design mode, objects enter a UI-activated state when the user presses ENTER or clicks an object that has focus, or when the user double-clicks the object.

Once the editing is finished you should set designMode to 'no' - then you can save the changed document's HTML to a file.

More TWebBrowser tips and tricks.

Delphi tips navigator:
» How to place a TEdit inside a TListBox to simulate inplace editing of ListBox items
« The role of the "AOwner" parameter in the Create constructor of Delphi components

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TWebBrowser
  6. How to enable editing of a document in TWebBrowser (in Delphi applications)

©2009 About.com, a part of The New York Times Company.

All rights reserved.