473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Printing a list box

Please could someone tell me what is wrong.
Ther error is:
An unhandled exception of type 'System.NullReferenceException' occurred in
microsoft.visualbasic.dll

Additional information: Object variable or With block variable not set.

I am trying to print the contents of a list box on another form.

This is the sub, and the highlighted line is where it is falling over.
Private Sub PrintDoco_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoco.PrintPage

'm_CurrentPage += 1

Dim max As Integer
Dim n As Integer
max = lstData.count *****************************************
MsgBox(max, , "Test")
For n = 0 To max - 1
e.Graphics.DrawString(lstData.item.count(), New Font("arial",
12, FontStyle.Regular), Brushes.Black, 200, 200 + n * 45)
Next

e.HasMorePages = False

End Sub

This is the code for both forms :

Imports System.Management
Imports System.Drawing.Printing
Imports System
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing

Public Class Print
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private WithEvents PrintDoco As New PrintDocument
Private m_CurrentPage As Integer

Private Sub Print_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

With Me.cboPrinters
For Each s As String In PrinterSettings.InstalledPrinters
.Items.Add(s)
Next s
If .Items.Count > 0 Then
.SelectedIndex = 0
Else
MessageBox.Show("No printers installed, quitting!",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Me.Close()
End If
End With
'PrintDoco.DocumentName = "Unknown"

AddHandler PrintDoco.PrintPage, AddressOf PrintDoco_PrintPage
m_CurrentPage = 0
End Sub

Private Sub cboPrinters_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboPrinters.SelectedIndexChanged
If Me.cboPrinters.SelectedIndex <> -1 Then
PrintDoco.PrinterSettings.PrinterName = Me.cboPrinters.Text
End If
End Sub

Private Sub PrintDoco_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoco.PrintPage

'm_CurrentPage += 1

Dim max As Integer
Dim n As Integer
max = lstData.count
MsgBox(max, , "Test")
For n = 0 To max - 1
e.Graphics.DrawString(lstData.item.count(), New Font("arial",
12, FontStyle.Regular), Brushes.Black, 200, 200 + n * 45)
Next

e.HasMorePages = False

End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click

PrintDoco.Print()
'lstData.Focus()
'lstData.SelectAll()
'lstData.Show()
Me.Close()
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
MsgBox("Into preview")
m_CurrentPage = 0
Dim ppdlg As ExtendedPrintPreviewDialog

With (ppdlg)
.Document = PrintDoco
.WindowState = FormWindowState.Maximized
.ShowDialog()
.Dispose()
End With
MsgBox("Out of Preview")
End Sub

Private Sub btnPrinterSettings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnPrinterSettings.Click

Dim pdlg As New PrintDialog
With pdlg
.Document = PrintDoco
.PrinterSettings = PrintDoco.PrinterSettings
.AllowPrintToFile = False
If .ShowDialog() = DialogResult.OK Then
SelectPrinter(cboPrinters, .PrinterSettings.PrinterName)
End If
.Dispose()
End With
End Sub

Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPageSetup.Click

Dim psdlg As New PageSetupDialog
With psdlg
.PrinterSettings = PrintDoco.PrinterSettings
.PageSettings = PrintDoco.DefaultPageSettings
If .ShowDialog() = DialogResult.OK Then
.PageSettings.Margins =
PrinterUnitConvert.Convert(.PageSettings.Margins,
PrinterUnit.ThousandthsOfAnInch, PrinterUnit.HundredthsOfAMillimeter)
SelectPrinter(cboPrinters, .PrinterSettings.PrinterName)
End If
.Dispose()
End With
End Sub
'Public Shared Sub Main()
' Application.EnableVisualStyles()
' Application.DoEvents()
' Application.Run(New Print)
'End Sub
Private Sub SelectPrinter(ByVal ComboBox As ComboBox, ByVal PrinterName
As String)
For i As Integer = 0 To ComboBox.Items.Count - 1
If ComboBox.Items(i).ToString() = PrinterName Then
ComboBox.SelectedIndex = i
Exit For
End If
Next i
End Sub

End Class
Module Module1
Public lstData
Public PrintDoco
End Module

Public Class frmSystemInformation
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

#End Region
Private PrintDoco As New PrintDocument

Private m_CurrentPage As Integer
Public frmPrint As Form

Private Sub cmdOperatingSystem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdOperatingSystem.Click

Dim sClassname As String = "Win32_OperatingSystem"
lstData.Items.Clear()
'grab the class
Dim oServices As New System.Management.ManagementClass(sClassname)
Dim oService As System.Management.ManagementObject
'loop through the instances
For Each oService In oServices.GetInstances
'Add to the list box
lstData.Items.Add(oService("Name"))
lstData.Items.Add(oService("Manufacturer"))
lstData.Items.Add(oService("Version"))
lstData.Items.Add(oService("CSDVersion"))
Next
End Sub

Private Sub cmdNetwork_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdNetwork.Click
Dim mc As System.Management.ManagementClass
Dim objWMI As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration ")
lstData.Items.Clear()
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each objWMI In moc
If objWMI.Item("IPEnabled") = True Then
lstData.Items.Add("MAC address is: " &
objWMI.Item("MacAddress").ToString())
lstData.Items.Add(objWMI("DNSHostName"))
'lstData.Items.Add(objWMI("DNSDomain"))
lstData.Items.Add(objWMI.Item("MACAddress"))
lstData.Items.Add(objWMI("Description"))
End If
Next
End Sub

Private Sub cmdBIOS_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdBIOS.Click
Dim sClassname As String = "Win32_BIOS"
lstData.Items.Clear()
'grab the class
Dim objWMISet As New System.Management.ManagementClass(sClassname)
Dim objWMI As System.Management.ManagementObject
'loop through the instances
For Each objWMI In objWMISet.GetInstances
'Add to the list box
lstData.Items.Add(String.Format("{0}:{1}", "Name",
objWMI("Name")))
lstData.Items.Add(String.Format("{0}:{1}", "Manufacturer",
objWMI("Manufacturer")))
lstData.Items.Add(String.Format("{0}:{1}", "Version",
objWMI("Version")))
lstData.Items.Add(String.Format("{0}:{1}", "Serial Number",
objWMI("SerialNumber")))

Next
End Sub

Private Sub cmdProcessor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdProcessor.Click
Dim sClassname As String = "Win32_Processor"
lstData.Items.Clear()
'grab the class
Dim objWMISet As New System.Management.ManagementClass(sClassname)
Dim objWMI As System.Management.ManagementObject
'loop through the instances
For Each objWMI In objWMISet.GetInstances
'Add to the list box
lstData.Items.Add(String.Format("{0}:{1}", "Name",
objWMI("Name")))
lstData.Items.Add(String.Format("{0}:{1}", "Manufacturer",
objWMI("Manufacturer")))
lstData.Items.Add(String.Format("{0}:{1}", "Maximum Clock
Speed", objWMI("MaxClockSpeed")))
Next
End Sub

Private Sub cmdDisks_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDisks.Click
Dim sClassname As String = "Win32_LogicalDisk"
lstData.Items.Clear()
'grab the class
Dim objWMISet As New System.Management.ManagementClass(sClassname)
Dim objWMI As System.Management.ManagementObject
'loop through the instances
For Each objWMI In objWMISet.GetInstances
'Add to the list box
lstData.Items.Add(String.Format("{0}:{1}", "Name",
objWMI("Name")))
lstData.Items.Add(String.Format("{0}:{1}", "Size",
objWMI("Size")))
lstData.Items.Add(String.Format("{0}:{1}", "Free Space",
objWMI("FreeSpace")))
Next
End Sub

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPrint.Click
Dim frmPrint As New Print

frmPrint.Show()
End Sub

End Class
Thanks
Jul 21 '05 #1
1 2041
Nick,

I think that when you want some help, you should first make a more simple
sample. This code you show now you can test, we miss all input, therefore
helping you is in my opinion almost impossible or there should be somebody
who has a lucky hit and than still it will be guessing in my opinion.

Probably by the way when you make that simple sample, you find the error
yourself.

Sorry could not give any help more,

Cor
"NickB" <Ni***@discussions.microsoft.com>
Please could someone tell me what is wrong.
Ther error is:
An unhandled exception of type 'System.NullReferenceException' occurred in
microsoft.visualbasic.dll

Additional information: Object variable or With block variable not set.

I am trying to print the contents of a list box on another form.

This is the sub, and the highlighted line is where it is falling over.
Private Sub PrintDoco_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoco.PrintPage

'm_CurrentPage += 1

Dim max As Integer
Dim n As Integer
max = lstData.count *****************************************
MsgBox(max, , "Test")
For n = 0 To max - 1
e.Graphics.DrawString(lstData.item.count(), New Font("arial",
12, FontStyle.Regular), Brushes.Black, 200, 200 + n * 45)
Next

e.HasMorePages = False

End Sub

This is the code for both forms :

Imports System.Management
Imports System.Drawing.Printing
Imports System
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing

Public Class Print
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private WithEvents PrintDoco As New PrintDocument
Private m_CurrentPage As Integer

Private Sub Print_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

With Me.cboPrinters
For Each s As String In PrinterSettings.InstalledPrinters
.Items.Add(s)
Next s
If .Items.Count > 0 Then
.SelectedIndex = 0
Else
MessageBox.Show("No printers installed, quitting!",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Me.Close()
End If
End With
'PrintDoco.DocumentName = "Unknown"

AddHandler PrintDoco.PrintPage, AddressOf PrintDoco_PrintPage
m_CurrentPage = 0
End Sub

Private Sub cboPrinters_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
cboPrinters.SelectedIndexChanged
If Me.cboPrinters.SelectedIndex <> -1 Then
PrintDoco.PrinterSettings.PrinterName = Me.cboPrinters.Text
End If
End Sub

Private Sub PrintDoco_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoco.PrintPage

'm_CurrentPage += 1

Dim max As Integer
Dim n As Integer
max = lstData.count
MsgBox(max, , "Test")
For n = 0 To max - 1
e.Graphics.DrawString(lstData.item.count(), New Font("arial",
12, FontStyle.Regular), Brushes.Black, 200, 200 + n * 45)
Next

e.HasMorePages = False

End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrint.Click

PrintDoco.Print()
'lstData.Focus()
'lstData.SelectAll()
'lstData.Show()
Me.Close()
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPreview.Click
MsgBox("Into preview")
m_CurrentPage = 0
Dim ppdlg As ExtendedPrintPreviewDialog

With (ppdlg)
.Document = PrintDoco
.WindowState = FormWindowState.Maximized
.ShowDialog()
.Dispose()
End With
MsgBox("Out of Preview")
End Sub

Private Sub btnPrinterSettings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnPrinterSettings.Click

Dim pdlg As New PrintDialog
With pdlg
.Document = PrintDoco
.PrinterSettings = PrintDoco.PrinterSettings
.AllowPrintToFile = False
If .ShowDialog() = DialogResult.OK Then
SelectPrinter(cboPrinters, .PrinterSettings.PrinterName)
End If
.Dispose()
End With
End Sub

Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles btnPageSetup.Click

Dim psdlg As New PageSetupDialog
With psdlg
.PrinterSettings = PrintDoco.PrinterSettings
.PageSettings = PrintDoco.DefaultPageSettings
If .ShowDialog() = DialogResult.OK Then
.PageSettings.Margins =
PrinterUnitConvert.Convert(.PageSettings.Margins,
PrinterUnit.ThousandthsOfAnInch, PrinterUnit.HundredthsOfAMillimeter)
SelectPrinter(cboPrinters, .PrinterSettings.PrinterName)
End If
.Dispose()
End With
End Sub
'Public Shared Sub Main()
' Application.EnableVisualStyles()
' Application.DoEvents()
' Application.Run(New Print)
'End Sub
Private Sub SelectPrinter(ByVal ComboBox As ComboBox, ByVal PrinterName
As String)
For i As Integer = 0 To ComboBox.Items.Count - 1
If ComboBox.Items(i).ToString() = PrinterName Then
ComboBox.SelectedIndex = i
Exit For
End If
Next i
End Sub

End Class
Module Module1
Public lstData
Public PrintDoco
End Module

Public Class frmSystemInformation
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

#End Region
Private PrintDoco As New PrintDocument

Private m_CurrentPage As Integer
Public frmPrint As Form

Private Sub cmdOperatingSystem_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cmdOperatingSystem.Click

Dim sClassname As String = "Win32_OperatingSystem"
lstData.Items.Clear()
'grab the class
Dim oServices As New System.Management.ManagementClass(sClassname)
Dim oService As System.Management.ManagementObject
'loop through the instances
For Each oService In oServices.GetInstances
'Add to the list box
lstData.Items.Add(oService("Name"))
lstData.Items.Add(oService("Manufacturer"))
lstData.Items.Add(oService("Version"))
lstData.Items.Add(oService("CSDVersion"))
Next
End Sub

Private Sub cmdNetwork_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdNetwork.Click
Dim mc As System.Management.ManagementClass
Dim objWMI As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration ")
lstData.Items.Clear()
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each objWMI In moc
If objWMI.Item("IPEnabled") = True Then
lstData.Items.Add("MAC address is: " &
objWMI.Item("MacAddress").ToString())
lstData.Items.Add(objWMI("DNSHostName"))
'lstData.Items.Add(objWMI("DNSDomain"))
lstData.Items.Add(objWMI.Item("MACAddress"))
lstData.Items.Add(objWMI("Description"))
End If
Next
End Sub

Private Sub cmdBIOS_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdBIOS.Click
Dim sClassname As String = "Win32_BIOS"
lstData.Items.Clear()
'grab the class
Dim objWMISet As New System.Management.ManagementClass(sClassname)
Dim objWMI As System.Management.ManagementObject
'loop through the instances
For Each objWMI In objWMISet.GetInstances
'Add to the list box
lstData.Items.Add(String.Format("{0}:{1}", "Name",
objWMI("Name")))
lstData.Items.Add(String.Format("{0}:{1}", "Manufacturer",
objWMI("Manufacturer")))
lstData.Items.Add(String.Format("{0}:{1}", "Version",
objWMI("Version")))
lstData.Items.Add(String.Format("{0}:{1}", "Serial Number",
objWMI("SerialNumber")))

Next
End Sub

Private Sub cmdProcessor_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles cmdProcessor.Click
Dim sClassname As String = "Win32_Processor"
lstData.Items.Clear()
'grab the class
Dim objWMISet As New System.Management.ManagementClass(sClassname)
Dim objWMI As System.Management.ManagementObject
'loop through the instances
For Each objWMI In objWMISet.GetInstances
'Add to the list box
lstData.Items.Add(String.Format("{0}:{1}", "Name",
objWMI("Name")))
lstData.Items.Add(String.Format("{0}:{1}", "Manufacturer",
objWMI("Manufacturer")))
lstData.Items.Add(String.Format("{0}:{1}", "Maximum Clock
Speed", objWMI("MaxClockSpeed")))
Next
End Sub

Private Sub cmdDisks_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdDisks.Click
Dim sClassname As String = "Win32_LogicalDisk"
lstData.Items.Clear()
'grab the class
Dim objWMISet As New System.Management.ManagementClass(sClassname)
Dim objWMI As System.Management.ManagementObject
'loop through the instances
For Each objWMI In objWMISet.GetInstances
'Add to the list box
lstData.Items.Add(String.Format("{0}:{1}", "Name",
objWMI("Name")))
lstData.Items.Add(String.Format("{0}:{1}", "Size",
objWMI("Size")))
lstData.Items.Add(String.Format("{0}:{1}", "Free Space",
objWMI("FreeSpace")))
Next
End Sub

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPrint.Click
Dim frmPrint As New Print

frmPrint.Show()
End Sub

End Class
Thanks

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
0
by: ALMA_J_III | last post by:
Hi all! Customer will use HP LaserJet 4200TN printer for printing of invoices. 3 different versions of invoice on different color of paper should be to print. HP LaserJet 4200TN have 3 paper...
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
0
by: Hank | last post by:
We have been printing pdf files through Adobe, from Access for several years. Adobe version 5.0 is currently installed. Recently we have received PDF files that were created under the Adobe 7.0...
3
by: William Chang | last post by:
Is the different behavior between __repr__ and __str__ intentional when it comes to printing lists? Basically I want to print out a list with elements of my own class, but when I overwrite __str__,...
2
by: Brad Pears | last post by:
I have a vb.net 2005 application and am using the print preview screen. This screen has a printer icon on it that the user can use to print the document currently being viewed. It uses the default...
4
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's...
1
by: Glenn | last post by:
I am writing a program for field work that will use a receipt printer. I need to be able to adjust the page settings prior to printing depending on how much needs to be printed. I have been able...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.