Copy text from a listbox

waughp

Member
Joined
Feb 18, 2005
Messages
15
Programming Experience
Beginner
Hey Experts,

1) I'm trying to copy the items of a ListBox to the clipboard so I can paste it into an excel document. I need the copied text to be in one row (to make it easy to paste into one column of the excel spreadsheet all in sepperate cells). The code I'm trying to use is below. I know a "ListViewItem" will not work with a listbox, but I wonder why "ListBoxItem" wouldn't be an available option? When I try to use "ListBoxItem" it says it's "not defined".

2) Additionally, If somebody knows how to directly export the text from a listbox to one column of an excel spreadsheet (each listbox item in a sepperate cell) that would be extremely helpful also.


Thanks in advance!

Pat

VB.NET:
 Dim s As String 

For Each lsvrow As ListViewItem In ListBox1.SelectedItems

s &= lsvrow.Text & ControlChars.NewLine

Next

Clipboard.SetDataObject(s)
 
VB.NET:
 Dim s As String 
 
s &= ListBoxMAIN.SelectedItem & ControlChars.NewLine
 
Clipboard.SetDataObject(s)

This successfully copies one item in the listbox exactly as I need it, however, I need it to be able to copy all the selected items in the listbox.

Any help is much appreciated!

Thanks,

Pat
 
in the first post above you have declared listviewitem and it is not adequate for listbox items collection. However, at the moment you are getting the only 1st selected item of listbox. Also, i would use listview insted listbox and in your case it should be accomplish like this:
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] s [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]String1
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] lvItem [/size][size=2][color=#0000ff]As[/color][/size][size=2] ListViewItem
 
[/size][size=2][color=#0000ff]For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] lvItem [/size][size=2][color=#0000ff]In[/color][/size][size=2] ListView1.SelectedItems
 
s &= lvItem.SubItems(0).Text & ControlChars.NewLine
 
[/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2]MessageBox.Show(s, "Selected items")
 
[/size]

Actually take a look at the attached project below :)


Cheers ;)
 

Attachments

  • listviewcollection.zip
    22.7 KB · Views: 60
Wow thanks! I like the listview much better and the solution you posted to copy the items works great!

My only problem now is that I'm not sure how to select all the items in the listview. The number of items in the listview will be completely random. Whether there are 2 items or 10,000 items, I want a "Select All" button to select all the items in the box. I've tried searching the internet and this forum with no success, sorry if this has come up before!

Thanks for the help!!

Pat
 
Just add this code from below to the button "Select all"
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] lvitem [/size][size=2][color=#0000ff]As[/color][/size][size=2] ListViewItem

[/size][size=2][color=#0000ff]For[/color][/size][size=2] [/size][size=2][color=#0000ff]Each[/color][/size][size=2] lvitem [/size][size=2][color=#0000ff]In[/color][/size][size=2] ListView1.Items

lvitem.Selected = [/size][size=2][color=#0000ff]True

[/color][/size][size=2][/size][size=2][color=#0000ff]Next[/color][/size]
[size=2][color=#0000ff][/color][/size]

Cheers ;)


 
Back
Top