8.8 Opening Web Pages with a LinkLabel Control

 <  Day Day Up  >  

8.8 Opening Web Pages with a LinkLabel Control

You want to open the default system Web browser when a user clicks a link associated with a LinkLabel control.


Technique

A LinkLabel control simply serves as a label whose text and cursor is formatted to appear like a Web hyperlink. You must handle the LinkClicked event to create any additional functionality with the control.

When the LinkClicked event is fired , you can open the default Web browser by calling the System.Diagnostics.Process.Start method, passing a URL string to navigate to as the parameter. After the LinkClicked event handler is invoked, you can optionally set the LinkVisited property of the LinkLabel control to true to change the color of the text. This color is specified in the VisitedLinkColor property:

 
 private void linkLabel1_LinkClicked(object sender,     System.Windows.Forms.LinkLabelLinkClickedEventArgs e) {     System.Diagnostics.Process.Start( linkLabel1.Text );     linkLabel1.LinkVisited = true; } 

Comments

The LinkLabel is derived from the Label control, which is why there are so many similarities between the two. Although you might assume a LinkLabel opens a Web browser to a specified page, that is simply just one of any number of possibilities. There is no "default" behavior associated with a LinkLabel , which means it is up to you to define that behavior.

The System.Diagnostics.Process.Start is a powerful method that contains several overloads. These overloads use system interoperability to call a number of different system-defined methods . For instance, passing a ProcessInfo object or two strings (application name and command-line arguments), the Start method calls the CreateProcess method in the kernel32.dll file. In the example shown earlier, which accepts a single string parameter, the Start method eventually uses the ShellExecuteEx method in the shell32.dll library. These methods allow you to pass a string that represents a moniker. A moniker is a system-defined object that is associated with a certain task or object. For example, a URL moniker in this example is associated with an object that can launch the default system browser. Fortunately, all this complicated material is transparent to the developer using the Start method. In addition to a URL moniker, if you pass a string representing a path to a file, the file moniker associated with that file extension is called, which results in the file being opened in its associated application. In other words, passing the path to a text file in turn causes your default text editor to open and display that text file.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net