Is it possible to add a list of hyperlink controls in list box in c#? If possible, how can I do this? Also, I have two textboxes: one for Title and other for URL. When I add a new item, the values in the text boxes must be added to list box as a hyperlink where, title is the hyperlink title, and URL is the hyperlink URL. Can anyone help me?
Asked
Active
Viewed 1,903 times
5
-
By default, you cannot add hyperlinks into ListBox control – Nagaraj S Aug 07 '15 at 04:45
-
OK. then is there any other option to place a list of hyperlinks? – Aug 07 '15 at 04:54
-
Can you please post the code which you have tried.. – Krsna Kishore Aug 07 '15 at 05:47
-
Is is asp.net, wpf, winforms?? – Mahesh Malpani Aug 07 '15 at 06:13
-
Actually i need this for my share point application. it is visual webpart @Mahesh Malpani – Aug 07 '15 at 06:31
-
Then Tag it accordingly!! – TaW Aug 07 '15 at 06:48
1 Answers
0
This code is very simple but hope this will get you started. I added a ID to the link so you can have 2 of the same Display Values with different links. Hope this helps.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Hyperlink_control_using_List_box
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<hyperlinks> hll = new List<hyperlinks>();
private void Form1_Load(object sender, EventArgs e)
{
hyperlinks link1 = new hyperlinks();
link1.hyperlink_id = 1;
link1.hyperlink_name = "Google";
link1.hyperlink_link = "www.google.com";
hll.Add(link1);
hyperlinks link2 = new hyperlinks();
link2.hyperlink_id = 2;
link2.hyperlink_name = "Facebook";
link2.hyperlink_link = "www.facebook.com";
hll.Add(link2);
hyperlinks link3 = new hyperlinks();
link3.hyperlink_id = 3;
link3.hyperlink_name = "Yahoo";
link3.hyperlink_link = "www.yahoo.com";
hll.Add(link3);
listBox1.DataSource = hll;
listBox1.DisplayMember = "hyperlink_name";
listBox1.ValueMember = "hyperlink_id";
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
foreach (hyperlinks link in hll)
{
if (listBox1.SelectedValue.ToString() == link.hyperlink_id.ToString())
{
label1.Text = link.hyperlink_link;
}
}
}
}
public class hyperlinks
{
public int hyperlink_id { get; set; }
public string hyperlink_name { get; set; }
public string hyperlink_link { get; set; }
}
}

Psystec
- 1
- 1
- 4