One Step Further: Using the LinkLabel Control


One Step Further: Using the LinkLabel Control

Providing access to the Web is now a standard feature of many Windows applications, and with Visual Studio 2005, adding this functionality is easier than ever. You can create a Visual Basic program that runs from a Web server by creating a Web Forms project and using controls in the Toolbox optimized for the Web. Alternatively, you can use Visual Basic to create a Windows application that opens a Web browser within the application, providing access to the Web while remaining a Windows program running on a client computer. We'll postpone writing Web Forms projects for a little while longer in this book, but in the following exercise you'll learn how to use the LinkLabel Toolbox control to create a Web link in a Windows program that provides access to the Internet through Microsoft Internet Explorer or the default Web browser on your system.

NOTE
To learn more about writing Web-aware Visual Basic 2005 applications, read Chapter 20, “Creating Web Sites and Web Pages Using Microsoft Visual Web Developer and ASP.NET.”

Create the WebLink program

  1. On the File menu, click Close Project to close the Input Controls project.

  2. On the File menu, click New Project.

    The New Project dialog box appears.

  3. Create a new Visual Basic Windows Application project named MyWebLink.

    The new project is created, and a blank form appears in the Designer.

  4. Click the LinkLabel control in the Toolbox, and draw a rectangular link label object on your form.

    Link label objects look like label objects, except that all label text is displayed in blue underlined type on the form.

  5. Set the Text property of the link label object to the URL for the Microsoft Press home page:

    http://www.microsoft.com/learning/books/

    Your form looks like this:

    graphic

  6. Click the form in the IDE to select it. (Click the form itself, not the link label object.)

    This is the technique you use to view the properties of the default form, Form1, in the Properties window. Like other objects in your project, the form also has properties that you can set.

  7. Set the Text property of the form object to Web Link Test.

    The Text property for a form controls what appears on the form's title bar at design time and when the program runs. Although this customization isn't related exclusively to the Web, I thought you'd enjoy picking up that skill now, before we move on to other projects. (We'll customize the title bar in most of the programs we build.)

  8. Double-click the link label object, and then type the following program code in the LinkLabel1_LinkClicked event procedure:

    ' Change the color of the link by setting LinkVisited to True.  LinkLabel1.LinkVisited = True  ' Use the Process.Start method to open the default browser  ' using the Microsoft Press URL:  System.Diagnostics.Process.Start _    ("http://www.microsoft.com/learning/books/")

    I've included comments in the program code to give you some practice entering them. As soon as you enter the single quote character ('), Visual Studio changes the color of the line to green, identifying the line as a comment. Comments are for documentation purposes only—they aren't evaluated or executed by the compiler.

    The two program statements that aren't comments control how the link works. Setting the LinkVisited property to True gives the link that dimmer color of purple, which indicates in many browsers that the HTML document associated with the link has already been viewed. Although setting this property isn't necessary to display a Web page, it's a good programming practice to provide the user with information in a way that's consistent with other applications.

    The second program statement (which I have broken into two lines) runs the default Web browser (such as Internet Explorer) if the browser isn't already running. (If the browser is running, the URL just loads immediately.) The Start method in the Process class performs the important work, by starting a process or executable program session in memory for the browser. The Process class, which manages many other aspects of program execution, is a member of the System.Diagnostics namespace. By including an Internet address or a URL along with the Start method, I'm letting Visual Basic know that I want to view a Web site, and Visual Basic is clever enough to know that the default system browser is the tool that would best display that URL, even though I didn't identify the browser by name.

    An exciting feature of the Process.Start method is that it can be used to run other Windows applications, too. If I did want to identify a particular browser by name to open the URL, I could have specified one using the following syntax. (Here I'll request the Internet Explorer browser.)

    System.Diagnostics.Process.Start("IExplore.exe", _    "http://www.microsoft.com/learning/books/")

    Here two arguments are used with the Start method, separated by a comma. The exact location for the program named IExplore.exe on my system isn't specified, but Visual Basic will search the current system path for it when the program runs.

    If I wanted to run a different application with the Start method—for example, if I wanted to run the Microsoft Word application and open the document c:\myletter.doc—I could use the following syntax:

    System.Diagnostics.Process.Start("Winword.exe", _    "c:\myletter.doc")

    As you can see, the Start method in the Process class is very useful.

    Now that you've entered your code, you should save your project. (If you experimented with the Start syntax as I showed you, restore the original code shown at the beginning of step 8 first.)

  9. Click the Save All button on the Standard toolbar to save your changes, and specify c:\vb05sbs\chap03 as the location.

You can now run the program.

Run the WebLink program

TIP
The complete WebLink program is located in the c:\vb05sbs\chap03\weblink folder.

  1. Click the Start button on the Standard toolbar to run the WebLink program.

    The form opens and runs, showing its Web site link and handsome title bar text.

  2. Click the link to open the Web site at http://www.microsoft.com/learning/books/.

    Recall that it's only a happy coincidence that the link label Text property contains the same URL as the site you named in the program code. (It is not necessary that these two items match.) You can enter any text you like in the link label. You can also use the Image property for a link label to specify a picture to display in the background of the link label. The following figure shows what the Microsoft Press Web page looks like (in English) when the WebLink program displays it using Internet Explorer.

    graphic

  3. Display the form again. (Click the Web Link Test form icon on the Windows taskbar if the form isn't visible.)

    Notice that the link now appears in a dimmed style. Like a standard Web link, your link label communicates that it's been used (but is still active) by the color and intensity that it appears in.

  4. Click the Close button on the form to quit the test utility.

You're finished writing code in this chapter, and you're gaining valuable experience with some of the Toolbox controls available for creating Windows Forms applications. Let's keep going!



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

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