Click here to Skip to main content
Licence CPOL
First Posted 31 Dec 2008
Views 27,409
Bookmarked 24 times

Disable the WebBrowser control context menu

By | 31 Dec 2008 | Article
A simple way to disable the WebBrowser control's context menu.

Introduction

While developing an application which uses the WebBrowser control that ships with Visual Studio, I realized I didn't want the default context menu to show up when the user right-clicks in the control because I had my own context menu functionality I wanted to implement. Well, you know the routine, search Google and other dev sites to find the answer. There were many solutions, most involving inheriting from some interface in MSHTML and casting that interface to a document, yadda, yadda, yadda.

It turns out that it was much simpler...is this something new or that no one else knows about, because every other solution was so convoluted?

The code

All you have to do is create a handler on the WebBrowser's Document.ContextMenuShowing event. To do this, place the following code in your form load method on a form with a WebBrowser control:

AddHandler Me.WebBrowser1.Document.ContextMenuShowing, AddressOf WebContextMenuShowing

Place this anywhere in the code:

Private Sub WebContextMenuShowing(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    'displays your contextmenustrip - you could leave it out to
    ' just disable the browsers context menu
    Me.WebBrowser1.ContextMenuStrip.Show(Cursor.Position)
    'suppresses the display of the browsers context menu
    e.ReturnValue = False
End Sub

That's all there is to it!

History

  • Original submittal - 12/31/2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Greg Osborne

Web Developer
American Equity Insurance
United States United States

Member

Visual Basic Developer since version 1.0
Java web developer
Currently developing in vb and c#

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionC# equivalnet code available ? Pinmemberghiutzu23:03 6 Jan '09  
AnswerRe: C# equivalnet code available ? PinmemberGreg Osborne2:29 7 Jan '09  
GeneralRe: C# equivalnet code available ? Pinmemberghiutzu2:43 7 Jan '09  
AnswerRe: C# equivalnet code available ? Pinmemberghiutzu21:09 7 Jan '09  
Generalsource code PinmemberMember 475411517:38 5 Jan '09  
GeneralRe: source code PinmemberGreg Osborne2:35 7 Jan '09  
GeneralThanks to both of you PinmemberSteveAbbott6:07 31 Dec '08  
GeneralRe: Thanks to both of you PinmemberGreg Osborne2:39 7 Jan '09  
Thanks...somtimes you just get a brain aneurism. Should have used the KISS pattern...
 
K - Keep
I - It
S - Simple
S - Stupid
 
Please rate my articles!
Greg Osborne

QuestionContextMenuStrip property? Pinmemberpaillave4:39 31 Dec '08  
AnswerRe: ContextMenuStrip property? PinmemberGreg Osborne4:52 31 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 31 Dec 2008
Article Copyright 2008 by Greg Osborne
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid