Results 1 to 5 of 5

Thread: [RESOLVED] Checking for Selected Text and Replacing Selected Text

  1. #1
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Resolved [RESOLVED] Checking for Selected Text and Replacing Selected Text

    Hi guys ,
    I have a RichTextBox and i'm trying to be able to add links to selected text,
    So i've been messing around with the SelectedText but It's not working the way I thought.

    Code:
    Dim Selection As String = ""
            Dim link As String = ""
            Dim pathC As String = ""
    
     If RichTextBox1.SelectedText <> "" Then
                Selection = RichTextBox1.SelectedText
                If MsgBox("Would you Like to Add an external link?", MsgBoxStyle.YesNo, "Link type") = MsgBoxResult.Yes Then
                    link = InputBox("Type hyperlink", "Type hyperlink", "http://")
                    pathC = "<a href=""" & link & """>" & Selection & "</a>"
                    RichTextBox1.SelectedText.Replace(RichTextBox1.SelectedText, pathC)
    Else
    ...
    End If
     End If
    Thanks in advance,
    Mike

  2. #2
    Hyperactive Member
    Join Date
    Jul 11
    Location
    UK
    Posts
    438

    Re: Checking for Selected Text and Replacing Selected Text

    Code:
    RichTextBox1.SelectedText = pathC

  3. #3
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: Checking for Selected Text and Replacing Selected Text

    Quote Originally Posted by Inferrd View Post
    Code:
    RichTextBox1.SelectedText = pathC
    lol
    I was going the other way. it always amazes me how everything becomes so simple.

    Thanks a lot

  4. #4
    Hyperactive Member
    Join Date
    Jul 11
    Location
    UK
    Posts
    438

    Re: [RESOLVED] Checking for Selected Text and Replacing Selected Text

    np.

    Just a quick note: the String Replace method "Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string" (from the MSDN docs) and it's that "returns a new string" bit that tends to trip people up on occasion (me included). So your original approach should work as:

    Code:
    RichTextBox1.SelectedText = RichTextBox1.SelectedText.Replace(RichTextBox1.SelectedText, pathC)
    Bit of a mouthful, though. :P

  5. #5
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: [RESOLVED] Checking for Selected Text and Replacing Selected Text

    Is always good to know thanks mate

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •