![]() |
Languages »
VB.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
VB.NET Code Package: Email And Web LinkLabelsBy George B GilbertCode for email and web LinkLabels including a test to see if the web link is valid. |
VB, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Demo project

Message displayed when the web link is broken
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.
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.
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.
Option Strict On. Region statements can be included or not included, each time each source file is repeated. 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.)
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.
Each time you need a copy of this code for an email LinkLabel, follow these steps in Double Text:
LinkLabel control and click OK (or press Enter). You can also just press Enter to accept the default name (lnkEmail). The source code is now on the clipboard. If you would like to review the code, press F4.
After the email code has been repeated, add it to your project.
LinkLabel control. LinkClicked 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.)
LinkLabel control and click OK (or press Enter). You can also just press Enter to accept the default name (lnkWeb). The source code is now on the clipboard. If you would like to review the code, press F4.
After the web code has been repeated, add it to your project.
LinkLabel control. LinkClicked sub. Note: When adding the Web sub code to a project, you must also add the supporting 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.")
The source code is now on the clipboard. If you would like to review the code, press F4.
After the function code has been repeated, add it to your project.
LinkLabel control. WebSiteIsAvailable private function. 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.)
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).
LinkLabel control (unless you used the default control name). 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
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
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
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?
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.
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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 Web21 | Advertise on the Code Project |