Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
please give me some techniques

thanks in advance
Posted
Comments
F-ES Sitecore 19-Jan-16 6:41am    
Have you googled "disable right click on webbrowser control"? If it's possible then someone will have posted how.

1 solution

The easiest way to do this is to disable the context menu of the document contained in the WebBrowser control e.g.
VB.NET
WebBrowser1.IsWebBrowserContextMenuEnabled = False

You could do this in the WebBrowser_Navigated event (rather than the Form_Load event) so that it applies to every page you might navigate to.

If you want to do something else when the User right-clicks in the browser control you actually want to handle the right-click rather than ignore it.

You could do something like this (the name of the function is arbitrary)
VB.NET
Private Sub WebDocument_MouseDown(ByVal sender As System.Object, ByVal e As HtmlElementEventArgs)
    If (e.MouseButtonsPressed = MouseButtons.Right) Then
        'Do what you want to do here
    End If
End Sub

And add the handler as
VB.NET
RemoveHandler WebBrowser1.Document.MouseDown, New HtmlElementEventHandler(AddressOf WebDocument_MouseDown)
AddHandler WebBrowser1.Document.MouseDown, New HtmlElementEventHandler(AddressOf WebDocument_MouseDown)
Note that I remove it first (there is no exception thrown if it doesn't exist) to prevent the handler being added multiple times.

Also note that if you have added a URL at design time then you should not add the handler in the Form_Load event as the WebBrowser.Document object will not yet exist (and an exception will be thrown). The WebBrowser_Navigated event is an appropriate place for this code. You will also need to set the IsWebBrowserContextMenuEnabled property to stop the context menu popping up after your WebDocument_MouseDown event has been processed
 
Share this answer
 
Comments
Joseph Chrzempiec 27-Dec-22 5:08am    
Hello, I have a question. I'm not running the default webbrowser1 in visual basic. I'm running browser that uses ChromiumWebBrowser. Can I change webbrowser1 to ChromiumWebBrowser to disable the right click?
CHill60 27-Dec-22 5:40am    
I don't know. That would depend on the browser control that you are using. You would need to consult the documentation for whatever that is

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900