Shorten link but still clickable in richtextbox

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1219 Views - Last Post: 10 February 2013 - 05:51 PM Rate Topic: -----

#1 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 11:57 AM

Hello every one, i have been racking my brain trying to get this to work for days now but still cant find an answer.

I am scraping my web page for links which are directly linked to files , i can get all the links to list into a richtextbox, but i would like only the file name to show and not the path but still remain clickable to the original link, any help would be very much appreciated

heres my code so far


 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.Navigate("http://www.blahblahblah.blah")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim PageElements As HtmlElementCollection = WebBrowser1.document.GetElementsByTagName("a")
        RichTextBox1.DetectUrls = True
        For Each CurElement As HtmlElement In PageElements
            RichTextBox1.Text = RichTextBox1.Text.Replace("@", " ") & CurElement.GetAttribute("href") & Environment.NewLine



        Next
    End Sub






Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Shorten link but still clickable in richtextbox

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 12:19 PM

        Dim sFile As String = "get file location from href"
        MessageBox.Show(System.IO.Path.GetFileName(sFile))

The above shows how to obtain the filename and extension but I don't know at the moment how to insert this as a link in a RichTextBox.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 12:29 PM

There are two methods described here.

The first is simply to paint the text blue and check the click event of the RichTextBox, but this seems messy as you would need to store the full link address somewhere, such as in an Array.

The second (better) approach is to append a LinkLabel to the RichTextBox. I believe you would use its Link and Link.LinkData properties to store the linked address.

MSDN

This post has been edited by andrewsw: 07 February 2013 - 12:30 PM

Was This Post Helpful? 1
  • +
  • -

#4 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 01:12 PM

thank you for your reply il look into both of them now, what i want to happen is that, it scrapes the links into the richtextbox but then shows just the file name but the is still a clickable link corresponding to the the full link , sorry if i haven't explained it to well im not sure about the correct wording for what i want to do :)

Any chance you could give me an example of the link.link data approach please
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 01:26 PM

Quote

Any chance you could give me an example of the link.link data approach please

There is a full example in the MSDN page that I linked to. You just need to append the link to the RichTextBox, as demonstrated in the first link I provided.

This post has been edited by andrewsw: 07 February 2013 - 01:28 PM

Was This Post Helpful? 1
  • +
  • -

#6 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 01:35 PM

ok il give it ago , would i have to do a for each loop for every link it finds? thats the bit im stuck on , because theres going to be around 20 links
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 01:41 PM

