A highly rated post on the VB MSDN Forum recently asked “How can I open up a web browser when a user clicks on a button?”

The first problem is “How can you start a web browser to a specified URL”. This is actually very easy to do in windows. You only need to create a process where the name is the URL you are trying to navigate to and windows will take care of the rest. This is quickly demonstrated by clicking on Start->Run and typing in an url (http://www.msn.com).

Doing it from Code is almost as simple. Make sure that System.Diagonstics is imported into your project and the following Sub will work.

Public Sub OpenUri(ByVal target As Uri)
    Dim info As New ProcessStartInfo()
    info.FileName = target.ToString()
    info.UseShellExecute = True
    Process.Start(info)
End Sub

Second problem is how to make a button open the link when it’s clicked. For this you’ll just need to handle the Clicked event and call the above Sub with the appropriate URL.

But why stop there? The UI Web Controls have a LinkButton class that does much the same but there is no such class in System.Windows.Forms. Lets create one.

First step is to create a new Windows Control Library project. Then add a new User Control called LinkButton. Open up the Designer file and make it derive from Button.

Now we need to add a field to allow the developer to specify the link to open. This field should be of type URI to make sure we’re passed a valid URI. When setting the Link if there is currently no text displayed we should just display the link for ease of use.

        Private m_link As Uri
    
        Public Property Link() As Uri
            Get
                Return m_link
            End Get
            Set(ByVal value As Uri)
                m_link = value
    
                If m_link IsNot Nothing Then
                    If String.IsNullOrEmpty(Me.Text) Then
                        Me.Text = m_link.ToString()
                    End If
                End If
            End Set
        End Property

Now we need to actually open the link when the user clicks on the button. Override the OnClick method and put in essentially the same code as above.

Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
    MyBase.OnClick(e)

    If m_link Is Nothing Then
        MessageBox.Show(Me, "Must specify a valid Uri to display", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return
    End If

    ' Start a process to open the link in a browser
    Dim info As New ProcessStartInfo()
    info.FileName = m_link.ToString()
    info.UseShellExecute = True
    Process.Start(info)
End Sub

While our LinkButton works at this point, it looks just like any other button. To make it more apparent that this is a LinkButton we will have the text be underlined and blue so it looks more like a hyperlink

Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.Font = New Font(Me.Font, FontStyle.Underline)
    Me.ForeColor = Color.Blue
    Me.Text = String.Empty
End Sub

At this point we have a fully functional LinkButton class. Lets make it a bit better though. It’s possible for the developer to have the text of the button be completely different than the actual Link. For instance the button could say “Hello” and the link could point to www.msn.com. We’ll add a tooltip so the user can see where the link actually goes to.

Open up LinkButton in the designer and drag a tooltip onto the surface. Rename it m_toolTip. Now lets update the code that sets the link

Public Property Link() As Uri
    Get
        Return m_link
    End Get
    Set(ByVal value As Uri)
        m_link = value

        If m_link IsNot Nothing Then
            If String.IsNullOrEmpty(Me.Text) Then
                Me.Text = m_link.ToString()
            End If
            m_toolTip.SetToolTip(Me, m_link.ToString())
        Else
            m_toolTip.SetToolTip(Me, "No Link Provided")
        End If
    End Set
End Property

At this point we have a fully functional link button that is ready for re-use. See the attachment for the full code sample.


Share Post

Google+

comments powered by Disqus