Click here to Skip to main content
15,741,047 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello. I'm trying to get a list of integers from Excel to my VB application by pulling them from the clipboard. The error is
Conversion from string "908313 <br />
264357 <br />
914466 <br />
280352<br />
" to type 'Integer' is not valid
so it still a string and not a list of integers as I hope for. Does anyone know how to crop the string. My hunch is that (2,"c) is wrong.

What I have tried:

Private Class SortedNumbers
        Public Property RandomNumber As Integer
    End Class

    Private Sub ButMedlemsnrPaste_Click(sender As Object, e As EventArgs) Handles ButMedlemsnrPaste.Click

        Dim myStopWatch As New Stopwatch
        Dim clipBoardNumbers As New List(Of SortedNumbers)

        If My.Computer.Clipboard.ContainsText Then
            clipBoardNumbers = My.Computer.Clipboard.GetText.Split(","c).Select(Function(x) New SortedNumbers With {.RandomNumber = Math.Abs(CInt(x))}).OrderByDescending(Function(x) x.RandomNumber).ToList
            ListMedlemsnr.DataSource = clipBoardNumbers
        End If

    End Sub
Updated 29-Mar-21 5:42am
Richard MacCutchan 29-Mar-21 7:34am    
By the look of the error message the numbers are separated by newline characters. There are no commas in the list. And the debugger should be able to confirm it.
Member 15076984 29-Mar-21 7:38am    
Yes. Do you know what I should write in order to replace the newline characters. I can't find i when i Google it. Thanks
Richard MacCutchan 29-Mar-21 9:00am    
Forget about Google and look at the actual data that is returned from the clipboard.
Member 15076984 29-Mar-21 9:13am    
But the GetText.Split(","c) must expect a comman and it's not in the text, so that's why I must use the newline. But I can't get it to work no matter what I write.
Richard MacCutchan 29-Mar-21 9:40am    
For the second time (sigh) use the debugger to see exactly what characters are in the data.

Try this:
Dim clipNumbers = clipboarddata.Split(New String(){Microsoft.VisualBasic.vbNewLine}, StringSplitOptions.RemoveEmptyEntries) _
    .Select(Function(x) New SortedNumbers With {.RandomNumber =Math.Abs(Convert.ToInt32(x))}) _
    .OrderByDescending(Function(x) x.RandomNumber) _
Share this answer
Member 15076984 29-Mar-21 9:31am    
Hi Maciej. Getting closer but now it posts a line containing "WindowsApplication1.MedlemEmailKampagner+SortedNumbers" for each record.
Maciej Los 29-Mar-21 15:25pm    
Do not use custom class. Use list of integers instead. For eaxmple:
Dim clipNumbers = clipboarddata.Split(New String(){Microsoft.VisualBasic.vbNewLine}, StringSplitOptions.RemoveEmptyEntries) _
    .Select(Function(x) Convert.ToInt32(x)) _
    .OrderByDescending(Function(x) x) _
Member 15076984 30-Mar-21 5:00am    
It worked. Thanks a lot for the effort ;)
Maciej Los 30-Mar-21 5:45am    
You're very welcome.
I would paste the clipboard data to a multiline textbox.
Then you see how the data really look.

Then remove all blank characters and html formatting code from the string(s)
264357 <br />

Format/parse/split the string(s) and convert it to integer.

And the last step would be to paste the converted results (integer) into your listbox/gridview.
Share this answer
Maciej Los 29-Mar-21 15:23pm    
Why multiple textboxes?
Html tags has been added via service. Originall text did NOT contain them. 29-Mar-21 15:35pm    
Not multiple textboxes, only one multiline textbox to see what comes from the clipboard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900