When debugging a new program, I like to add log style messages to a listbox on a form as a very easy way for me to see what is happening with the program and verify if the proper actions are occurring. Recently I had a program that generated a LOT of messages and I wanted to copy the text contents of that listbox to the clipboard with a button so that I paste the text into WordPad and have a hardcopy record of the program's execution log. As with many things in VB.Net, it turned out to be very easy to do.
In case you have a similar need in the future, the code is listed below for your use. I hope you find it helpful.
Joe Kunk
'Copy contents of a string Listbox to the clipboard
Dim strList(Me.lbStatus.Items.Count) As String
Me.lbStatus.Items.CopyTo(strList, 0)
Dim strClip As String = String.Join(vbCrLf, strList)
Clipboard.SetDataObject(strClip)
5 comments:
thank you very much for this, been looking for a while for this, why they didnt include copy into listbox ill never know lol, this code was very easy for beggginer to understand and edit to my needs, thank you.
I am using M/S Visual Studio 2005. The compiler does not accept
Clipboard.
The error associated with this is:
Clipboard. not declared.
I have tried several other ways to copt text from listbox to Clipboard, however tey have also failed.
Please let me know if you are aware of another solution.
If you require to keep a hard copy of your log messages why not store these messages in the database that you are using as well as displaying them? Or with a few lines output the messages to a text file?
Very useful post!
Thank you very much.
Doesn't work with asp.net
Doesn't recognise 'Clipboard'
Post a Comment