JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating a LinkLabel in Code

I'll create a link label in code with multiple hyperlinks in it in this and the following two topics. You can see this in the CreateLinkLabel example on the CD-ROM; to create this example, I've added a button, Button1, to a form with the text "Create LinkLabel". When the user clicks that button, I create a new link label control, giving it the text "Interested in Black Books? Click here to see them all!" (note that I must declare the new link label LinkLabel1 with the WithEvents keyword to indicate to Visual Basic that I want to let this object handle events):

Private WithEvents LinkLabel1 As LinkLabel

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    LinkLabel1 = New LinkLabel()
    LinkLabel1.AutoSize = True
    LinkLabel1.Location = New Point(15, 15)
    LinkLabel1.Size = New Size(135, 15)
    LinkLabel1.Text = _
        "Interested in Black Books? Click here to see them all!"
        

The hyperlinks in the new link label control are stored in its Links collection, and I can use the Add method to add new hyperlinks. You pass this method the start location for the hyperlink in the control's text, the length of the link text, and some text to associate with the hyperlink. I can add two hyperlinks to this control, connecting them to the text "Black Books" and "here":

Private WithEvents LinkLabel1 As LinkLabel
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    LinkLabel1 = New LinkLabel()
    LinkLabel1.AutoSize = True
    LinkLabel1.Location = New Point(15, 15)
    LinkLabel1.Size = New Size(135, 15)
    LinkLabel1.Text = _
        "Interested in Black Books? Click here to see them all!"

    LinkLabel1.Links.Add(14, 11, "info")
    LinkLabel1.Links.Add(33, 4, "www.coriolis.com")
        

We'll also need to connect an event handler to the new link label. I will connect an event handler named LinkLabel1_LinkClicked to the link label's LinkClicked event using the AddHandler method and the AddressOf operator (which returns the address the LinkLabel1_LinkClicked Sub procedure in this case), and then add the link label to the form's Controls collection to display it:

Private WithEvents LinkLabel1 As LinkLabel

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    LinkLabel1 = New LinkLabel()
    LinkLabel1.AutoSize = True
    LinkLabel1.Location = New Point(15, 15)
    LinkLabel1.Size = New Size(135, 15)
    LinkLabel1.Text = _
        "Interested in Black Books? Click here to see them all!"

    LinkLabel1.Links.Add(14, 11, "info")
    LinkLabel1.Links.Add(33, 4, "www.coriolis.com")

    AddHandler LinkLabel1.LinkClicked, AddressOf Me.LinkLabel1_LinkClicked
    Me.Controls.Add(LinkLabel1)
End Sub

Private Sub LinkLabel1_LinkClicked(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        
End Sub

The actual hyperlink is passed to us in the LinkLabel1_LinkClicked event handler's e argument as e.Link. I can set its Visited property to True to set its color to the color in the VisitedLinkColor property, like this:

Private Sub LinkLabel1_LinkClicked(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
    LinkLabel1.Links(LinkLabel1.Links.IndexOf(e.Link)).Visited = True
        
End Sub

When you run this program and click the Create LinkLabel button, you'll see the link label with two hyperlinks in it, as in Figure 5.16.


Figure 5.16: Creating a link label in code.

This is fine as far as it goes, but nothing happens when you click the links. To make them active, see the following two topics.

Related solution:

Found on page:

Adding and Removing Controls at Run Time

196

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor