someone who can help me please
I have a code for a listview printing
but the problem is that he don't print the icons.
And this I never have to test until now
I can add anything like a color and other stuff but I do not know how to solved this.
please anyone?
Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Headings
currentY = 100
For Each c In lstView.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, f, b, lr)
l += c.Width
idx += 1
Next
currentY += maxY + gap
g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = lstView.Items.Count - 1
l = 0
maxY = 0
idx = 0
For Each lvsi In lstView.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
idx += 1
Next
currentY += maxY + gap
Next
If you want to draw an Icon you will call the DrawIcon method of the Graphics object. You call it right before calling DrawString to draw the text, then move the text the appropriate amount to the right.
Then the first thing you should do is read the documentation for that method. As is more often the case than not, the documentation contains a code example. If you can't get it to work after that then post back but you should always look first, ask questions later.
Actually, given that the ImageList contains Image objects rather than icon objects, you'd call DrawImage rather than DrawIcon. Not the same but very similar. Sorry about the bum steer. You will use the item's ImageIndex to get the appropriate Image from the ImageList and pass it to DrawImage.
AddHandler listView1.DrawSubItem, AddressOf Me.listView1_DrawSubItem
Dim DrawSubItem As The
Private Event : As handler
Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs)
'specify the lisview column
If (e.ColumnIndex = 1) Then
'draw the subitem with the same background color.
e.DrawBackground
'set the image from the imagelist associated with the ListViewItem
e.Graphics.DrawImage(e.Item.ImageList.Images(e.Item.ImageIndex), e.SubItem.Bounds.Location)
End If
End Sub
It is going to be easy. It's a simple If statement to determine which index to use, get the Image and pass it to DrawImage, which will be much like DrawString. Give it a go and post back what you've done if you get stuck, so we can help you fix it.
Private Sub listView1_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs)
'specify the lisview column
If (e.ColumnIndex = 1) Then
'draw the subitem with the same background color.
e.DrawBackground()
'set the image from the imagelist associated with the ListViewItem
e.Graphics.DrawImage(e.Item.ImageList.Images(e.Item.ImageIndex), e.SubItem.Bounds.Location)
End If
End Sub
code for printing listview:
Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
AddHandler LstView.DrawSubItem, AddressOf Me.listView1_DrawSubItem
'Headings
currentY = 100
For Each c In lstView.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, f, b, lr)
l += c.Width
idx += 1
Next
currentY += maxY + gap
g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = lstView.Items.Count - 1
l = 0
maxY = 0
idx = 0
For Each lvsi In lstView.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
idx += 1
Next
currentY += maxY + gap
Next
What's the point here? It's to print the icon with each item, correct? So shouldn't the code to draw the images be in the PrintPage event handler? They aren't going to get printed otherwise. I said back in post #2:
You call it right before calling DrawString to draw the text, then move the text the appropriate amount to the right.
yes correct only there are 2 icons to use in the listview.
i have relocate the code
and add it in the printpage
but i get a error
'Object reference not set to an instance of a object.
I don't get it.
i did:
Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Headings
currentY = 100
For Each c In lstView.Columns
maxY = Math.Max(maxY, g.MeasureString(c.Text, f, c.Width).Height)
colLefts(idx) = l
colWidths(idx) = c.Width
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(c.Text, f, b, lr)
l += c.Width
idx += 1
Next
currentY += maxY + gap
g.DrawLine(Pens.Black, 0, currentY, e.PageBounds.Width, currentY)
currentY += gap
'Rows
iCount = lstView.Items.Count - 1
If (re.ColumnIndex = 0) Then
'draw the subitem with the same background color.
re.DrawBackground()
'set the image from the imagelist associated with the ListViewItem
re.Graphics.DrawImage(re.Item.ImageList.Images(re.Item.ImageIndex), re.SubItem.Bounds.Location)
End If
l = 0
maxY = 0
idx = 0
For Each lvsi In lstView.Items(ii).SubItems
maxY = Math.Max(maxY, g.MeasureString(lvsi.Text, f, colWidths(idx)).Height)
lr = New RectangleF(colLefts(idx), currentY, colWidths(idx), maxY)
If lr.Width > 0 Then g.DrawString(lvsi.Text, f, b, lr)
idx += 1
Next
currentY += maxY + gap
Next
If there are two icons to draw then you'll need to call DrawImage twice, but I only see it once in that code. Basically you need to call DrawString for each subitem you want to draw, and call DrawImage before each of those you want an image in front of.
When something doesn't work, take a step back and start simple, then build it up. Start by printing just the first image. Once that works, add the first subitem text. Once that works, add the second image. Once that works... etc.
As for your error, please always point out what line it occurs on. Otherwise we have to spend time trying to work out where the error is before we can even think about working what caused it.
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="Bill_Payments_System"
StackTrace:
at Bill_Payments_System.Form1.PrintDocument11_PrintPage(Object sender, PrintPageEventArgs e) in G:\Drive D\Bill1\Bill1\Form1.vb:line 85
at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at System.Windows.Forms.PrintPreviewControl.ComputePreview()
at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
InnerException:
If (re.ColumnIndex = 0) Then
'draw the subitem with the same background color.
re.DrawBackground()
'set the image from the imagelist associated with the ListViewItem
e.Graphics.DrawImage(re.Item.ImageList.Images(re.Item.ImageIndex), re.SubItem.Bounds.Location)
End If
That doesn't tell us which line of your code throws the exception. We don't know which is line 85. The IDE will highlight the current line when an exception is thrown. You need to let us know which line that is.
That said, I still say you should just comment that code out and start building it up one part at a time. If you try to do too much in one go and something goes wrong, it's difficult to determine what part the issue is in. If you only implement small parts at a time then it's easy to determine where the issue is when one occurs because you know that what went before works.
If (e.ColumnIndex = 1) Then
'draw the subitem with the same background color.
e.DrawBackground()
'set the image from the imagelist associated with the ListViewItem
e.Graphics.DrawImage(e.Item.ImageList.Images(e.Item.ImageIndex), e.SubItem.Bounds.Location)
You don't conjure a DrawListViewSubItemEventArgs yourself. One gets passed to you in the DrawListViewSubItem event handler, but you aren't handling that event because you aren't drawing a ListViewSubItem. You're handling the PrintPage event because you're printing a page.
I'll say this one last time: you need to stop, take a step back and start simple because you've wandered down a road that doesn't work.
i need to load the current icons from the imagelist
and get them right in the listview like before you print it.
it just i can't find anything about this
to print the icon's
i only found the string to print but nothing about the icons