9

Possible Duplicates:
Disable browser's back button
How do I disable the F5 refresh on the browser?

Hi,

I created an application in C# that will download data from the internet (and this is done one time only) and put it in a webbrowser and this data should be static. I want to know if there is a way to disable the F5 key from doing a refresh?

I tried injecting javascript to disable F5 but it will still refresh the webbrowser.

Community
  • 1
  • 1
jantox
  • 2,185
  • 4
  • 21
  • 22
  • 1
    also there are many similar questions: http://stackoverflow.com/search?q=disable+refresh+browser – mauris Oct 25 '10 at 02:11
  • 5
    I can't cast a vote to reopen, but I would if I could. This question is asking about disabling F5 in a `WebBrowser` control, which the 'possible duplicates' do **not** address. – josh3736 Oct 25 '10 at 16:02
  • 2
    I don't really know why this is a duplicate entry. This is using WebBrowser control on a Windows Form. – jantox Oct 26 '10 at 07:25
  • 2
    Voted to re-open, it's unique from the other questions for the reasons stated by the two commenters above. – Andrew Barrett Nov 28 '11 at 17:45
  • I'd vote for a reopen if I could as well. The duplicates listed aren't at all related to WebBrowserControl... Did anyone ever figure out a solution that would allow for CTRL+C to still work? – blak3r Sep 19 '12 at 23:35
  • I would also vote to reopen question as this refers to a WebBrowser control which none of the other questions seem to. The question could be made clearer. – PhillC Aug 29 '13 at 11:21
  • As an aside, I managed to solve this problem by handling the previewKeyDown event and looking for f5 and setting event.handled to true – PhillC Aug 29 '13 at 11:56

2 Answers2

21

You cannot disable F5 (or other browser shortcut keys) via JavaScript.

Setting the WebBrowserShortcutsEnabled property of the WebBrowser control to false should accomplish what you're trying to do.

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • Got a problem. It disables keyboard shortcuts but also disables links that will open IE. – jantox Oct 25 '10 at 08:48
  • 2
    Handle the `WebBrowser.Navigating` event: `private void WebBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e) { if (e.Url.ToString() != "about:blank") { System.Diagnostics.Process.Start(e.Url.ToString()); e.Cancel=true; } }` – josh3736 Oct 25 '10 at 16:09
  • Windows acts weird. Tried pressing F5 continuously then clicking a link but it will not respond to it. But, if you say you put MessageBox onClick event, it will respond to it. – jantox Oct 26 '10 at 07:26
  • 5
    Quick warning, this disables all shortcut keys, so no Ctrl-C/Ctrl-V etc... – Andrew Barrett Nov 28 '11 at 17:46
  • 1
    If anyone is struggling with the lack of Ctrl-C/Ctrl-V, I just posted up some sample code to get around this, you can find it here: http://blog.intninety.co.uk/2012/08/disable-shortcuts-in-webbrowser-control-and-keep-copy-and-paste/ Hope this helps anyone else who may be stuck with it! –  Aug 19 '12 at 22:10
  • 3
    As mentioned by a few people setting WebBrowserShortcutsEnabled to false will indeed stop ALL shortcuts from working, which is far from ideal. Btw, the link provided by rastating doesn't work! – Sheldmandu Sep 01 '14 at 07:28
  • rastating's link is available in the Wayback Machine: https://web.archive.org/web/20130414212547/http://blog.intninety.co.uk/2012/08/disable-shortcuts-in-webbrowser-control-and-keep-copy-and-paste/ – mikiqex Jan 09 '20 at 14:56
-1

call this function on the onKeyDown event

 document.onkeydown = function(e) {
      // keycode for F5 function
      if (e.keyCode === 116) {
        return false;
      }
      // keycode for backspace
      if (e.keyCode === 8) {
        // try to cancel the backspace
        return false;
      }
    };

This will prevent from F5 reload and backspace back.

Azhar
  • 20,500
  • 38
  • 146
  • 211
  • this doesn't work. I tried this by injecting in webbrowser. On normal html this will work. – jantox Oct 26 '10 at 08:32
  • @Azhar where did you add the onkeydown event. It's not exposed by WebBrowser or WebBrowser.Document. I tried it on the parent form and it isn't invoked on keypresses. – blak3r Sep 19 '12 at 23:37