Print Listboxes

p_nivalis

Active member
Joined
Oct 1, 2004
Messages
33
Programming Experience
5-10
Hello,
I was wondering how to print a list box that is a form in VB.NET, and I mean physically to a printer. This is for a school project and any help would be greatly appreciated.
Thanx
 
I just completed a similar one:

VB.NET:
Private Sub mnuReportsCars_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReportsCars.Click
		' Create a report containing each car and its mileage
		Try
			Me.PrintPreviewDialog1.Document = Me.CarPrintDocument
			Me.PrintPreviewDialog1.ShowDialog()
		Catch es As Exception
			MessageBox.Show(es.Message)
		End Try
	End Sub

	Private Sub CarPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles CarPrintDocument.PrintPage
		Dim font_style As FontStyle = FontStyle.Bold Or FontStyle.Italic
		Dim fontHeader As New Font("Times New Roman", 20, font_style)
		Dim fontText As New Font("Arial", 12, FontStyle.Regular)
		Dim y As Single = 10
		Dim gr As Graphics = Me.CreateGraphics
		Dim myItem As String
		Dim myString As String

		' Print the Header
		e.Graphics.DrawString("Car Mileage Report", fontHeader, Brushes.Black, 300, y)
		y += fontHeader.GetHeight(gr) + fontText.GetHeight(gr)

		' Get the items in the Comboboxes
		For Each carItem In cboCars.Items
			carString &= carItem & ","
		Next

		Dim carArray() As String = Split(carString, ",")
		'Print the Items
		For x As Integer = 0 To carArray.Length - 2  ' It is 2 because the way it is built there is always a comma at the end with no further data
		    e.Graphics.DrawString(carArray(x).ToString , fontText, Brushes.Black, 300, y)
			y += fontText.GetHeight(gr)
		Next
	End Sub
 
If you want to print all items in a list box add a PrintPreview Control and A PrintDocument Control to your form, and modify my sample code from ComboBoxes to your listboxes. The same code should pretty much work.
 
Back
Top