HtmlEditor reference

Key Properties


Set this to a ContextMenu component to display a custom menu when the control is right-clicked. Two things to note. First, this only works if the isContextMenuEnabled property is set to True. Second, if isContextMenuEnabled is True, and the ContextMenu property is not set, then the default IE context menu will display. It is actually possible to customize this menu as well, but more difficult than using a standard .NET ContextMenu component.

Key Methods

LoadDocument(string documentVal)

Use this method to assign an HTML string to the editor. This will replace (not append to) any existing HTML content.

Important note. This method is asynchronous. In other words, it returns before the HTML is displayed in the component. This means you cannot immediately start working with the HTML Document Object Model (DOM). When the HTML is fully loaded, the HtmlEditor will fire a ReadyStateChanged event with a value of “complete”. You should handle this event in order to know when you can work with the DOM. Another idea is to poll the ReadyState property until it is “complete”; but handling the event is a better solution.

The HtmlEditor tries to work out whether you have given it an Ansi string or an Unicode string. On Windows 98 or Me, the string will always be passed as Ansi (using Marshal.StringToHGlobalAnsi). On other versions of Windows, the string will be treated as Unicode unless you set the isAnsiStringAlwaysUsed property to True. If the string does not contain a Unicode bytemark, the HtmlEditor will add it before sending to Mshtml; this appears to be necessary for reliable display of extended characters.

The DocumentEncoding property is not used for loading an HTML string, but only comes into play when you read the string back.

Mostly you can ignore the Unicode aspect and just pass an HTML string without worrying about it. If you want your application to work the same way on both Windows 98 and XP, set isAnsiStringAlwaysUsed and use Html Entities for extended characters. Using Entities is recommended anyway; but be warned that Mshtml will unhelpfully convert these to extended characters when you read back the HTML content.

String GetDocumentSource()

This method returns the contents of the HtmlEditor as HTML.

Note there are a couple of oddities about this. In Edit mode, GetDocumentSource is a reliable means of grabbing the current HTML source, as currently modified via the DOM or by the user. If the control is not in Edit mode, then it appears you will get the HTML as orignally loaded, even if it has since been modified via the Mshtml DOM. In this case you might have to resort to the DOM methods to get the source (eg. OuterHTML).

Even in Edit Mode, the HTML returned will not be exactly the same as that returned by outerHTML. GetDocumentSource will usually include additional header content such as the DOCTYPE. There can also be odd differences: the DOM methods appear to convert HTML elements to uppercase, for example. There is no problem with using the DOM methods instead of GetDocumentSource if the results work better for you, though I'd expect GetDocumentSource to perform better (I've not verified this).

Internally, this method uses the IPersistStreamInit interface to ask Msthml to save its contents to a stream. It also attempts to deal properly with Unicode characters, by inspecting the content of the stream. If you run into problems with this, you can use the version of GetDocumentSource in the utils class, which lets you pass an System.Text.Encoding argument; in the case, the routine will use this Encoding to convert the stream data into a string.

Key Events

ReadyStateChanged Event

reference.txt · Last modified: 2007/09/23 09:25 by itwriting
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki