Поделиться через


Another little code snippet

Whenever I have to
code a "real" project, I end up building a bunch of components to deal with anything that seems
likely to reoccur
. Sometimes the class or Windows Forms control I've created
never gets used again, but often I end up using them in a whole bunch of
additional apps. Anyway, I think I'll post some of these little bits of
development work to my blog when it seems useful enough, and perhaps other
developers will be able to find this code when they are looking for some help.

This particular piece of code is
pretty simple; it is just a small extension to the LinkLabel class to allow it
to handle launching the appropriate link when clicked.

ns = "urn:schemas-microsoft-com:office:office" />

public
class
ClickableLinkLabel : LinkLabel

{

private string m_URL =
"about:blank";

public
ClickableLinkLabel()

{

}

protected override void OnLinkClicked

(LinkLabelLinkClickedEventArgs e)

{

ProcessStartInfo psi

= new
System.Diagnostics.ProcessStartInfo(m_URL);

psi.UseShellExecute = true;

System.Diagnostics.Process.Start(psi);

base.OnLinkClicked(e);

}

/// <summary>

/// Represents the link to
be navigated

/// to when the label is
clicked

/// </summary>

public string
URL

{

get

{

return
m_URL;

}

set

{

m_URL = value;

}

}

}

Comments

  • Anonymous
    March 09, 2003
    Good idea Duncan. That got me thinking... it would be good to create derived anchor controls ( such as the HyperLink control ) and alter the NavigateUrl properties so as to ensure a valid http(s)? address here's a simple-minded function to kick things off with:


    Function FixLink(ByVal objUrl As String) As String
    If objUrl Is System.DBNull.Value Then
    Return "about: blank"
    Else
    Dim urlPattern As String = "(?<httpBit>http(?:s?)://)?(?<urlBit>[^s]+)"
    Dim source As String = objUrl.ToString()

    If Regex.IsMatch(source, urlPattern) Then
    If Regex.Match(source, urlPattern).Groups("httpBit").Value.Length = 0 Then
    Return "http://" & Regex.Match(source, urlPattern).Groups("urlBit").Value
    Else
    Return source
    End If
    End If
    End If
    End Function