Format links

Mattia Fanti 356 Reputation points
2022-10-16T22:42:04.133+00:00

Hello, I would like to know if it possible to format the following text with links:
input

match 1   
1: http://www.google.it    
2: http://www.example.it   
3: http://www.example2.it  
  
match2 15:30  
1: http://www.google.it  
2: http://www.example.it  
3: http://www.example2.it  

into the output

 match 1   
 1: LINK1  
 2: LINK2  
 3: LINK3  
      
 match2 15:30  
 1: LINK1  
 2: LINK2  
 3: LINK3  

I would like maybe to display it in a richtextbox or whatever control.. Just wondering if it's possible to "hide" the real link under a link label. Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,610 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-10-17T00:05:32.877+00:00

    Hi
    Here is a stand alone example. I have just used the same url for all the links, but each with the text as per your question. You can just edit to suit.
    There is a RichTextBox1 on Form1.

    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Dim urls() As String = {"www.google.com", "www.google.com", "www.google.com"}  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Dim c As Integer = 1  
        For Each s As String In urls  
          Dim link As LinkLabel = MakeLink("LINK" & c.ToString, s)  
          RichTextBox1.Controls.Add(link)  
          c += 1  
        Next  
      End Sub  
      Function MakeLink(text As String, url As String) As LinkLabel  
        Dim link As New LinkLabel  
        With link  
          .Text = text  
          Dim Data As New LinkLabel.Link()  
          Data.LinkData = url  
          .Links.Add(Data)  
          .AutoSize = True  
          .Location = RichTextBox1.GetPositionFromCharIndex(RichTextBox1.TextLength)  
          RichTextBox1.AppendText(vbCrLf)  
        End With  
        AddHandler link.LinkClicked, AddressOf LinkClicked  
        Return link  
      End Function  
      Private Sub LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs)  
        Process.Start(e.Link.LinkData.ToString)  
      End Sub  
    End Class  
    

  2. LesHay 7,126 Reputation points
    2022-10-17T14:45:20.167+00:00

    Hi
    My last attemp to answer your modified question. Here I have opted for a Panel instead of a RTB. Used 500 LinkLabels (just to prove they can indeed serve the purpose). Added a button to redo and fit a new Panel size if Form/Panel resized.

    ' Form1 with Panel1 and  
    ' Button1 (fit to Panel)  
    Option Strict On  
    Option Explicit On  
    Public Class Form1  
      Dim urls As New List(Of String)  
      Dim links As New List(Of LinkLabel)  
      Dim wdth As Integer = 70  
      Dim f As New Font(Font.FontFamily, 15)  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        With Panel1  
          .AutoScroll = True  
          .BackColor = Color.PaleGoldenrod  
        End With  
        For i As Integer = 0 To 500  
          urls.Add("www.google.com")  
      
          ' measure for 3 digits  
          Dim m As Size = TextRenderer.MeasureText("LINK 000", f)  
          If m.Width > wdth Then wdth = m.Width  
        Next  
        MakeEm()  
        ShowEm()  
      End Sub  
      Sub MakeEm()  
        Dim c As Integer = 1  
        For Each s As String In urls  
          Dim link As LinkLabel = MakeLink("LINK" & c.ToString, s)  
          links.Add(link)  
          c += 1  
        Next  
      End Sub  
      Sub ShowEm()  
        Dim x As Integer = 8  
        Dim y As Integer = 8  
        Panel1.Controls.Clear()  
        For i As Integer = 0 To links.Count - 1  
          With links(i)  
            .Location = New Point(x, y)  
            x += links(i).Width + 8  
            If x > Panel1.Width - wdth Then  
              x = 8  
              y += links(i).Height + 8  
            End If  
          End With  
          Panel1.Controls.Add(links(i))  
        Next  
      End Sub  
      Function MakeLink(text As String, url As String) As LinkLabel  
        Static x As Integer = 8  
        Static y As Integer = 8  
        Static w As Integer = 0  
        Dim link As New LinkLabel  
        With link  
          .Text = text  
          .Font = f  
          .AutoSize = False  
          .Width = wdth  
          Dim Data As New LinkLabel.Link()  
          Data.LinkData = url  
          .Links.Add(Data)  
        End With  
        AddHandler link.LinkClicked, AddressOf LinkClicked  
        Return link  
      End Function  
      Private Sub LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs)  
        Process.Start(e.Link.LinkData.ToString)  
      End Sub  
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        Panel1.Controls.Clear()  
        ShowEm()  
      End Sub  
    End Class