I would probably store the links in a separate array. When one of the links in the RTB is clicked I would determine the index number for this clicked link (I'm guessing it would be available as part of a collection - otherwise you would have to loop through the links, comparing them in some way); then use this index number to follow the corresponding link in the array.

Get the links in the RTB First!
Store them in an array (as you append them);
Then you can worry about determining which of the links was clicked.

Actually, when you click a link, it is probably possible to read its LinkData values directly (without having to loop).

This post has been edited by andrewsw: 07 February 2013 - 01:43 PM

Was This Post Helpful? 1
  • +
  • -

#8 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 01:58 PM

View Postandrewsw, on 07 February 2013 - 01:26 PM, said:

Quote

Any chance you could give me an example of the link.link data approach please

There is a full example in the MSDN page that I linked to. You just need to append the link to the RichTextBox, as demonstrated in the first link I provided.



ok i have it working by using this code


  Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim lit As New LinkLabel
        Dim PageElements As HtmlElementCollection = WebBrowser1.document.GetElementsByTagName("a")
        RichTextBox1.DetectUrls = True
        For Each CurElement As HtmlElement In PageElements

            Dim sFile As String = CurElement.GetAttribute("href")
            Me.RichTextBox1.Controls.Add(lit)
            lit.Text = lit.Text & System.IO.Path.GetFileName(sFile) & Environment.NewLine






        Next
    End Sub







but the only problem is that its only showing the first one?? have i got my for loop or the way im adding the linklabel wrong?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 02:01 PM

Actually.. it wouldn't require an array. As each link is created I would attach a click-event handler (AddHandler) using a delegate (I would use an inline function - a lambda) to each link.

So each link with have its own click-event handler, which would know which link it is referring to, and therefore enable the link-address to be read directly (probably via e.Link.LinkDate).

That's what I would do :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 02:09 PM

There is only 20 minutes between your last two posts. You need to spend longer trying to do this for yourself :whistling:. (I assume you might be getting paid..?)

..but you are only creating one (New) link. You need to create a new one each time, within the loop.
Was This Post Helpful? 0
  • +
  • -

#11 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 02:16 PM

View Postandrewsw, on 07 February 2013 - 02:01 PM, said:

Actually.. it wouldn't require an array. As each link is created I would attach a click-event handler (AddHandler) using a delegate (I would use an inline function - a lambda) to each link.

So each link with have its own click-event handler, which would know which link it is referring to, and therefore enable the link-address to be read directly (probably via e.Link.LinkDate).

That's what I would do :bigsmile:/>



im not really sure what you mean is it ok if i send you the files in a zip and see if you can find the problem because im stumped completely?

ok i cant send a zip heres the full code

Imports System.Text.RegularExpressions
Imports System.Net
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1



 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.Navigate("http://www.freevintagegames.com/SMSarcade/newarcade/spectrum/A/1/")



    End Sub
    Dim lit As New LinkLabel

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        Dim PageElements As HtmlElementCollection = WebBrowser1.document.GetElementsByTagName("a")
        RichTextBox1.DetectUrls = True
        For Each CurElement As HtmlElement In PageElements

            Dim sFile As String = CurElement.GetAttribute("href")
            Me.RichTextBox1.Controls.Add(lit)

            lit.Text = lit.Text & System.IO.Path.GetFileName(sFile) & vbNewLine







        Next
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
    End Sub
End Class





theres just a button and richtextbox
Was This Post Helpful? 0
  • +
  • -

#12 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 02:28 PM

View Postandrewsw, on 07 February 2013 - 02:09 PM, said:

There is only 20 minutes between your last two posts. You need to spend longer trying to do this for yourself :whistling:/>. (I assume you might be getting paid..?)

..but you are only creating one (New) link. You need to create a new one each time, within the loop.


yes sorry buddy , thats the bit im stuck on why its only making the one link and not for each as i have told it, i cant work out what im doing wrong :/ i wouldnt say im a beginner but sometimes i hit a brick wall
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 04:08 PM

So have you been studying the links I provided..?

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        Dim PageElements As HtmlElementCollection = WebBrowser1.document.GetElementsByTagName("a")
        RichTextBox1.DetectUrls = True
        For Each CurElement As HtmlElement In PageElements

            Dim sFile As String = CurElement.GetAttribute("href")
            Dim lit As New LinkLabel

            lit.Text = System.IO.Path.GetFileName(sFile) & vbNewLine
            lit.AutoSize = True
            lit.Location = Me.RichTextBox1.GetPositionFromCharIndex(Me.RichTextBox1.TextLength)
            Me.RichTextBox1.Controls.Add(lit)
            Me.RichTextBox1.AppendText(lit.Text & "   ")
            Me.RichTextBox1.Selectionstart = Me.RichTextBox1.TextLength

        Next
    End Sub

and delete your original declaration of Dim lit As New LinkLabel.
Was This Post Helpful? 0
  • +
  • -

#14 superfurry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 07-February 13

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 04:57 PM

View Postandrewsw, on 07 February 2013 - 04:08 PM, said:

So have you been studying the links I provided..?

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

        Dim PageElements As HtmlElementCollection = WebBrowser1.document.GetElementsByTagName("a")
        RichTextBox1.DetectUrls = True
        For Each CurElement As HtmlElement In PageElements

            Dim sFile As String = CurElement.GetAttribute("href")
            Dim lit As New LinkLabel

            lit.Text = System.IO.Path.GetFileName(sFile) & vbNewLine
            lit.AutoSize = True
            lit.Location = Me.RichTextBox1.GetPositionFromCharIndex(Me.RichTextBox1.TextLength)
            Me.RichTextBox1.Controls.Add(lit)
            Me.RichTextBox1.AppendText(lit.Text & "   ")
            Me.RichTextBox1.Selectionstart = Me.RichTextBox1.TextLength

        Next
    End Sub

and delete your original declaration of Dim lit As New LinkLabel.



not sure if my last message sent, yes ive been studying them and looking at other things i got them to list but they all lit up when i clicked them , i tried your modified code to and it now clicks individual ones but in the background when i scroll its in text in the background and the linklabels don't move. ive been trying all night grrr :)any ideas?
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: Shorten link but still clickable in richtextbox

Posted 07 February 2013 - 05:12 PM

Maybe the link labels are positioned statically in the RTB(?). This would be annoying and I don't think this should be the case. They have an Anchor property so perhaps they can (somehow) be anchored at their initial position within the textbox; that is, anchored to the previous character.


Erm.. seems we can borrow code to InsertHyperlink into an RTB. This looks like it will be easier, the hard work having been done already. This is what happens when other people do your Googling for you :unsure:

This post has been edited by andrewsw: 07 February 2013 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2