1. Computing

The VB.NET LinkLabel

A Label Component on Steriods

By

LinkLabel, new in Visual Basic .NET, is a standard control that lets you embed web-style links in a form. Like a lot of VB.NET controls, this one doesn't do anything that you couldn't do before ... but with more code and more trouble. For example, VB 6 had the Navigate (and Navigate2 when the first one proved inadequate) methods that you could use with a URL text string to call a web page.

LinkLabel is much more convenient and trouble free than older techniques. But, in sync with .NET architecture, LinkLabel is designed to be used with other objects to do the whole job. You still need to use a separate command to start an email or browser for example. Example code is included below.

The basic idea is to put the email address or web URL into the Text property of a LinkLabel component, then when the label is clicked, the LinkClicked event is triggered. There are well over a hundred methods and objects available for the LinkLabel object including properties to handle everything you might want to do with a link like changing the color, text, position, how it behaves when you click it ... whatever! You can even check mouse buttons and positions and test whether the Alt, Shift, or Ctrl keys are pressed when the link is clicked. A list is shown in the illustration below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

An object with a really long name is also passed to this event: LinkLabelLinkClickedEventArgs. Fortunately, this object is instantiated with the nice short name used for all event arguments, e. The Link object has more methods and properties. The illustration below shows the event code and the Link object.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

You will normally use the Text property of the Link object to get a URL or email address and then pass this value to System.Diagnostics.Process.Start.

To bring up a web page ...

System.Diagnostics.Process.Start("http://visualbasic.about.com")

To start an email using the default email program ...

System.Diagnostics.Process.Start("mailto:" & "visualbasic@aboutguide.com")

But you're really limited only by your imagination in using the five overloads of the Start method. You could, for example, start the Solitaire game:

System.Diagnostics.Process.Start("sol.exe")

If you put a file in the string field, then the default processing program for that file type in Windows will kick in and process the file. This statement will display MyPicture.jpg (if it's in the root of drive C:).

System.Diagnostics.Process.Start("C:MyPicture.jpg")

You can use the LinkLabel almost like a button by simply putting any code you like in the LinkClicked event instead of the Start method.

The investigation of the hundred or so other possibilities is wa-a-a-y beyond the scope of this article, but here are a few examples to get you started.

One new concept used in LinkLabel is the idea that there can be multiple links in a LinkLabel and they're all stored in a LinkCollection type. The first element, Links(0), in the collection is created automatically although you can control what it is using the LinkArea property of LinkLabel. In the example below, the Text property of LinkLabel1 is set to "FirstLink SecondLink ThirdLink" but only the first 9 characters are specified as a link. The Links collection has a Count of 1 because this link was added automatically.

To add more elements to the Links collection, just use the Add method. The example also shows how ThirdLink can be added as an active part of the link.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

It's easy to associate different targets with the different parts of the Link Text. Just set the LinkData property. To make FirstLink target the About Visual Basic web page and ThirdLink target the main About.Com web page, simply add this code to the initialization (the first two statements are repeated from the illustration above for clarity):

LinkLabel1.LinkArea = New LinkArea(0, 9)
LinkLabel1.Links.Add(21, 9)
LinkLabel1.Links(0).LinkData = "http://visualbasic.about.com"
LinkLabel1.Links(1).LinkData = "http://www.about.com"

You might want to do something like this to customize links for different users. You could use code to make one group of users go to a different target than another group.

Microsoft "saw the light" about hyperlinks with VB.NET and included everything you might want to do with them.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. The VB.NET LinkLabel

©2014 About.com. All rights reserved.