1. Computing & Technology

Discuss in my forum

How to enable editing of a document in TWebBrowser

By , About.com Guide

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.