Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.12/5 (4 votes)
See more:
Hello,

How can I add a hyperlink as listbox item?

Example:
VB
ListBox1.Items.Add(item, "http://www.google.com/")
Posted
Updated 16-Jan-20 5:40am
v3
Comments
Mitchell J. 15-Mar-14 21:39pm    
Do you want the URL to be clickable?
TheGoffy 15-Mar-14 21:45pm    
i need the item to work as link, so when i click the item then it throws me to the http://google.com/. i dont need to display the link. i just want my item to work as link.
TcJoshJohnson 15-Mar-14 22:26pm    
You'll want to subscribe to SelectedIndexChanged or SelectedValueChanged and grab the SelectedItem property of your ListBox. Then, you can do what you'd like with the selected item such as call Process.Start() with the link.

A very simple example of how you can treat list box items as links without displaying the actual URL:

C#
private ListBox _listBox1;

public void Initialize()
{
    MyObject myObject = new MyObject();
    myObject.DisplayName = "Google";
    myObject.URL = "http://www.google.com";

    _listBox1.Items.Add(myObject);
    _listBox1.DisplayMember = "DisplayName";
    _listBox1.SelectedIndexChanged += _ListBox1_SelectedIndexChanged;
}

private void _listBox1_SelectedIndexChanged(object pSender, EventArgs pArgs)
{
    MyObject myObject = (MyObject)_listBox1.SelectedItem;
    Process.Start(myObject.URL);
}
 
Share this answer
 
VB.NET
Public Class Form1
    Dim Link_ As New LinkLabel
    Dim Proc As Process
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Me.TextBox1.TextLength = 0 Then Exit Sub
        If Me.TextBox2.TextLength = 0 Then Exit Sub
        Link_ = New LinkLabel With {.Text = Me.TextBox1.Text, .Tag = TextBox2.Text}
        Me.ListBox1.Items.Add(Link_)
    End Sub

    Private Sub ListBox1_DoubleClick(sender As Object, e As System.EventArgs) Handles ListBox1.DoubleClick
        Dim Item As LinkLabel = ListBox1.SelectedItem
        If Item Is Nothing Then Exit Sub
        Proc = Process.Start(Item.Tag.ToString)
    End Sub

    Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        If e.Index = -1 Then Exit Sub
        Dim Item As LinkLabel = ListBox1.Items(e.Index)
        e.DrawBackground()
        e.Graphics.DrawString(Item.Text, e.Font, New SolidBrush(e.ForeColor), e.Bounds.X + 1, e.Bounds.Y)
    End Sub
End Class


Download Project Source Code[^]
 
Share this answer
 
v3
Comments
CHill60 13-Sep-23 5:55am    
That link is highly suspicious, I recommend using well known file repositories such as github.
Also - a code dump without commentary is not a good solution. Also - did you notice that the question is 9 years old? Also did you notice you haven't actually added anything new to the thread

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900