6 Replies - 304 Views - Last Post: 21 November 2017 - 09:33 PM Rate Topic: ***-- 2 Votes

#1 james27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-May 12

paste list data object return null value

Posted 18 November 2017 - 06:08 PM

hello i created List of Dataobject to store multiple clipboard item then i iterated paste process through List but it paste null value.
any help much appreciate

Dim clipList as new List(Of DataObject)
        Dim clip As Object

    Public Sub AddHtmlImageToClipboard(image As Drawing.Image, body As String)
        Const sContextStart As String = "<HTML><BODY><!--StartFragment -->"
        Const sContextEnd As String = "<!--EndFragment --></BODY></HTML>"
        Const m_sDescription As String = "Version:1.0" & vbCrLf + "StartHTML:aaaaaaaaaa" + vbCrLf + "EndHTML:bbbbbbbbbb" + vbCrLf + "StartFragment:cccccccccc" + vbCrLf + "EndFragment:dddddddddd" + vbCrLf
        
        Dim imgB64 As String = ImageToBase64(image)
        '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAAAyCAYAAADhna1TAAAAAXNSR0IA" />
        'Dim sHtmlFragment As String = "<A HREF=" & Chr(34) & link & Chr(34) & ">" & description & "</A>"
        Dim sHtmlFragment As String = ""
        sHtmlFragment &= "<p>"
        sHtmlFragment &= "<img src=""data:image/png;base64," & imgB64 & """ />"
        sHtmlFragment &= "</p>"
        sHtmlFragment &= Environment.NewLine & "</p>"
        
        sHtmlFragment &= "<p>" & body & Environment.NewLine & "</p>"
        Dim sData As String = Convert.ToString(Convert.ToString(m_sDescription & sContextStart) & sHtmlFragment) & sContextEnd
        sData = sData.Replace("aaaaaaaaaa", m_sDescription.Length.ToString().PadLeft(10, "0"c))
        sData = sData.Replace("bbbbbbbbbb", sData.Length.ToString().PadLeft(10, "0"c))
        sData = sData.Replace("cccccccccc", (m_sDescription & sContextStart).Length.ToString().PadLeft(10, "0"c))
        sData = sData.Replace("dddddddddd", (Convert.ToString(m_sDescription & sContextStart) & sHtmlFragment).Length.ToString().PadLeft(10, "0"c))
        'sData.Dump()
        Clipboard.SetDataObject(New DataObject(DataFormats.Html, sData), True)
        
        clip = Clipboard.GetDataObject()
        clipList.Add(clip)
    End Sub
				
				
    
    For Each xx As DataObject In clipList
        Clipboard.SetDataObject(xx)
        SendKeys.Send("^v")
    
    
    Next


Is This A Good Question/Topic? 0
  • +

Replies To: paste list data object return null value

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13666
  • View blog
  • Posts: 54,541
  • Joined: 12-June 08

Re: paste list data object return null value

Posted 18 November 2017 - 06:13 PM

I am not following. What is null where?
Was This Post Helpful? 0
  • +
  • -

#3 james27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-May 12

Re: paste list data object return null value

Posted 18 November 2017 - 06:15 PM

.when here paste it paste nothing..
    For Each xx As DataObject In clipList
        Clipboard.SetDataObject(xx)
        SendKeys.Send("^v")
    Next
    

Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1465
  • View blog
  • Posts: 3,715
  • Joined: 01-February 13

Re: paste list data object return null value

Posted 19 November 2017 - 05:55 PM

I wont lie and say i studied the format of your results, nor did i check the indexes of the Start/End of the Html and Fragments. However, i have this short example that seems to work good on my end. I added a few comments in it that may help a little.

This demonstrates using the StringBuilder Class which is mutable unlike the String Class which is immutable. Each time you concatenate one string to another or replace parts of a string as you are doing many times throughout your code, a new String is created in the memory. A StringBuilder is mutable and does not do that. Therefore, with a StringBuilder you use less memory, it is much quicker, and in my honest opinion it helps make the code cleaner and more readable.

You can test and experiment with the example by creating a new form project with 2 Buttons and 1 RichTextBox added to the form.
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RichTextBox1.WordWrap = False 'just so you can see the actual format without word wrapping
        RichTextBox1.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'Pass an Image and the body text to the CreateHtmlImageDataObject function. It will return a DataObject that you can add to a List(Of DataObject) or directly to the Clipboard.
        Dim HtmlDataObject As DataObject = CreateHtmlImageDataObject(Image.FromFile("C:\TestFolder\AngryBird.png"), "You Angry Bird You !!!")

        Clipboard.SetDataObject(HtmlDataObject) 'sets the DataObject to the Clipboard

        MessageBox.Show("You can now paste it into a forum which has an editor that supports pasting html directly into it by using (Ctrl+V). The Right Click menu may not have (Paste) enabled but, Ctrl+V works if the editor supports it.")
    End Sub

    Private Function CreateHtmlImageDataObject(img As Image, bodytext As String) As DataObject
        Dim HtmlBuilder As New System.Text.StringBuilder
        With HtmlBuilder
            .AppendLine("Version:0.9")
            .AppendLine("StartHTML:sssshtml")
            .AppendLine("EndHTML:eeeehtml")
            .AppendLine("StartFragment:ssssfrag")
            .AppendLine("EndFragment:eeeefrag")

            .AppendLine("<html>")
            .Replace("sssshtml", .Length.ToString.PadLeft(8, "0"c))

            .AppendLine("<body>")

            .AppendLine("<!--StartFragment-->")
            .Replace("ssssfrag", .Length.ToString.PadLeft(8, "0"c))

            .AppendLine("<img src=" & Chr(34) & "data:image/png;base64, " & ImageToBase64(img) & Chr(34) & "/>") 'add the base64 string of the image in the image source tag
            .AppendLine("<p>" & bodytext & "</p>") 'add the body text in between the Paragraph tags

            .AppendLine("<!--EndFragment-->")
            .Replace("eeeefrag", .Length.ToString.PadLeft(8, "0"c))

            .AppendLine("</body>")

            .AppendLine("</html>")
            .Replace("eeeehtml", .Length.ToString.PadLeft(8, "0"c))
        End With

        Return New DataObject(DataFormats.Html, HtmlBuilder.ToString)
    End Function

    Private Function ImageToBase64(img As Image) As String
        Dim s64 As String = ""
        Using ms As New IO.MemoryStream
            img.Save(ms, Imaging.ImageFormat.Png) 'save the image to the MemoryStream as a Png type image so it matches the png type in the html image source tag
            s64 = Convert.ToBase64String(ms.ToArray) 'make sure you use (ms.ToArray) instead of the (ms.GetBuffer) to get the bytes to convert to a base64 string.
        End Using
        Return s64
    End Function

    'this is so you can inspect the html DataObject contents that you set to the clipboard or one that you have copied from a web page.
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If Clipboard.GetDataObject.GetDataPresent(DataFormats.Html) Then 'Make sure there is a DataObject with the Html format on the Clipboard first
            RichTextBox1.Text = Clipboard.GetData(DataFormats.Html).ToString
        End If
    End Sub
End Class


I tested it on a website witch allowed directly pasting html into a post (first image below) and in a Yahoo email (second image below).
Posted Image
Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 james27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-May 12

Re: paste list data object return null value

Posted 19 November 2017 - 09:40 PM

hi!
thanks for you reply!
actually my code also working for paste function .
what i purpose is i want to store multiple item to clipboard for instance i store 2 images and 2 text sentence and loop through list to paste stored 2 images items to pate html page but when i loop through paste it null value pasted .
that is my problem .
but your stringbuilder method is more clean and fast!


For Each xx As DataObject In clipList
    Clipboard.SetDataObject(xx)
    SendKeys.Send("^v")
Next


Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1465
  • View blog
  • Posts: 3,715
  • Joined: 01-February 13

Re: paste list data object return null value

Posted 20 November 2017 - 06:03 AM

Well, after testing this with several DataObjects added to a List in my prior example, it appears that using a loop as you are doing does not give enough time in-between setting each DataObject to the Clipboard and pasting it. I even tried sticking a dreaded Thread.Sleep call in the loop with no luck.

However, using a Timer to paste each DataObject every few hundred milliseconds seems to work good. I have modified my prior example by adding 1 more Button (Button3), 1 Timer, and the code below which fixed the problem for me...
    Private HtmlClipboardDataObjects As New List(Of DataObject)
    Private PasteIndex As Integer = 0

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        PasteIndex = 0
        Timer1.Interval = 5000 'gives you 5 seconds to set focus to where you want them pasted
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'when the timer is started the initial interval will be 5000ms (5 seconds). We want it set to paste one DataObject every 250ms (.25 seconds) after the initial 5000ms wait
        'you may need to adjust (250ms) up to be slower or down to be faster depending on the size of the images and paragraphs you have in the html DataObjects.
        If Timer1.Interval > 250 Then Timer1.Interval = 250

        Clipboard.SetDataObject(HtmlClipboardDataObjects(PasteIndex)) 'set the DataObject at (PasteIndex) to the clipboard
        SendKeys.SendWait("^{v}") 'simulate Ctrl+V keys

        PasteIndex += 1 'add 1 to the PasteIndex
        If PasteIndex = HtmlClipboardDataObjects.Count Then Timer1.Stop() 'if the last one was pasted already, then stop the timer
    End Sub


Was This Post Helpful? 1
  • +
  • -

#7 james27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 14-May 12

Re: paste list data object return null value

Posted 21 November 2017 - 09:33 PM

Hi..
you saved my life! ^^
i really appreciate! thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1