Another little code snippet


Whenever I have to
code a “real” project, I end up building a bunch of components to deal with href="http://dotnetweblogs.com/duncanma/posts/3242.aspx">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.

face="Trebuchet MS" color=#008080>

style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> ns = "urn:schemas-microsoft-com:office:office" /> color=#000000> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 1">     
public
class
ClickableLinkLabel : LinkLabel


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 1">     
{


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> color=#000000> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: blue">private style="COLOR: blue">string m_URL =
“about:blank”;


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> color=#000000> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 2"> color=#000000>           
public
ClickableLinkLabel()


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 2">           
{


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 2">           
}


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> color=#000000> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: blue">protected style="COLOR: blue">override style="COLOR: blue">void OnLinkClicked


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
(LinkLabelLinkClickedEventArgs e)


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">       
{


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
ProcessStartInfo psi


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">               
=
new
System.Diagnostics.ProcessStartInfo(m_URL);


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
psi.UseShellExecute =
true color=#000000>;


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
System.Diagnostics.Process.Start(psi);


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>           
base color=#000000>.OnLinkClicked(e);


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">       
}


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> color=#000000> 


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: gray">/// style="COLOR: gray"><summary>


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: gray">/// Represents the link to
be navigated


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: gray">/// to when the label is
clicked


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: gray">/// style="COLOR: gray"></summary>


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>        style="COLOR: blue">public style="COLOR: blue">string
URL


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">       
{


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>           
get


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
{


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>               
return
m_URL;


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
}


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes"> color=#000000>           
set


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">    style="mso-spacerun: yes">        {


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">               
m_URL =
value color=#000000>;


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">           
}


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-spacerun: yes">       
}


style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"> style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> style="mso-tab-count: 1">     
}


color=#000000> 


 


 


Comments (1)

  1. 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://&quot; & Regex.Match(source, urlPattern).Groups("urlBit").Value

    Else

    Return source

    End If

    End If

    End If

    End Function