vb.nethyperlinklistboxvb.net-2010

Hyperlinks inside ListBox


ScreenShot of my project Hello, can someone help me with my project that I'm working on? Anything is appreciated!

So, what do I need? Its simple, I need link and name bars to actually create a hyperlink into the LSB listbox. Can someone please help me??

PS: Could you please keep it simple for me, I just started with this language.


Solution

  • OK This isn't as simple as I would like it to be for you but here goes. I'll try to explain as I go along

    OK all the bits code below goes in your Form1 class as one lump - without my explanations that is. What I've done here is create a new class called Hyperlink

    It has a couple of properties called Name and Link and a constructor called New

    Class HyperLink
        Private friendlyName As String
        Private link As String
        Public Property Name As String
            Get
                Return friendlyName
            End Get
    
            Set(ByVal value As String)
                friendlyName = value
            End Set
        End Property
    
        Public Property URL As String
            Get
                Return link
            End Get
    
            Set(value As String)
                link = URL
            End Set
        End Property
    
        Public Sub New(nm As String, ln As String)
            friendlyName = nm
            link = ln
        End Sub
    End Class
    

    Here I'm creating list called linkList - this will hold the list of hyperlinks - I'm doing this so that later on the listbox will be set to use this as it's source of list items

    Dim linklist As New List(Of HyperLink)
    

    This sub called addLinkToListbox actually does several things. The important bits are that it tells you program to temporarily stop responding when the index of the listbox changes(which happens even when you're changing the contents off the listbox)

    Then it adds a new **hyperlink* to the list of hyperlinks taking data from the link textbox and the name textbox.

    To actually refresh the data shown in the textbox, I have to change the listbox datasource to nothing and then back to to the linkList

    The next two lines tell the listbox to display the Name property inn the list box and when the item is actually clicked, to return the URL property.

    Finally, I tell the program to start responding again when the index of the listbox index changes.

    Private Sub addLinkToListbox(linkName As String, linkURL As String)
         RemoveHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
         linklist.Add(New HyperLink(linkName, linkURL))
         ListBox1.DataSource = Nothing
         ListBox1.DataSource = linklist
         ListBox1.DisplayMember = "Name"
         ListBox1.ValueMember = "URL"
         AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
    End Sub
    

    These last two bits of code are hopefully self-explanatory

        Private Sub btnAddLink_Click(sender As Object, e As EventArgs) Handles btnAddLink.Click
            addLinkToListbox(txtName.Text, txtLink.Text)
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            MessageBox.Show(ListBox1.SelectedValue.ToString)
        End Sub