Printing items in a listbox

C

Carlo

Hi
I'm trying to print the items in a listbox with the code below but
when I click on print I get
"An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in system.windows.forms.dll

Additional information: Specified argument was out of the range of
valid values"

Firstly, what does the message mean and secondly what am I doing wrong
in the coding below. More importantly how do I fix it?
Thanks
Carlob1


Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage
Dim max, n As Integer
max = lstTally.Items.Count
For n = 1 To max
e.Graphics.DrawString(lstTally.Items(n), New Font("arial",
40, FontStyle.Regular), Brushes.Black, 200, 200)
Next
End Sub

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MenuItem1.Click
PrintDocument1.Print()
txtInput.Focus()
txtInput.SelectAll()
End Sub
 
K

Ken Tucker [MVP]

Hi,

The first item is 0.
For n = 0 To max-1
e.Graphics.DrawString(lstTally.Items(n), New Font("arial",
40, FontStyle.Regular), Brushes.Black, 200, 200)
Next

Ken
------------
 
C

Carlo

Thanks very much Ken
Just one problem - it prints all the items in the list box on top of
one another! How do I get a cariage return between each item when
printing?
Thanks
carlo
 
K

Ken Tucker [MVP]

Hi,

Try something like this.

For n = 0 To max-1
e.Graphics.DrawString(lstTally.Items(n), New Font("arial", 40,
FontStyle.Regular), Brushes.Black, 200, 200 + (n * 45))
Next


Ken
 
H

Herfried K. Wagner [MVP]

* "Ken Tucker said:
Try something like this.

For n = 0 To max-1
e.Graphics.DrawString(lstTally.Items(n), New Font("arial", 40,
FontStyle.Regular), Brushes.Black, 200, 200 + (n * 45))
Next

.... I would not create a new 'Font' object in every iteration. Instead,
create the font prior to the loop and always use the same font object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top