dcsimg
Results 1 to 7 of 7
  1. #1

    Thread Starter
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    Resolved [RESOLVED] Add Icon to Richtextbox

    Hi,

    I have no problem adding an Icon to a specific place
    between a text.

    I just can't figure out how to ..
    click on that Icon to open a diffrent Form

    here my code placing the Icon in a Text..
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            RichTextBox1.Text = "This is your Helpfile click -> *) to show ....some more text..."
        End Sub
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Clipboard.Clear()
            ' Icon 24x24 in Imagelist
            Clipboard.SetImage(ImageList1.Images(0))
    
            With RichTextBox1
                Dim SearchFor As String = "*)"
                Dim i As Long = .Find(SearchFor)
                .SelectionStart = i
                .SelectionLength = SearchFor.Length
                .Paste()
            End With
        End Sub


    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  2. #2
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    453

    Re: Add Icon to Richtextbox

    If you use a PictureBox, you can use the PB's click event.

    vb Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.  
    3.     Dim IconPosition As Point = Nothing
    4.  
    5.     With RichTextBox1
    6.         Dim SearchFor As String = "*)"
    7.         Dim i As Integer = .Find(SearchFor)
    8.         .SelectionStart = i
    9.         .SelectionLength = SearchFor.Length
    10.  
    11.         IconPosition = .GetPositionFromCharIndex(i)
    12.     End With
    13.  
    14.     Dim p As New PictureBox With {
    15.         .Name = "HelpIcon",
    16.         .Cursor = Cursors.Default,
    17.         .BackgroundImageLayout = ImageLayout.Center,
    18.         .BackgroundImage = My.Resources.Image1,
    19.         .Size = New Size(16, 16),
    20.         .Location = IconPosition
    21.     }
    22.  
    23.     AddHandler p.Click, AddressOf IconClicked
    24.  
    25.     RichTextBox1.Controls.Add(p)
    26.  
    27.     End Sub
    28.  
    29. Private Sub IconClicked(sender As Object, e As EventArgs)
    30.     MessageBox.Show($"{DirectCast(sender, PictureBox).Name} clicked.")
    31.  
    32. End Sub

    Name:  rtx.png
Views: 38
Size:  10.0 KB
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  3. #3

    Thread Starter
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    Re: Add Icon to Richtextbox

    Hi Arve,

    thanks

    would this work with more than one Icon inside the Text ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    453

    Re: Add Icon to Richtextbox

    Yes, quick and dirty using you current logic, you could do it like this:

    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    3.         RichTextBox1.Text = "This is your Helpfile click -> *)    to show ....some more text... Test ^)"
    4.  
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.  
    9.         Dim IconHelpPosition As Point = Nothing
    10.         Dim IconTestPosition As Point = Nothing
    11.  
    12.         With RichTextBox1
    13.             Dim SearchFor As String = "*)"
    14.             Dim i As Integer = .Find(SearchFor)
    15.             .SelectionStart = i
    16.             .SelectionLength = SearchFor.Length
    17.  
    18.             IconHelpPosition = .GetPositionFromCharIndex(i)
    19.         End With
    20.  
    21.         With RichTextBox1
    22.             Dim SearchFor As String = "^)"
    23.             Dim i As Integer = .Find(SearchFor)
    24.             .SelectionStart = i
    25.             .SelectionLength = SearchFor.Length
    26.  
    27.             IconTestPosition = .GetPositionFromCharIndex(i)
    28.         End With
    29.  
    30.         Dim HelpIcon As New PictureBox With {
    31.             .Name = "HelpIcon",
    32.             .Cursor = Cursors.Default,
    33.             .BackgroundImageLayout = ImageLayout.Center,
    34.             .BackgroundImage = My.Resources.Image1,
    35.             .Size = New Size(16, 16),
    36.             .Location = IconHelpPosition
    37.         }
    38.  
    39.         Dim TestIcon As New PictureBox With {
    40.             .Name = "TestIcon",
    41.             .Cursor = Cursors.Default,
    42.             .BackgroundImageLayout = ImageLayout.Center,
    43.             .BackgroundImage = My.Resources.Image2,
    44.             .Size = New Size(16, 16),
    45.             .Location = IconTestPosition
    46.         }
    47.  
    48.         AddHandler HelpIcon.Click, AddressOf IconClicked
    49.         AddHandler TestIcon.Click, AddressOf IconClicked
    50.  
    51.         RichTextBox1.Controls.AddRange({HelpIcon, TestIcon})
    52.  
    53.     End Sub
    54.  
    55.     Private Sub IconClicked(sender As Object, e As EventArgs)
    56.         MessageBox.Show($"{DirectCast(sender, PictureBox).Name} clicked.")
    57.  
    58.     End Sub
    59.  
    60. End Class


    Name:  rtx2.png
Views: 36
Size:  8.9 KB
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  5. #5

    Thread Starter
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    Re: Add Icon to Richtextbox

    Hi

    Code:
        RichTextBox1.Controls.AddRange({HelpIcon1, HelpIcon2})
    one fault is...
    these are marked {

    I use VS2008

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6
    Hyperactive Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    453

    Re: Add Icon to Richtextbox

    RichTextBox1.Controls.Add(HelpIcon1)
    RichTextBox1.Controls.Add(HelpIcon2)
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  7. #7

    Thread Starter
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,063

    Re: Add Icon to Richtextbox

    Hi

    just found that out myself, thanks for you help

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
By using this site, you agree to the Privacy Policy