Geeks With Blogs
Łukasz Kuryło's blog

The WebBrowser control uses behind the scenes the IE engine to render the pages. Fortunatelly/Unfortunatelly is that, we have together with the rendered page the IE-default shortcuts, the context menu shown after pressing the right mouse button etc.

To disable e.g. refreshing the page by pressing F5, we can set the WebBrowserShortcutsEnabled property to false.

To disable the context menu, the WebBrowser control have either a property, called IsWebBrowserContextMenuEnabled. When we set it to false, the context won't be shown after pressing a right mouse button.

At this moment there is only one element to disable. This is when I press the Backspace button. I don't want to have the possibility to go to the previous page by pressing the button on the keybord. Unfortunatelly the control doesn't have a build-in property to disable this behavious, but we can do it by using some javascript code. In the page we can paste this code to do it:

           document.onkeydown = function (e)
            {        
                var evt = e || window.event;
                if (evt)
                {
                    var keyCode = evt.charCode || evt.keyCode;
                    if (keyCode === 8)
                    {
                        if (evt.preventDefault)
                        {
                            evt.preventDefault();
                        } else
                        {
                            evt.returnValue = false;
                        }
                    }
                }
            };

This code will be executed when the page will be loaded.

 

The WebBrowser control api lets us execute some js code either. If we, e.g. want to disable the backspace button only in certain circumstances, we can make a function from this code and execute it using the api by

 

 webBrowser1.Document.InvokeScript("js function name to execute");
Posted on Monday, August 22, 2011 3:13 PM WinForms | Back to top


Comments on this post: Disabling the browser-default functonality in WinForms WebBrowser control

Comments are closed.
Comments have been closed on this topic.
Copyright © Łukasz Kuryło | Powered by: GeeksWithBlogs.net