-1

I'm trying to find a way to make my Winforms app open a webpage when pressing F1. Currently, HelpProvider, a Microsoft default class, tries to open a CHM or html file, and due to it using a static method, there is no way to override it. I can't find a way to intercept the F1 command to HelpProvider, and only use my own method, or a way to override HelpProvider to point to a live webpage. Any help would be greatly appreciated.

Ben L.
  • 1
  • 2
    I think in the `KeyDown` event of the form you can control that behaviour, check [this answer](https://stackoverflow.com/a/33818966/12511801) or search for similar approaches. – Marco Aurelio Fernandez Reyes Sep 23 '21 at 21:59
  • Handle the KeyPress event, perhaps? Also, how is the HelpProvider being invoked, *exactly*? – Dai Sep 23 '21 at 21:59

1 Answers1

1

Maybe this can help you, it is very simple. Let's suppose we have only 1 textbox on our form. This textbox should have the URL to be opened. In this case we have 2 components where we have to subscribe to KeyDown event (depending on where the actual focus is): Form and TextBox.

private void MyCustom_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.F1)
        {
            System.Diagnostics.Process.Start(textBox1.Text);
        }
    }

Now you should subscribe with both of the components to this very event in your form constructor:

public Form1()
    {
        InitializeComponent();

        textBox1.KeyDown += MyCustom_KeyDown;
        this.KeyDown += MyCustom_KeyDown;
    }

Now if you type in any URL in the textbox and press F1 immediatelly (so the focus is on textbox) or type in the URL, click on the form outside of the textbox (so the focus is on the form), it will open the URL with your default browser.

Attila
  • 64
  • 2