Leveraging The Windows Forms WebBrowser Control (For The Win)

I’ve been working on a little utility to experiment with the XMPP protocol.  The idea was to write a tool that would allow me to send, receive, and display the XML stream and XML stanza messages at the core of XMPP.


Of course, I could have implemented it using a simple multi-line text box for the XML entry, but that would mean that I wouldn’t have nice things like syntax highlighting (for XML) and nice (auto) indentation.


On the desktop, I’m not familiar with any free Windows Forms editor controls that are capable of syntax highlighting.  But on the web side, there are several free, open source script libraries at our disposal.  For example, CodePress, EditArea, and CodeMirror.


I chose CodeMirror for this application as it was the simplest library that met my needs.



There really aren’t any tricks to this aside from getting the URL correct.  In this case, I have my static HTML content in a directory in my project:



And I set the content files to “Copy always” in the properties pane for the files so that they get copied to the output directory of the project.  To set the correct path, I find the executing directory of the application and set the URL properly:



Note that I check if the control is in design mode (the designer throws an error if you don’t do this since the path points to Visual Studio’s runtime directory instead of your applications output).  Now all that’s left is to get the script and style references correct in your HTML page:



And in your script:



The final part is getting your Windows forms code talking to the Javascript in the browser.  In this case, I’ve written some simple Javascript that interacts with the editor control:



From the application code, we can call these script functions using the InvokeScript method:



Awesome! You can see that I can both pass arguments into the Javascript functions and read the return data from the Javascript function as well.  The big win is that now you can take advantage of your favorite Javascript utilities in your Windows Forms applications.

You may also like...