Printing from a Listview in Visual Basic 2008

tombhunter

New Member
Joined
Mar 29, 2009
Messages
1
Hi, I am trying to print the items which I have entered into a Listview box. At the moment all the data entered appears in the text box and all the calculations are completed successfully. However when i try to print the data in the listview it comes up with the title and the date just as I want it, but then underneath the title where the numbers and collum headers should be it just has:

System.Windows.Forms.ListView+ListViewItemCollection

It has this listed one under the other 6 times, it then stops and has a gap before listing,

Monthly Cost: 0
Monthly Cost Over a Year: 6
Monthly Cost over 2 Years : 6

I believe that the problem is around the line just after the beginning of the loop where it says, "SingleMonthlyCost = lstExpenses.Items.ToString()"

The code for my whole printing function is as follows, this consists of a module as well:

Code:
<TABLE style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; MARGIN: 2px 0px; WIDTH: 99%; BORDER-BOTTOM: #eee 0px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #fff; BORDER-RIGHT-WIDTH: 0px" cellSpacing=0 cellPadding=0><TBODY><TR><TD>Private PrintPageSettings As New PageSetupDialog  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">    Private StringtoPrint As String  </TD></TR><TR><TD>    Private PrintFont As New Font("Courier New", 12)  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">    Dim Print As System.Array  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7"> Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click  </TD></TR><TR><TD>        If [COLOR=red]PrintDialog1.ShowDialog[/COLOR] = [COLOR=blue]Windows[/COLOR].Forms.DialogResult.OK Then  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            PrintDocument1.Print()  </TD></TR><TR><TD>        End If  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">    End Sub  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">    Private Sub PrintDocument1_PrintPage(ByVal Sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage  </TD></TR><TR><TD>        Static MonthlyCost, MonthlyCostYear, MonthlyCostTwoYears As Short  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Static ExpensesPrinted As Short  </TD></TR><TR><TD>        Dim LinesPrinted As Short  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Dim LinesRequired As Short  </TD></TR><TR><TD>        Dim FontHeight As Short  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Dim Titles, SingleMonthlyCost As String  </TD></TR><TR><TD>        Dim MyFont As New Font("Courier New", 12, FontStyle.Bold)  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Dim MyTitleFont As New Font("Courier New", 14, FontStyle.Bold)  </TD></TR><TR><TD>        Dim X, Y As Short  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Dim FooterPrinted As Boolean  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        [COLOR=red]FontHeight[/COLOR] = [COLOR=blue]MyFont[/COLOR].GetHeight(e.Graphics)  </TD></TR><TR><TD>        [COLOR=red]LinesRequired[/COLOR] = e.MarginBounds.Height / FontHeight  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        [COLOR=red]X[/COLOR] = [COLOR=blue]50[/COLOR] </TD></TR><TR><TD>        [COLOR=red]Y[/COLOR] = [COLOR=blue]50[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        e.Graphics.DrawString("List of Current Prospect Customers Expenses " & Format(Today, "long date"), MyTitleFont, Brushes.Black, X, Y)  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        [COLOR=red]LinesPrinted[/COLOR] = [COLOR=blue]2[/COLOR] </TD></TR><TR><TD>        [COLOR=red]Y[/COLOR][COLOR=blue]Y[/COLOR] = Y + FontHeight * 2  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Do While (LinesPrinted [COLOR=blue]<[/COLOR] [COLOR=black][B]LinesRequired[/B][/COLOR]) And (ExpensesPrinted [COLOR=blue]<[/COLOR]= lstExpenses.Items.Count - 1)  </TD></TR><TR><TD>            [COLOR=red]SingleMonthlyCost[/COLOR] = [COLOR=blue]lstExpenses[/COLOR].Items.ToString()  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            e.Graphics.DrawString(SingleMonthlyCost, Font, Brushes.Black, X, Y)  </TD></TR><TR><TD>            [COLOR=red]LinesPrinted[/COLOR][COLOR=blue]LinesPrinted[/COLOR] = LinesPrinted + 1  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            [COLOR=red]ExpensesPrinted[/COLOR][COLOR=blue]ExpensesPrinted[/COLOR] = ExpensesPrinted + 1  </TD></TR><TR><TD>            If SingleMonthlyCost.IndexOf("Monthly Cost") [COLOR=blue]<[/COLOR]> -1 Then  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">                [COLOR=red]MonthlyCost[/COLOR][COLOR=blue]MonthlyCost[/COLOR] = MonthlyCost + 1  </TD></TR><TR><TD>            Else  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">                [COLOR=red]MonthlyCostYear[/COLOR][COLOR=blue]MonthlyCostYear[/COLOR] = MonthlyCostYear + 1  </TD></TR><TR><TD>                [COLOR=red]MonthlyCostTwoYears[/COLOR][COLOR=blue]MonthlyCostTwoYears[/COLOR] = MonthlyCostTwoYears + 1  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            End If  </TD></TR><TR><TD>            [COLOR=red]Y[/COLOR][COLOR=blue]Y[/COLOR] = Y + FontHeight  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Loop  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        If [COLOR=red]ExpensesPrinted[/COLOR] = [COLOR=blue]lstExpenses[/COLOR].Items.Count Then  </TD></TR><TR><TD>            Call PrintBlankLine(X, Y, FontHeight, Font, e)  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            e.Graphics.DrawString("Monthly Cost: " & MonthlyCost, Font, Brushes.Black, X, Y)  </TD></TR><TR><TD>            [COLOR=red]Y[/COLOR][COLOR=blue]Y[/COLOR] = Y + FontHeight  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            e.Graphics.DrawString("Monthly Cost Over a Year: " & MonthlyCostYear, Font, Brushes.Black, X, Y)  </TD></TR><TR><TD>            [COLOR=red]Y[/COLOR][COLOR=blue]Y[/COLOR] = Y + FontHeight  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            e.Graphics.DrawString("Monthly Cost Over 2 Years: " & MonthlyCostTwoYears, Font, Brushes.Black, X, Y)  </TD></TR><TR><TD>            [COLOR=red]FooterPrinted[/COLOR] = [COLOR=blue]True[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        End If  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        If (LinesPrinted [COLOR=blue]<[/COLOR] [COLOR=black][B]LinesRequired[/B][/COLOR]) Or (FooterPrinted) Then  </TD></TR><TR><TD>            [COLOR=red]e.HasMorePages[/COLOR] = [COLOR=blue]False[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            [COLOR=red]ExpensesPrinted[/COLOR] = [COLOR=blue]0[/COLOR] </TD></TR><TR><TD>            [COLOR=red]MonthlyCost[/COLOR] = [COLOR=blue]0[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">            [COLOR=red]MonthlyCostYear[/COLOR] = [COLOR=blue]0[/COLOR] </TD></TR><TR><TD>            [COLOR=red]MonthlyCostTwoYears[/COLOR] = [COLOR=blue]0[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        Else  </TD></TR><TR><TD>            [COLOR=red]e.HasMorePages[/COLOR] = [COLOR=blue]True[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        End If  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">    End Sub  </TD></TR><TR><TD> </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click  </TD></TR><TR><TD>        [COLOR=red]PrintPreviewDialog1.Document[/COLOR] = [COLOR=blue]PrintDocument1[/COLOR] </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        PrintPreviewDialog1.ShowDialog()  </TD></TR><TR><TD>    End Sub  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7"> </TD></TR><TR><TD>Module:   </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7"> </TD></TR><TR><TD>Public Sub PrintBlankLine(ByVal XCoord As Short, ByRef YCoord As Short, ByVal OneLineHeight As Short, ByVal FontToUse As Font, ByVal e As System.Drawing.Printing.PrintPageEventArgs)  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        [COLOR=red]YCoord[/COLOR][COLOR=blue]YCoord[/COLOR] = YCoord + OneLineHeight  </TD></TR><TR><TD>        e.Graphics.DrawString("", FontToUse, Brushes.Black, XCoord, YCoord)  </TD></TR><TR><TD style="BACKGROUND-COLOR: #f7f7f7">        [COLOR=red]YCoord[/COLOR][COLOR=blue]YCoord[/COLOR] = YCoord + OneLineHeight  </TD></TR><TR><TD>    End Sub </TD></TR></TBODY></TABLE>
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top