G
Guest
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
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