Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,942 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,424 people online right now. Registration is fast and FREE... Join Now!




VB.Net 2005 print listview contents

 
Reply to this topicStart new topic

VB.Net 2005 print listview contents

bolson50
31 Jan, 2008 - 03:34 PM
Post #1

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 15


My Contributions
i have found a way to print the contents if a textbox but have not been able to find how to do it with listview. i have tryed using listivew1.Items but it does not work. thank you for any ideas or help with this probelm. i have a feeling that there is a simple answer that i am not seeing. thanks again

CODE

Dim canvas As Graphics = e.Graphics
canvas.DrawString(TextBox.Text, _font, _brush, 0, 0)

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: VB.Net 2005 Print Listview Contents
31 Jan, 2008 - 04:34 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Here is an example that will show you how to get at and print the contents of a listview and its subitems.

CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Setup variables of ListViewItem and ListViewSubItem
        Dim lvwItem As ListViewItem
        Dim lvwSubItem As ListViewItem.ListViewSubItem

        ' Counter for display purposes
        Dim listviewcount As Integer = 1

        ' Loop through our listview items
        For Each lvwItem In ListView1.Items

            ' Print the count
            Debug.Print(listviewcount)

            ' Print the subitems of this particular ListViewItem
            For Each lvwSubItem In lvwItem.SubItems
                Debug.Print(lvwSubItem.ToString())
            Next

            ' Increment the count (for display purposes)
            listviewcount += 1
        Next

End Sub


Just read the in code comments and you should see that we loop through the listview items. For each item we then loop through its subitem collection. So if we had two columns in our listview and the first was "test1" and its subitem was "value1" and the second was "test2" with the subitem "value2" we would see this in our debug window...

CODE

1
ListViewSubItem: {test1}
ListViewSubItem: {Value1}
2
ListViewSubItem: {test2}
ListViewSubItem: {Value2}


Hope that makes sense to you. Enjoy!

"At DIC we be listview code ninjas!" decap.gif

This post has been edited by Martyr2: 31 Jan, 2008 - 04:35 PM
User is offlineProfile CardPM
+Quote Post

bolson50
RE: VB.Net 2005 Print Listview Contents
31 Jan, 2008 - 07:56 PM
Post #3

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 15


My Contributions
hi Martyr2
yes this does make sence and work great to print to Debug.Print but i would like this to paper as did the code i send was the part that takes the information in the textbox1 and prints it to paper. i am trying to do the same think but instead of getting the info from textbox1 i want to get it from listview1.
i am sorry if i was not clear about that in my first post.

CODE

Dim canvas As Graphics = e.Graphics
canvas.DrawString(TextBox1.Text, _font, _brush, 0, 0)



QUOTE(Martyr2 @ 31 Jan, 2008 - 05:34 PM) *

Here is an example that will show you how to get at and print the contents of a listview and its subitems.

CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Setup variables of ListViewItem and ListViewSubItem
        Dim lvwItem As ListViewItem
        Dim lvwSubItem As ListViewItem.ListViewSubItem

        ' Counter for display purposes
        Dim listviewcount As Integer = 1

        ' Loop through our listview items
        For Each lvwItem In ListView1.Items

            ' Print the count
            Debug.Print(listviewcount)

            ' Print the subitems of this particular ListViewItem
            For Each lvwSubItem In lvwItem.SubItems
                Debug.Print(lvwSubItem.ToString())
            Next

            ' Increment the count (for display purposes)
            listviewcount += 1
        Next

End Sub


Just read the in code comments and you should see that we loop through the listview items. For each item we then loop through its subitem collection. So if we had two columns in our listview and the first was "test1" and its subitem was "value1" and the second was "test2" with the subitem "value2" we would see this in our debug window...

CODE

1
ListViewSubItem: {test1}
ListViewSubItem: {Value1}
2
ListViewSubItem: {test2}
ListViewSubItem: {Value2}


Hope that makes sense to you. Enjoy!

"At DIC we be listview code ninjas!" decap.gif


User is offlineProfile CardPM
+Quote Post

Sothrie
RE: VB.Net 2005 Print Listview Contents
1 Feb, 2008 - 06:52 AM
Post #4

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
You can combine the two segments of code!
All you really need is your code rerun for each item in the listbox. So if we put the two pieces of code together, adding an offsetting variable so all of your words don't draw on top of each other, we'd get something like this:

CODE

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Setup variables of ListViewItem and ListViewSubItem
        Dim lvwItem As ListViewItem
        Dim lvwSubItem As ListViewItem.ListViewSubItem
        Dim offsettingCount as Integer = 0
        Dim canvas As Graphics = e.Graphics

        ' Loop through our listview items
        For Each lvwItem In ListView1.Items

            ' Print the subitems of this particular ListViewItem
            For Each lvwSubItem In lvwItem.SubItems
                canvas.DrawString(lvwSubItem.ToString(), _font, _brush, 0, offsettingCount)
            Next

            ' Increment the count (for display purposes)
            offsettingCount += 10
        Next

End Sub


So, haven't tried it, but something like that should work.

Hope it helps!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: VB.Net 2005 Print Listview Contents
1 Feb, 2008 - 12:52 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Sothrie is going in the direction I was expecting you to go in Bolson. I was showing you how to loop through the listview, in combination with any paper printing you had done before, you can print the content of the listview onto the paper. Sothrie is on the right track but you may actually need to create the graphics object using Me.CreateGraphics() depending on where you use it (like in a button).

Now printing to paper of course in VB.NET can be a bit more tricky than it was in previous versions of VB. So you might want to check out printing ability and how you may need to set margins, line height etc to get it to the way you want on paper. The article below shows you some of the printing abilities and code that can be employed for your purpose.

Printing in VB.NET

Of course there are many other sites that show you how to do this as well. Just type into Google "VB.NET printing" to see them.

Enjoy!

smile.gif
User is offlineProfile CardPM
+Quote Post

bolson50
RE: VB.Net 2005 Print Listview Contents
4 Feb, 2008 - 06:35 PM
Post #6

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 15


My Contributions
thnks both of you i was trting to add both bits of code but i could not see how to change

canvas.DrawString(TextBox.Text, _font, _brush, 0, 0)

to

canvas.DrawString(lvwSubItem.ToString(), _font, _brush, 0, offsettingCount)

that was missing me up more i think
thanks again
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month