Skip to content

Instantly share code, notes, and snippets.

@tkouba
Created May 16, 2017 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkouba/bfe86b36fbd9744049970754f3921013 to your computer and use it in GitHub Desktop.
Save tkouba/bfe86b36fbd9744049970754f3921013 to your computer and use it in GitHub Desktop.
Enhanced LinkLabel for Windows Forms
using System;
using System.Text.RegularExpressions;
namespace Arci.Windows.Forms
{
public class MultiLinkLabel : System.Windows.Forms.LinkLabel
{
protected override void OnTextChanged(EventArgs e)
{
CreateLinks();
base.OnTextChanged(e);
}
void CreateLinks()
{
Links.Clear();
// Get all links in text, supported links are http, ftp, https, ftps and mailto
MatchCollection matches = Regex.Matches(Text, @"((?:(?:(?:http|ftp|https|ftps):\/\/)|(?:mailto:))[\w\-_:@]+(?:\.[\w\-_]+)+(?:[\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");
foreach (Match match in matches)
{
Links.Add(match.Index, match.Length, match.Value);
}
}
}
}
private void multiLinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Link.LinkData != null)
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment