Is it possible to open a LinkLabel
in the default computers web browser?
-
1well 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?– Davide PirasCommented Aug 22, 2011 at 22:18
-
1I 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.– acctmanCommented 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– beppe9000Commented Jan 26, 2016 at 19:28
Add a comment
|
5 Answers
yes - you can use System.Diagnostics.Process.Start(url)
in the "link clicked" event.
-
1so something like this private void linkSubmit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start(linkSubmit.text as String); }– acctmanCommented Aug 22, 2011 at 22:35
-
-
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);
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.
Try this solution it is better:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(((LinkLabel)sender).Text);
}
-
Agreed - code reuse using the sender object beats a dedicated handler per link/control. Commented Aug 4, 2022 at 16:20
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
});
}