Making a part of the text a url

How do you make a part of the text a url ... like for instance

"blah blah ....
 http://www.experts-exchange.com
 blah blah..."

I think they make it a link automatically on expert-exchange ... but in a text box that has that ... how could i make it a link?
LVL 10
ThaSmartUnoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mark2150Commented:
Depends on what control you're talking about. RTF box, TextBox, Labels, have no knowledge of their content and will ignore URLS as just another string of characters. Web control will recognize the URL, but is essentially impossible to place dynamically created text into and doesn't support editing.

You can code around this with a RTB. The RTB allows you to set colors and underline for portions of text. You could automatically convert the URL into blue/underlined and you could also track the cursor position. In the double click event if you see that you're positioned over a URL then pass the URL to the web control and the page will come up. This would be a moderate effort to code but should work.

If you want the URL to come up *in* the RTB, that's not going to happen. But you can simulate it by laying the web control directly over the RTB and setting the .Visible property to false. When the URL is selected toggle both the RTB and Web controls .visible property (rtb off, web on) and from the users perspective the URL that they just typed will appear.

The Web control supports a [Back] functionality that returns an error when there is no [back] left (you're at the top of the tree...) Anyway, this can then signal that your software should toggle back and hide the web control and have the RTB re-appear so the user can continue editing.

Should work ok, but not trivial to code.

M
0
ThaSmartUnoAuthor Commented:
so if i had a rtf text box ... and it had that in it ... i couldn't make a link?
0
ThaSmartUnoAuthor Commented:
and if i can ... could u paste some code in? Thanks in advance.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mark2150Commented:
The RTB is not aware that a string starting HTTP is anything other than a series of characters. It doesn't know a "URL" from anything else that is typed into it. You have to look for the string and decide to highlight it.

In the keyup event watch for a space or enter. Then scan the .Text looking for "HTTP" or "WWW".

  IF INSTR(UCASE(RTB.TEXT), "WWW") ...

If you find it search backwards and forwards for the next space of vbCrLf pair to identify the ends of the URL. Use SetStart and SelLenght to highlight the text and apply an underline and color to make it look like a URL.

On the mouse click event look at the .SelStart and if it's between the .SelStart and .SelStart + .SelLength of the URL then you trigger your web control.

For 50 points this is as close to code as I'm going to give...

M
0
ThaSmartUnoAuthor Commented:
how bout this for the code?
0
mark2150Commented:
Ok, I'll see what I can do tomorrow... Too late tonight...

M
0
ThaSmartUnoAuthor Commented:
k thanks ... ill check back either tonight or monday night
0
mark2150Commented:
Ok. This more or less works. Create a form and place a RichTextBox and a Web control on it. Drop in the following code:

'
' Intranet web browser
' Copyright 1999 - Mark M. Lambert - All Rights Reserved
' By: Mark M. Lambert on June 15, 1999
'
' V2.0.x - 20 Dec 99 - MML - Make RTB hotlink
' V1.0.x - 15 Jun 99 - MML - Initial Code
'
Option Explicit
'

Private Sub Form_Click()
On Error GoTo AtTop
web.GoBack
On Error GoTo 0
Exit Sub
' ===========
'
' Here when we're at top of web tree
'
AtTop:
Resume AtTop1
'
AtTop1:
web.Visible = False
RTB.Visible = True
'
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'
' Here every time a key is pressed. Watch for space and then if we see one look to see if we just
' typed a URL
'
If Chr(KeyCode) <> " " Then Exit Sub
'
Dim Work        As String
Dim Marker1     As Integer
Dim Marker2     As Integer
Dim Ptr         As Integer
'
Work = RTB.Text
Ptr = 1
'
Marker1 = InStr(Ptr, UCase(Work), "HTTP://", vbTextCompare)
Do While Marker1 > 0
    '
    ' ok, found head of what could be a URL. Look for tail
    '
    Marker2 = InStr(Marker1, Work, " ")
    If Marker2 > Marker1 Then
            RTB.SelStart = Marker1 - 1
           RTB.SelLength = Marker2 - Marker1
            RTB.SelColor = vbBlue
        RTB.SelUnderline = True
    End If
    '
    Ptr = Marker1 + 1
    Marker1 = InStr(Ptr, UCase(Work), "HTTP://", vbTextCompare)
    '
Loop
'
RTB.SelStart = Len(RTB.Text)
RTB.SelLength = 0
RTB.SelUnderline = False
'
End Sub

Private Sub Form_Load()
'
Me.Caption = Me.Caption & " - V" & App.Major & "." & App.Minor & "." & App.Revision
'
web.Top = RTB.Top
web.Left = RTB.Left
web.Width = RTB.Width
web.Height = RTB.Height
web.Visible = False
RTB.Visible = True
'
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'
Dim frm As Form
'
For Each frm In Forms
    Unload frm
    Set frm = Nothing
Next frm
'
End
'
End Sub

Private Sub RTB_Click()
'
Dim Marker1     As Integer
Dim Marker2     As Integer
Dim aChar       As String
'
' Check to see if we're on URL
'
If Not RTB.SelUnderline Then Exit Sub       'Nothing to do if we're not a URL
'
Marker1 = RTB.SelStart
'
Backup:
aChar = Mid(RTB.Text, Marker1, 1)
If (aChar = Space(1) Or aChar = vbLf) Then
Else
    Marker1 = Marker1 - 1
    If Marker1 > 0 Then GoTo Backup
    '
End If
'
' Ok, found start, now look for end
'
Marker1 = Marker1 + 1
Marker2 = RTB.SelStart
'
Forward:
aChar = Mid(RTB.Text, Marker2, 1)
If (aChar = Space(1) Or aChar = vbCr) Then
    If Marker2 > Marker1 Then
        web.Navigate Trim(Mid(RTB.Text, Marker1, Marker2 - Marker1))
        RTB.Visible = False
        web.Visible = True
        Exit Sub
        ' ============
        '
    End If
    '
Else
    Marker2 = Marker2 + 1
    If Marker2 <= Len(RTB.Text) Then GoTo Forward
    '
End If
'
End Sub

This changes text that starts with HTTP:// and ends with a space into blue and underlined and if you click on it (after a delay) the page will appear.

Clicking on the body of the form acts like a [Back] button and when you're all the way back to where you started the RTB re-appears and you're back in edit mode.

Took less than 45 minutes to write from a cold, standing, start.

If you have trouble getting all of the web controls and such loaded simply download the mini-web browser from my web page and use that as the underlying project (which is what I did...)

M

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ThaSmartUnoAuthor Commented:
this did not work for me ... I added WebBrowser1 (renamed to Web) and a RichTextBox (renamed to RTB) and i typed in the RTB "http://www.experts-exchange.com " and it didn't work
0
mark2150Commented:
As I said in the last para of my previous post, use the project I referenced as a "seed" for this. You have to have several components loaded over and above the raw controls. It's easier to load the project and paste this code in than it is to hunt for SHDOCVW.OCA and .DLL. All of this is dependent on IE4 or higher being installed on the system.

M


0
ThaSmartUnoAuthor Commented:
well ...  i have IE 5 ... and what didn't work was that the RTB didn't change the text's colo
0
ThaSmartUnoAuthor Commented:
color that is
0
mark2150Commented:
Try typing it with some other text around it. It worked fine on my system.

Now is the time to visit http://cyberchute.com/rvbus/madmark and see all the fun stuff I have online

M
0
ThaSmartUnoAuthor Commented:
I put a breakpoint inside the sub
"Form_KeyUp" ... right at the first statement ... the If statement ... I started typing stuff ... and it never broke ...
0
ThaSmartUnoAuthor Commented:
I changed it to RTB_KeyUp ... and it works now
0
ThaSmartUnoAuthor Commented:
Thank you
0
mark2150Commented:
I should have mentioned that you had to set the form Keypreview to true...

Sorry. Is problem with sending code and not project...

M
0
ThaSmartUnoAuthor Commented:
=) ... thanx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.