Click here to Skip to main content
6,295,667 members and growing! (10,306 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » HTML Browser Controls     Beginner License: The Code Project Open License (CPOL)

Disable the WebBrowser control context menu

By Greg Osborne

A simple way to disable the WebBrowser control's context menu.
VB (VB 9.0), C# 3.0, .NET (.NET 3.5), Visual Studio (VS2008), WinForms, Dev
Posted:31 Dec 2008
Views:6,676
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
4 votes for this article.
Popularity: 2.21 Rating: 3.67 out of 5

1

2
2 votes, 50.0%
3

4
2 votes, 50.0%
5

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


Member
Visual Basic Developer since version 1.0
Occupation: Software Developer (Senior)
Company: Iowa Foundation for Medical Care
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralC# equivalnet code available ? Pinmemberghiutzu0:03 7 Jan '09  
AnswerRe: C# equivalnet code available ? PinmemberGreg Osborne3:29 7 Jan '09  
GeneralRe: C# equivalnet code available ? Pinmemberghiutzu3:43 7 Jan '09  
AnswerRe: C# equivalnet code available ? Pinmemberghiutzu22:09 7 Jan '09  
Generalsource code PinmemberMember 475411518:38 5 Jan '09  
GeneralRe: source code PinmemberGreg Osborne3:35 7 Jan '09  
GeneralThanks to both of you PinmemberSteveAbbott7:07 31 Dec '08  
GeneralRe: Thanks to both of you PinmemberGreg Osborne3:39 7 Jan '09  
GeneralContextMenuStrip property? Pinmemberpaillave5:39 31 Dec '08  
GeneralRe: ContextMenuStrip property? PinmemberGreg Osborne5:52 31 Dec '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 31 Dec 2008
Editor: Smitha Vijayan
Copyright 2008 by Greg Osborne
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project