Created
May 16, 2017 07:39
-
-
Save tkouba/bfe86b36fbd9744049970754f3921013 to your computer and use it in GitHub Desktop.
Enhanced LinkLabel for Windows Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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