Results 1 to 13 of 13

Thread: [RESOLVED] Paste Image to Rich Tect Box issue

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Resolved [RESOLVED] Paste Image to Rich Tect Box issue

    Hi.
    I'm having some issue with pasting a list of images to the RTB.
    I'm using an list with images, I'm also using the same image object as you can see on the code.
    The problem is that, SOMETIMES, the RTB will skip an image here and there. If i step into a next node (I have a treeview and calling the code on TreeView1.AfterSelect) then it will get all the images. As I can see, the problem will go away if I go back and forth on the treeview nodes. It seems to happen the first time the AfterSelect event hits and , again, not always.
    The code runs synchronous as it's a very fast calculation.
    I've tried to use a thread.sleep in some of the image changes but it's about 30 image changes so even if it works it will slow down the app.
    Any thoughts?
    Maybe the problem is the clipboard not catching up? In that case is there another way to paste images on RTB (besides creating my own clipboard, that is a real pain)?
    Code:
      Dim img As Image
      img = Imagelst(0).imageRTB
      Clipboard.SetImage(img)
      RichTextBox1.Paste()
    RichTextBox1.AppendText(vbCrLf)  
     RichTextBox1.AppendText("something & vbCrLf)
    
    RichTextBox1.AppendText(vbCrLf)
     img = Imagelst(1).imageRTB
      Clipboard.SetImage(img)
      RichTextBox1.Paste()
    RichTextBox1.AppendText(vbCrLf)  
     RichTextBox1.AppendText("somethingelse & vbCrLf)
    
    ...etc
    .

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,336

    Re: Paste Image to Rich Tect Box issue

    A few coding tips:
    1. Dim img As New Image
    2. Instead of Imagelst(0) you could use Imagelst.First
    3. Use System.Environment.NewLine instead of vbcrlf.
    4. Use string interpolation: https://docs.microsoft.com/en-us/dot...olated-strings

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Re: Paste Image to Rich Tect Box issue

    That was not the question .
    Anyhow.

    1)Does not matter, image is instantiated afterwards.
    2)Imagelist can accept a key so 0 can be the key, I could have 1,2,3,"John"...etc. Also .First ,if I am not mistaken, is a lambda so it takes more time on large lists.
    3)It doesn't really affect a richtextbox and it's smaller to write.
    4)Fair enough but I am not doing any equality right now.
    .

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,336

    Re: Paste Image to Rich Tect Box issue

    I know.

    1. Then merge the lines. A variable can be instantiated upon declaration.
    2. Okay, but doesn't that only matter in loops with a lot of iterations? We're probably talking about milliseconds here.
    3. Use an Imports statement.
    4. What does equality have to do with this?
    Last edited by Peter Swinkels; Apr 28th, 2021 at 12:42 PM. Reason: fixed a sentence.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,253

    Re: Paste Image to Rich Tect Box issue

    What is the code in the AfterSelect event? Is it the code that you have in your original post or is it something else?

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Re: Paste Image to Rich Tect Box issue

    First,
    1) I followed your advice and used New on image, now I get Image cannot be instantiated and the VS suggestion is do delete the NEW, can you help?
    Attachment 181211

    2)I want to get the key ("hello") on imagelist. Is there a .Getkey or somethingm or just .First?
    3)I don't wanna

    dday ,let me check....

    OK,
    On afterselect i set a class to the tag and I call a function
    Code:
      Dim DM As DeviceManagerClass = e.Node.Tag
            GetNodeData(DM)
    Then the initial code is like so:

    Code:
        Dim img As Image
            If DM.Operating_System_ServerorPC = "pc" Then
                img = Imagelst(0).imageRTB
            Else
                img = Imagelst(1).imageRTB
            End If
            Clipboard.SetImage(img)
            RichTextBox1.Paste()
            RichTextBox1.AppendText(vbCrLf)
    To be fair I haven't noticed any clipboard image skipping again, so maybe there was something the debugger did back then, as I was using breakpoints, starting, stopping...
    .

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,336

    Re: Paste Image to Rich Tect Box issue

    1. I think you misunderstood me or I wasn't clear:

    Code:
    Dim img As Image = Imagelst(0).imageRTB
    2. I don't know, sounds like you might want to try a dictionary object: https://docs.microsoft.com/en-us/dot...2?view=net-5.0.
    3. Why not?

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Re: Paste Image to Rich Tect Box issue

    I did not misunderstood,
    You wrote:
    Dim img As New Image

    Anyhow.
    Thanks.
    .

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,253

    Re: Paste Image to Rich Tect Box issue

    This smells like a race condition, but I'm not sure how.

    Imagine this was asynchronous for a moment. The race condition would present if it took too long to get the node's tag prior to you calling GetNodeData. But since this appears to be blocking code, I'm not sure what would be the cause.

    Just that what you're describing sounds like a race condition. Perhaps someone else can chime in?

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,336

    Re: Paste Image to Rich Tect Box issue

    Quote Originally Posted by sapator View Post
    I did not misunderstood,
    You wrote:
    Dim img As New Image

    Anyhow.
    Thanks.
    As I said: "or I wasn't clear" - sorry. You're welcome btw.

  11. #11

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Re: Paste Image to Rich Tect Box issue

    As I haven't had a lot of experience with the Clipboard object I preferred to do this synchronous.
    What you write would make a lot of sense indeed if it was async.
    I can even see frictions of code that I commented afterwards , try to to do a sleep of a couple of milliseconds.
    Although, as I've said this haven't happened for quite a while.
    Also this happened on the first node load only.
    Maybe the imagelist did not have time to get the images on _load? As I am adding them manually with an Imagelist.AddRange, but if I am not mistaken, _load fires before _AfterSelect.
    I can also see that the first line on _load is dumping the data on an empty obect: "OClipb = Clipboard.GetDataObject" , maybe i did that and it work and i left it? I'm not sure as it has been a while.
    Eventually this is not such a big deal, even if it happens from times to times but if there is any solution...
    .

  12. #12
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,795

    Re: Paste Image to Rich Tect Box issue

    don't know if this will Help, I place a Image based on a String
    Code:
    With RichTextBox1
                Dim SearchFor As String = "*)"
    so looking for *) wil place the Image from the Imagelist at that position

    here a Image
    Name:  RTBImage.jpg
