Question

I created a little tool that loads up three web pages in front of each other. I use 3 buttons to bring their corresponding browser forward and back within a VB Form.

I created 3 buttons for forward and back and refresh and was able to use a little if/elseif/else logic to make them only interact with whichever browser is in front. I would like to add a keyboard shortcut to the buttons so that I can refresh the front browser with CTRL+R... Could someone give me guidance in how to do this?

Here is the form code: Public Class Form1 Dim Front As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.BringToFront()
        Front = "WebBrowser1"
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        WebBrowser2.BringToFront()
        Front = "WebBrowser2"
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        WebBrowser3.BringToFront()
        Front = "WebBrowser3"
    End Sub

    Private Sub BackBtn_Click(sender As Object, e As EventArgs) Handles BackBtn.Click
        If Front = "WebBrowser1" Then
            WebBrowser1.GoBack()
        ElseIf Front = "WebBrowser2" Then
            WebBrowser2.GoBack()
        ElseIf Front = "WebBrowser3" Then
            WebBrowser3.GoBack()
        End If
    End Sub

    Private Sub ForwardBtn_Click(sender As Object, e As EventArgs) Handles ForwardBtn.Click
        If Front = "WebBrowser1" Then
            WebBrowser1.GoForward()
        ElseIf Front = "WebBrowser2" Then
            WebBrowser2.GoForward()
        ElseIf Front = "WebBrowser3" Then
            WebBrowser3.GoForward()
        End If
    End Sub

    Private Sub RefreshBtn_Click(sender As Object, e As EventArgs) Handles RefreshBtn.Click
        If Front = "WebBrowser1" Then
            WebBrowser1.Refresh()
        ElseIf Front = "WebBrowser2" Then
            WebBrowser2.Refresh()
        ElseIf Front = "WebBrowser3" Then
            WebBrowser3.Refresh()
        End If
    End Sub
End Class
Was it helpful?

Solution

Best way to handle such combination is to override ProcessCmdKey, the key combination will be caught anytime.

Example:

Paste the overriden function below in your form.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean

        If ((keyData And Keys.R) = Keys.R) And ((keyData And Keys.Control) = Keys.Control) Then
            MessageBox.Show("CTRL+R pressed")
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
End Class

Note : one would be tempted to use Control.ModifierKeys to catch CTRL but it won't work, the right approach is to use the one above using the bitwise And operator against keyData.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top