Created
March 26, 2012 14:56
-
-
Save maximilian-krauss/2205671 to your computer and use it in GitHub Desktop.
LinkLabel with BB-Code Style Url support
This file contains 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.ComponentModel; | |
using System.Diagnostics; | |
using System.Drawing.Design; | |
using System.Text.RegularExpressions; | |
using System.Windows.Forms; | |
/* | |
* linkLabelEx - Ein Linklabel with BB-Code | |
* Copyright (c) 2012 by Maximilian Krauss | |
* http://kraussz.com | |
* License: | |
* Do whatever you want but DO NOT REMOVE THESE CREDITS! | |
*/ | |
namespace MaximilianKrauss { | |
internal sealed class linkLabelEx : LinkLabel { | |
private string _originalText; | |
private readonly Regex _urlEx; | |
public linkLabelEx() { | |
_urlEx = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); | |
base.Text = "LinkLabelEx"; | |
LinkArea = new LinkArea(0, 0); | |
} | |
//Eigenschaften verstecken, die der Benutzer nicht mehr verwenden können soll: | |
[System.ComponentModel.Browsable(false)] | |
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] | |
public override string Text { | |
get { | |
return base.Text; | |
} | |
set { | |
base.Text = value; | |
} | |
} | |
[System.ComponentModel.Browsable(false)] | |
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] | |
public new LinkArea LinkArea { | |
get; | |
set; | |
} | |
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] | |
[Category("Appearance")] | |
[Description("The Text associated with the Control.")] | |
[Bindable(true)] | |
[DefaultValue("")] | |
public string linkText { | |
get { return _originalText; } | |
set { | |
_originalText = value; | |
parseText(); | |
} | |
} | |
[Category("Behavior")] | |
[Description("Auto open Links with the default Browser")] | |
[DefaultValue(false)] | |
public bool autoOpenLinks { | |
get; | |
set; | |
} | |
private void parseText() { | |
MatchCollection matches = _urlEx.Matches(_originalText); | |
this.Text = _originalText; | |
this.Links.Clear(); | |
foreach (Match match in matches) { | |
if (match.Groups.Count < 3) | |
continue; | |
string link = match.Groups[1].Value; | |
string linkText = match.Groups[2].Value; | |
this.Text = this.Text.Replace(match.Value, match.Groups[2].Value); | |
int linkIndex = this.Text.IndexOf(linkText); | |
if (linkIndex == -1) | |
continue; | |
this.Links.Add(linkIndex, linkText.Length, link); | |
} | |
} | |
protected override void OnLinkClicked(LinkLabelLinkClickedEventArgs e) { | |
base.OnLinkClicked(e); | |
if (autoOpenLinks && !string.IsNullOrEmpty((string)e.Link.LinkData)) { | |
Process.Start((string)e.Link.LinkData); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment