33

Is it possible to open a LinkLabel in the default computers web browser?

3
  • 1
    well it's the default behavior once you have set a proper valid url. What kind of problem are you having and how does your code look like so far? Commented Aug 22, 2011 at 22:18
  • 1
    I was looking in the Properties for something that would start it. Originally I tried just setting a url address to the .Text property and of course that didn't work.
    – acctman
    Commented Aug 22, 2011 at 22:32
  • i don't understand the existence of this control, probably because i do don understand how to use it
    – beppe9000
    Commented Jan 26, 2016 at 19:28

5 Answers 5

57

yes - you can use System.Diagnostics.Process.Start(url) in the "link clicked" event.

3
  • 1
    so something like this private void linkSubmit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(linkSubmit.text as String); }
    – acctman
    Commented Aug 22, 2011 at 22:35
  • Thank you... :D :D Commented Aug 16, 2017 at 13:55
  • stackoverflow.com/a/61035650/2152973 Commented Sep 23, 2021 at 14:44
11

I always use them like this. This way you will get the default browser to open the URL.

ProcessStartInfo sInfo = new ProcessStartInfo("http://www.google.com");
Process.Start(sInfo);
10

Here's a solution inspired by MSDN that works without hardcoding the URL into your code:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    string url;
    if (e.Link.LinkData != null)
        url = e.Link.LinkData.ToString();
    else
        url = linkLabel1.Text.Substring(e.Link.Start, e.Link.Length);

    if (!url.Contains("://"))
        url = "https://" + url;

    var si = new ProcessStartInfo(url);
    Process.Start(si);
    linkLabel1.LinkVisited = true;
}

You can then easily use LinkArea to have un-hyperlinked text around the link.

2

Try this solution it is better:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(((LinkLabel)sender).Text);
}
1
  • Agreed - code reuse using the sender object beats a dedicated handler per link/control.
    – SteveCinq
    Commented Aug 4, 2022 at 16:20
0

Here is my Solution:

public Main()
{
    InitializeComponent();
    ...
    linkLabel1.Links.Add(0, linkLabel1.Text.Length, "https://www.example.com");
}

And Here the Click-Function:

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(new ProcessStartInfo
    {
        FileName = e.Link.LinkData.ToString(),
        UseShellExecute = true
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.