Click here to Skip to main content
6,305,776 members and growing! (17,386 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate License: The Code Project Open License (CPOL)

VB.NET Code Package: Email And Web LinkLabels

By George B Gilbert

Code for email and web LinkLabels including a test to see if the web link is valid.
VB, Windows, .NET, Visual Studio, Dev
Posted:11 Jan 2006
Updated:11 Jan 2006
Views:40,335
Bookmarked:22 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 3.38 Rating: 4.00 out of 5

1
1 vote, 14.3%
2

3
1 vote, 14.3%
4
5 votes, 71.4%
5

Demo project

Message displayed when the web link is broken

Contents

Introduction

Use this code to make a VB.NET LinkLabel control work for either email or web links. The email link starts the user's default email program. The web link code checks if the target web site can be reached before starting the user's default browser. If the web site does not respond for any reason, a message is displayed and no attempt is made to display the web site.

The code is available in a package file for Double Text users, as well as in a demo project for non-Double Text users.

Background

Being a long time VB 6 developer, I admit to being excited about VB.NET because of the functionality that the new language makes available. LinkLabel controls are part of that new functionality. When starting my first VB.NET project, I wanted to use LinkLabels in my About window for both email addresses and a link to my web site. Knowing that I would be using LinkLabels in just about every new VB.NET project, I set up the needed code in this Double Text library. With the Email And Web LinkLabels Library in place, I can now add LinkLabels wherever I need them, very quickly.

Double Text Users

Library Overview

The Email And Web LinkLabels (EWL) library consists of three source files (.DbC), and an overview (.DbO). The source file, "Email sub", is the only one needed for email LinkLabels. For web LinkLabels, both the "Web sub" and "WebSiteIsAvailable function" are needed. The source files are set up with the default LinkLabel control names of lnkEmail and lnkWeb.

The EWL source files generate fully commented code that is ready to compile.

  • The code is written with Option Strict On.
  • The repeated code has been tested multiple times.
  • The code is fully commented.
  • Region statements can be included or not included, each time each source file is repeated.

Unwrapping The Library

  1. Download and unzip the package file. The package file is named Link Labels.DbP.
  2. Unwrap the package in Double Text (File | Library Exchange | Unwrap). Save the library in an empty folder.

The library is ready to go. Repeat the code per the following instructions whenever you need a ready to compile copy. (If you don't like the way I've set up the source files, change them in the edit window.)

Using The Library

Before repeating more than one of the source files in this library, turn on the Rollover Responses command on the Options menu in the Double Text main window. With Rollover Responses turned on, you will only have to enter a developer's name once instead for each source file.

The steps in the instructions below apply as written only if you do not change how the source files are set up.

Repeating The Email Sub

Each time you need a copy of this code for an email LinkLabel, follow these steps in Double Text:

  1. In the main window, press F12 and browse to the EWL library.
  2. Click on the "Email sub" title.
  3. In the Region window, click the All button to select both items if you want the region statements included in your code and then click OK. To exclude the region statements, just click OK.
  4. For the "Name of email LinkLabel control" prompt, enter the name of the email LinkLabel control and click OK (or press Enter). You can also just press Enter to accept the default name (lnkEmail).
  5. For the "Developer's name" prompt, enter your name and click OK (or press Enter). You can also just press Enter to accept the default name (mine). (This prompt may not be displayed if you have Rollover Responses turned on.)

The source code is now on the clipboard. If you would like to review the code, press F4.

Adding The Email Sub To Your Project

After the email code has been repeated, add it to your project.

  1. In Visual Studio, open the code view for the form containing the email LinkLabel control.
  2. Place the insertion cursor where you want to add the control's LinkClicked sub.
  3. Press Ctrl^V.

Repeating The Web Sub

Each time you need a copy of this code for a web LinkLabel, follow these steps in Double Text. (You must also repeat the WebSiteIsAvailable function.)

  1. In the main window, press F12 and browse to the EWL library.
  2. Click on the "Web sub" title.
  3. In the Region window, click the All button to select both items if you want the region statements included in your code and then click OK. To exclude the region statements, just click OK.
  4. For the "Name of web site LinkLabel control" prompt, enter the name of the web LinkLabel control and click OK (or press Enter). You can also just press Enter to accept the default name (lnkWeb).
  5. For the "Developer's name" prompt, enter your name and click OK (or press Enter). You can also just press Enter to accept the default name (mine). (This prompt may not be displayed if you have Rollover Responses turned on.)

The source code is now on the clipboard. If you would like to review the code, press F4.

Adding The Web Sub To Your Project

After the web code has been repeated, add it to your project.

  1. In Visual Studio, open the code view for the form containing the web site LinkLabel control.
  2. Place the insertion cursor where you want to add the control's LinkClicked sub.
  3. Press Ctrl^V.

Note: When adding the Web sub code to a project, you must also add the supporting WebSiteIsAvailable function.

Repeating The WebSiteIsAvailable Function

Each time you need a copy of this code for a web LinkLabel, follow these steps in Double Text. (Do not repeat the code for this function unless you also repeat the "Web sub.")

  1. In the main window, press F12 and browse to the EWL library.
  2. Click on the "WebSiteIsAvailable function" title.
  3. In the Region window, click the All button to select both items if you want the region statements included in your code and then click OK. To exclude the region statements, just click OK.
  4. For the "Developer's name" prompt, enter your name and click OK (or press Enter). You can also just press Enter to accept the default name (mine). (This prompt may not be displayed if you have Rollover Responses turned on.)

The source code is now on the clipboard. If you would like to review the code, press F4.

Adding The WebSiteIsAvailable Function To Your Project

After the function code has been repeated, add it to your project.

  1. In Visual Studio, open the code view for the form containing the web site LinkLabel control.
  2. Place the insertion cursor where you want to add the WebSiteIsAvailable private function.
  3. Press Ctrl^V.

All of the above instructions for using the library are in the library overview. Click on the Float button in the Double Text main window whenever you need a refresher. (If you change any of the source files, don't forget to reflect those changes in the overview.)

Non-Double Text Users

Demo Project Summary

Download and unzip the demo project. Open the solution in Visual Studio. Both sub procedures and the function are in the F_LinkLabels form. The subs are in the "Link Labels" region, and the function is in the "Procedures" region.

When you need this code, use whichever tool you prefer to make a copy of the appropriate sub and function, and then make any needed modifications. You might want to find and change the following either before or after you paste the code into your project in the form containing the LinkLabel control(s).

  • If you don't want the Region statements, delete them.
  • Change the date the code is added to your project.
  • Change the name of the developer.
  • Find and replace the name of the LinkLabel control (unless you used the default control name).

lnkEmail_LinkClicked Sub Code

This code opens the user's default email program.

#Region " ... Email "
    Private Sub lnkEmail_LinkClicked(ByVal sender As System.Object, _
            ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
            Handles lnkEmail.LinkClicked
        '------------------------------------------------


        '     Date    Developer            Comments


        '  ---------- -------------------- --------------


        '  12/11/2005 G Gilbert            Original code


        '------------------------------------------------



        '------------------------------------------------


        ' Launch default email application


        '------------------------------------------------


        Dim EmailLink As String
        EmailLink = "mailto:" & _
                    lnkEmail.Text.Substring(e.Link.Start, e.Link.Length)
        System.Diagnostics.Process.Start(EmailLink)

    End Sub
#End Region

lnkWeb_LinkClicked Sub Code

This code tries to get a response from the linked web site before opening the user's default browser. If the linked web site does not respond (for any reason), a message is displayed and no attempt is made to open the browser.

#Region " ... Web Site "
    Private Sub lnkWeb_LinkClicked(ByVal sender As System.Object, _
                ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
                Handles lnkWeb.LinkClicked
        '-------------------------------------------------


        '     Date    Developer            Comments


        '  ---------- -------------------- ---------------


        '  12/11/2005 G Gilbert            Original code


        '-------------------------------------------------



        '-------------------------------------------------


        ' Local Constant/Variable Declarations


        '-------------------------------------------------


        Dim WebSite As String = _
            lnkWeb.Text.Substring(e.Link.Start, e.Link.Length)
        Dim WebURL As String = "http://" & WebSite

        '-------------------------------------------------


        ' Check for a response from the web site


        ' before starting the default web browser


        '-------------------------------------------------


        If WebSiteIsAvailable(WebURL) Then
            System.Diagnostics.Process.Start(WebURL)
        Else
            Dim xMsg As String = "Unable to connect to the site" & _
                                 ControlChars.CrLf & _
                                 WebSite & _
                                 ControlChars.CrLf & ControlChars.CrLf & _
                                 "(Are you connected to the Internet?!?)"
            MessageBox.Show(xMsg, _
                            "No Response", _
                            MessageBoxButtons.OK, _
                            MessageBoxIcon.Exclamation)
        End If

    End Sub
#End Region

WebSiteIsAvailable Function Code

This code attempts to get a response from the passed URL.

#Region " ... WebSiteIsAvailable Function "
    Private Function WebSiteIsAvailable(ByVal linkText As String) As Boolean
        '----------------------------------------------------


        ' Attempt to get a response from the passed URL


        ' Pass:       linkText    URL to site being checked


        ' Return:     True        The site responded


        '             False       The site did not respond


        '----------------------------------------------------


        '     Date    Developer            Code Change


        '  ---------- -------------------- ------------------


        '  12/11/2005 G Gilbert            Original code


        '----------------------------------------------------



        '----------------------------------------------------


        ' Local Constant/Variable Declarations


        '----------------------------------------------------


        Dim URL_Object As New System.Uri(linkText)
        Dim URL_WebRequest As System.Net.WebRequest
        Dim URL_WebResponse As System.Net.WebResponse
        Dim Response_Result As Boolean

        '----------------------------------------------------


        ' Attempt to get a response from the URL


        '----------------------------------------------------


        Try
            URL_WebRequest = System.Net.WebRequest.Create(URL_Object)
            URL_WebResponse = URL_WebRequest.GetResponse
            Response_Result = True
        Catch Any_Error As Exception
            Response_Result = False
        End Try
        URL_WebResponse = Nothing
        URL_WebRequest = Nothing
        URL_Object = Nothing

        '----------------------------------------------------


        ' Return the result


        '----------------------------------------------------


        Return Response_Result

    End Function
#End Region

Points Of Interest

The web link code performed as needed with all four combinations of no Internet connection, a good Internet connection, a valid web site, and a bogus web site. All four possible combinations tested successfully. The email code has only two scenarios ... a default email program and no default email program. I didn't bother to test the latter since Windows ships with IE. That might constitute an invalid assumption, but, if a person has Windows and no email program, what's up with that?

Conclusion

I expect to be using LinkLabels often in my projects. If for nothing else, the About windows will probably have at least one web site link and at least two email links. With the Email And Web LinkLabels Library, adding any number of LinkLabels to my code will be quick and practically effortless.


Licensing And Limitation Of Liability

You may use the Double Text library and all code offered in this article any way you choose without restriction.

Under no circumstances, and under no legal theory, tort, contract, or otherwise, will George Gilbert (hereafter referred to as "software author") or his licensors, be liable to the user of the Double Text library and all code offered in this article (hereafter referred to collectively as "article code") for any damages, including any lost profits, lost data, or other indirect, special, incidental or consequential damages, arising out of the use or inability to use the article code, and data or information supplied, even if the software author, his licensors or authorized dealer have been advised of the possibility of such damages, or for any claim by any other party.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

George B Gilbert


Member
Click here to see a complete list of my articles.
Occupation: Software Developer
Location: United States United States

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralThanx very much Pinmembereng / Mahmoud abdel Aziz elgindy0:59 7 Dec '08  
GeneralRe: Thanx very much PinmemberGeorge B Gilbert6:36 7 Dec '08  
Generalthankes Pinmembergoldman141511:00 24 Jun '08  
GeneralRe: thankes PinmemberGeorge B Gilbert11:22 24 Jun '08  
GeneralGreat Article Pinmemberumertahir2:10 16 Oct '07  
GeneralRe: Great Article PinmemberGeorge B Gilbert4:52 16 Oct '07  
GeneralAwesome Article PinmemberBAbrian1:19 11 Feb '07  
GeneralRe: Awesome Article PinmemberGeorge B Gilbert3:49 11 Feb '07  
GeneralHow to open website explicitly in new window? PinmemberMehdiAnis7:30 19 Oct '06  
GeneralRe: How to open website explicitly in new window? PinmemberGeorge B Gilbert8:53 19 Oct '06  
Question"Double Text user"? Pinmembersabine3221:21 18 Jan '06  
AnswerRe: "Double Text user"? PinmemberMansonP2:35 19 Jan '06  
AnswerRe: "Double Text user"? PinmemberGeorge B Gilbert5:49 19 Jan '06  
AnswerRe: "Double Text user"? Pinmembersabine328:10 19 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jan 2006
Editor: Sean Ewington
Copyright 2006 by George B Gilbert
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project