5 Replies - 31652 Views - Last Post: 04 February 2008 - 07:35 PM Rate Topic: -----

#1 bolson50  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-January 08

VB.Net 2005 print listview contents

Post icon  Posted 31 January 2008 - 04:34 PM

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

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


Is This A Good Question/Topic? 0
  • +

Replies To: VB.Net 2005 print listview contents

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4318
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: VB.Net 2005 print listview contents

Posted 31 January 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.

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...

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



Hope that makes sense to you. Enjoy!

"At DIC we be listview code ninjas!" :snap:

This post has been edited by Martyr2: 31 January 2008 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 bolson50  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-January 08

Re: VB.Net 2005 print listview contents

Posted 31 January 2008 - 08:56 PM

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.

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




View PostMartyr2, on 31 Jan, 2008 - 05:34 PM, said:

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

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...

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



Hope that makes sense to you. Enjoy!

"At DIC we be listview code ninjas!" :snap:

Was This Post Helpful? 0
  • +
  • -

#4 Sothrie  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 41
  • Joined: 03-December 07

Re: VB.Net 2005 print listview contents

Posted 01 February 2008 - 07:52 AM

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:

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!
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4318
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: VB.Net 2005 print listview contents

Posted 01 February 2008 - 01:52 PM

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!

:)
Was This Post Helpful? 1

#6 bolson50  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-January 08

Re: VB.Net 2005 print listview contents

Posted 04 February 2008 - 07:35 PM

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1