.NET Web Browser Control Gotchas

How many of you folks out there have used the .NET Web browser control for Windows applications? I bet most of you, at some point, must have done some head-scratching as to why Microsoft did not make this control as powerful as it should have been. This control does offer all of the navigation functions and basic browser capabilities but there is lot more that could have been done to enchance it's utility value. After some persistent fighting with this control in our current project, here are some gotchas that I have to offer.

Where the heck is print?

There is a print method that is available on this control which directly prints the current web page without popping up the print dialog. This is fine but what if I want the print dialog to be show up? After some digging into the msdn documentation and googling around, I determined there was a way to do this. The trick is to use the execcommand method on the browser's document object. The syntax goes like this: 

 Browser.Document.ExecCommand("Print", false, 0)

I believe this method basically executes the specified command on the underlying Microsoft active-x/html control. There are number of different commands such as cut, copy, paste, undo, redo etc that are available. One thing to keep in mind is that all the edit commands operate directly on the DOM elements of the HTML document. 

What about Search?

This one did take quite a while to figure out.  The browser control did not offer anything directly for searching the current web page.  If  its the Control-F search (that you normally get to see on the browser) is what you want, there is a simple way to enable that on the browser control.  Set the WebBrowserShortcutsEnabled property to true and you have it. This also enables other shortcut operations like control-C, Control-V, Delete etc.  I came across a number of posts that talked about using interop to get to the underlying active-x control to accomplish functions like search, copy, cut, paste etc. I guess you can avoid all this trouble by making use of this property on the control.

Any other gotchas with this control? Feel free to let me know.

 

 


Related posts:

  1. Keeping up with Firefox 3: Six more bookmark-manager gotchas
  2. Don’t be lazy, download a good browser
  3. GWT 2.0: Cool Beans on In Browser Development Mode
  4. 2007 Predictions Coming True? – The Coming Browser Revolution
  5. How to use will_paginate with non-ActiveRecord collection/array

Comments: 6 so far

  1. The “gotcha” I’ve run into is that the webobject is great at throwing script errors and ignoring your IE settings to disable the display of script errors. Given that the webobject appears to be based on IE 6, most recent sites and just about all embedded ads throw these errors. You know of any way to suppress script error reporting?

    thanks,
    breeding@cox.net

    Comment by Bob Breeding, Sunday, July 5, 2009 @ 11:19 pm

  2. @Bob I know there is a way to suppress script errors by doing this “WebBrowser.ScriptErrorsSuppressed = true”. I think this should work if the IE setting to display script errors is ignored.

    Comment by Karthik, Tuesday, July 7, 2009 @ 8:35 am

  3. I have an application that won’t allow buttons on a website to fire the html click code for submit…something new in the i.e webbrowser control has broken how buttons work…do you have any idea???

    Comment by Mark, Thursday, July 9, 2009 @ 7:08 pm

  4. Anything regarding Ajax support? So far I haven’t found a way to get notified when a call is made using the XMLHttpRequest object. Do you guys know if there’s any specific tricky event that fires or is it that the WebBrowser control simply can’t access to this?

    Comment by Kenia, Tuesday, August 11, 2009 @ 7:39 am

  5. WebBrowser1 is displaying a PDF file.

    I added a Print button to display the printdialog box. It tried to do the following but it doesn’t work.

    Help pls. Thanks!

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
    WebBrowser1.WebBrowserShortcutsEnabled = True
    WebBrowser1.Document.ExecCommand(”Print”, True, 0)

    Catch ex As Exception
    MsgBox(”oops..” & ex.Message)
    End Try
    End Sub

    Comment by Richard, Friday, January 8, 2010 @ 8:47 am

  6. @Kenia – I m not sure if the control offers any ajax support. I know you can hook into DOM events in the browser and do things.

    @Richard – WebBrowser1.Document.ExecCommand(”Print”, True, 0)? Can you try passing in a “false”? WebBrowser1.Document.ExecCommand(”Print”, false, 0). It worked for me so I dont see any reasons why it wont for you.

    Comment by Karthik, Monday, January 11, 2010 @ 9:49 am

Leave a comment

Powered by WP Hashcash

Launch: Pathfinder Newsletter

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email


    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com