Question Paste Items into Listbox

david84

Member
Joined
May 6, 2010
Messages
20
Programming Experience
Beginner
I'm using the following to add a list from a txt into a listbox, which works perfectly.

VB.NET:
Dim OFD As New OpenFileDialog
            OFD.Filter = "Text Documents (*.txt)|*.txt"
            OFD.Title = "Open List"
            OFD.ShowDialog()
            ListBox1.Items.AddRange(IO.File.ReadAllLines(OFD.FileName))

The similar thing below does not work to load sites from the clipboard. What else could I use.

ListBox1.Items.AddRange(IO.File.ReadAllLines(Clipboard.GetText))
 
What exactly is on the clipboard? Is it text for a start? If so, is it the name of a file? If not, File.ReadAllLines is not going to help because it reads all the lines from a file.
 
it will websites e.g
http://www.website1.com
http://www.website2.com
http://www.website3.com
http://www.website4.com
http://www.website5.com
each one needs to be a seperate item in the listbox.
 
So, the clipboard contains a single string with multiple lines? A string array? Something else? You need to be completely clear.

Also, your profile says that you are using .NET 1.0. That would mean that you are using the original version of VB.NET from 2002. Is that actually correct? If not then you should update your profile.
 
The clipboard will contain the following:
1
2
3
4
5
Copy them like that, one per line.
And when they're pasted, each must be a different item in the listbox.
So like:
1
2
3
4
5
 
If that is so you can split the string into multiple strings using one of these methods: String.Split Method (System)
VB.NET:
Dim lines() As String = Clipboard.GetText.Split(vbNewLine.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
A new line if often indicated by two chars (vbCr + vbLf = vbNewline), so the above example would split the string containing the sample 1-5 lines into nine strings and remove the empty entries between the carriage return + line feed chars. Whenever any one of the separator chars occur a new string is started.

This example is using a different overload, it splits by the exact string sequence of vbNewLine and does not discard empty lines:
VB.NET:
Dim lines() As String = Clipboard.GetText.Split(New String() {vbNewLine}, StringSplitOptions.None)
The result of copying your sample 1-5 lines to clipboard and calling either of these is the same.
 
Back
Top