Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Calling a subroutine by clicking a link in a textbox.

Hello all,
          I need to know how to create a hyperlink in a textbox (or richtextbox) that when clicked DOES NOT open a browser window, I would like it to call a subroutine instead with the clicked text as a parameter instead.

I also don't want the whole box clickable, or to call the routine, just a specfic word in the box.

For instance if the textbox contained, "Hello my name is Joe.", when the user clicks on Joe (which should be underlined and blue to look like a hyperlink) a subroutine would be called and "Joe" would be stored in the variable.  BUT, clicking on any other word would do nothing.

Thanks for any help or suggestions in Advance,
1 Solution
Try this:

Put a Multiline TextBox with scrollBars and write this code:

Private Sub Text1_DblClick()
If Text1.SelText <> "" Then myPro (Text1.SelText)
End Sub

Sub myProc(t As String)
MsgBox t
'your code goes here
End Sub

Hope this helps!

Here is an example of doing this with a richtextbox, you cannot set colours etc on the standard textbox anyway so try this out and see if you like it:

Private Sub Form_Load()
    Dim intPos1 As Integer
    Dim intPos2 As Integer
    With RichTextBox1
        .Text = "Hello this is some text that I want to test the linking thing with"
        For i = 1 To 5
            intPos1 = InStr(intPos2 + 1, .Text, " ")
            intPos2 = InStr(intPos1 + 1, .Text, " ")
            .SelStart = intPos1
            .SelLength = intPos2 - intPos1
            .SelBold = True
            .SelColor = vbBlue
            .SelUnderline = True
        .SelStart = Len(.Text)
    End With
End Sub

Private Sub RichTextBox1_Click()
    Dim intPos1 As Integer
    Dim intPos2 As Integer
    With RichTextBox1
        intPos1 = InStrRev(.Text, " ", .SelStart)
        intPos2 = InStr(.SelStart, .Text, " ")
        If .SelColor = vbBlue And .SelUnderline And .SelBold Then
            FollowLink Mid(.Text, intPos1, intPos2 - intPos1)
        End If
    End With
End Sub

Private Sub FollowLink(ByVal FollowedText As String)
    MsgBox "You Clicked on the Link : " & FollowedText
End Sub
CideAuthor Commented:
I love it!  Good work!  :)


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now