Results 1 to 3 of 3

Thread: URLs and RichTextBox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    Redondo Beach
    Posts
    25

    URLs and RichTextBox

    I have code that will make a portion of the text in a richtextbox look like a hyperlink but when you click on it the program doesn't launch.

    Does anyone know how to make it launch?

    This is what I am trying to do:

    A user will be typing in text in a RichTextBox and I want them to have the ability to add a URL or a link to a file on their harddrive. When they click the link I want the program to launch.

    Here is a link where I found info on how to make the text look like a hyperlink but when you select the link it doesn't do anything.

    http://www.vbforums.com/showthread....ht=rich+textbox

  2. #2
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308
    I don't know how to handle it with rich text box, but this code will launch the link, here an example on the click of a label.

    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    2.  
    3. Const SW_SHOWNORMAL = 1
    4.  
    5. Private Sub Label1_Click()
    6.     ShellExecute Me.hwnd, vbNullString, "http://www.hotmail.com", vbNullString, vbNullString, SW_SHOWNORMAL
    7. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    Redondo Beach
    Posts
    25
    I got most of it to work. My last problem, I hope, is how to detect when the users mouse is over the link? I need this so I can call the mouseup event which is where I will be calling the shellExecute. Here is my sample code.

    Code:
    Option Explicit
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const SW_MINIMIZE = 6
    Private Const SW_MAXIMIZE = 3
    Private Const SW_RESTORE = 9
    Private Const SW_SHOWNORMAL = 1
    Private Const WM_USER = &H400
    Private Const EM_AUTOURLDETECT = (WM_USER + 91)
    
    Public Sub DetectURL(p_RichText As Object, p_blnDetect As Boolean)
        Dim lngRet As Long
        Dim strText As String
        
        With p_RichText
            ' this line is needed because the function will not update the
            ' url if you had it before
            strText = .Text
            ' send message to detect urls
            ' notice the Abs function. This is needed to pass 0 or 1
            ' in VB true is -1, so we have to get the absolute value of that
            lngRet = SendMessage(RichTextBox1.hwnd, EM_AUTOURLDETECT, Abs(p_blnDetect), ByVal 0)
            ' rewrite the text into the RichText so it will change all URLs if you
            'had them before
            .Text = strText
        End With
    End Sub
    
    Private Sub RichTextBox1_Change()
        DetectURL RichTextBox1, True
        RichTextBox1.SelStart = Len(RichTextBox1.Text)
    End Sub
    
    Private Sub RichTextBox1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
        Dim tempFileName As String
    
        tempFileName = 'I need just the hyperlink in RichTextBox1.Text
        ShellExecute Me.hwnd, "Open", "iexplore.exe", tempFileName, "c:\Program Files\Internet Explorer", SW_SHOWNORMAL
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width