Views: 35
Size:  14.9 KB

    and the code to try, In my Imagelist I placed two Icons
    Code:
    Option Strict On
    Imports System.Text.RegularExpressions
    
    Public Class Form1
       
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim IconPosition As Point = Nothing
            Dim IconHelpPosition As Point = Nothing
            Dim IconTestPosition As Point = Nothing
    
            With RichTextBox1
                Dim SearchFor As String = "*)"
                Dim i As Integer = .Find(SearchFor)
                .SelectionStart = i
                .SelectionLength = SearchFor.Length
    
                IconPosition = .GetPositionFromCharIndex(i)
            End With
    
            With RichTextBox1
                Dim SearchFor As String = "^)"
                Dim i As Integer = .Find(SearchFor)
                .SelectionStart = i
                .SelectionLength = SearchFor.Length
    
                IconHelpPosition = .GetPositionFromCharIndex(i)
            End With
            Dim HelpIcon1 As New PictureBox With { _
               .Name = "HelpIcon1", _
               .Cursor = Cursors.Default, _
               .BackgroundImageLayout = ImageLayout.Center, _
               .BackgroundImage = ImageList1.Images(0), _
               .Size = New Size(16, 16), _
               .Location = IconPosition}
    
    
            Dim HelpIcon2 As New PictureBox With { _
                .Name = "HelpIcon2", _
                .Cursor = Cursors.Default, _
                .BackgroundImageLayout = ImageLayout.Center, _
                .BackgroundImage = ImageList1.Images(1), _
                .Size = New Size(16, 16), _
                .Location = IconHelpPosition}
    
            AddHandler HelpIcon1.Click, AddressOf IconClicked
            AddHandler HelpIcon2.Click, AddressOf IconClicked
    
            RichTextBox1.Controls.Add(HelpIcon1)
            RichTextBox1.Controls.Add(HelpIcon2)
    
            ' RichTextBox1.Controls.AddRange("HelpIcon1")
    
        End Sub
    
        Private Sub IconClicked(ByVal sender As Object, ByVal e As EventArgs)
            'MessageBox.Show("{DirectCast(sender, PictureBox).Name} clicked.")
            MessageBox.Show(DirectCast(sender, PictureBox).Name & " clicked")
            Form2.Show()
    
        End Sub
    
        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... Test ^)"
            Mark(RichTextBox1, "This", Color.Blue)
            Mark(RichTextBox1, "some", Color.DarkGoldenrod)
    
        End Sub
    
       
        Private Sub Mark(ByVal rtb As RichTextBox, ByVal word As String, ByVal color As Color)
            Dim matchc As MatchCollection = Regex.Matches(rtb.Text.ToLower(), word.ToLower())
            For Each m As Match In matchc
                rtb.Select(m.Index, m.Length)
                rtb.SelectionColor = color
                rtb.Select(rtb.Text.Length, 0)
                rtb.SelectionColor = Drawing.Color.Black
            Next
        End Sub
    
    End Class
    and the result placing the Image
    Name:  RTB2.jpg
Views: 34
Size:  15.4 KB
    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.

  13. #13

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Re: Paste Image to Rich Tect Box issue

    Hmm.
    Yes that, I was trying to avoid PictureBoxes.

    However I will write down the code for future reference, if I need to create something similar again.

    I will close this now as I don't really have an issue.

    Thanks.
    .

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