我正在尝试将结构的内容打印到Visual Basic中的打印页面上(visual studio 2008)。但是我的每个循环都会产生转换错误?有人能帮我搞清楚什么是错的?感谢
Public Class Form1
Structure IncomeRecord
Dim IDVal As Integer
Dim HouseholdNum As Integer
Dim YearlyIncome As Integer
End Structure
Dim ArrayOfData(100) As IncomeRecord
Dim ArrayofDataIndex As Integer = -1
Dim PovertyLevels(,) As Integer = {{1, 10210}, {2, 13690}, {3, 17170}, {4, 20650}, {5, 24130}, {6, 27610}, {7, 31090}, {8, 34570}}
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub EnterDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnterDataToolStripMenuItem.Click
Dim IDVal As Integer
Dim HouseholdNum As Integer
Dim YearlyIncome As Integer
IDVal = Integer.Parse(TextBox1.Text)
HouseholdNum = Integer.Parse(TextBox3.Text)
YearlyIncome = Decimal.Parse(TextBox2.Text)
ArrayofDataIndex += 1
ArrayOfData(ArrayofDataIndex).IDVal = IDVal
ArrayOfData(ArrayofDataIndex).HouseholdNum = HouseholdNum
ArrayOfData(ArrayofDataIndex).YearlyIncome = YearlyIncome
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim PrintFont As New Font("Arial", 12)
Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold)
Dim LineHeightSingle = PrintFont.GetHeight + 2
Dim Column1HorizontalLocationSingle As Single = e.MarginBounds.Left
Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top
Dim Column2HorizontalLocationSingle As Single = 300
Dim Column3HorizontalLocationSingle As Single
Dim PrintLineString As String
Dim FontSizeF As New SizeF
Dim FormattedPriceString As String
PrintLineString = "Income Survey-Listing All Input Data"
e.Graphics.DrawString(PrintLineString, HeadingFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
VerticalPrintLocationSingle += LineHeightSingle
e.Graphics.DrawString(PrintLineString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
For Each ElementString As String In ArrayOfData
e.Graphics.DrawString(ElementString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
Column2HorizontalLocationSingle += 200
Next
Column1HorizontalLocationSingle = e.MarginBounds.Left
VerticalPrintLocationSingle += LineHeightSingle
End Sub
Private Sub AllDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllDataToolStripMenuItem.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
End Class
Structure IncomeRecord
Dim IDVal As Integer
Dim HouseholdNum As Integer
Dim YearlyIncome As Integer
End Structure
Dim ArrayOfData(100) As IncomeRecord
Dim ArrayofDataIndex As Integer = -1
Dim PovertyLevels(,) As Integer = {{1, 10210}, {2, 13690}, {3, 17170}, {4, 20650}, {5, 24130}, {6, 27610}, {7, 31090}, {8, 34570}}
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub EnterDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnterDataToolStripMenuItem.Click
Dim IDVal As Integer
Dim HouseholdNum As Integer
Dim YearlyIncome As Integer
IDVal = Integer.Parse(TextBox1.Text)
HouseholdNum = Integer.Parse(TextBox3.Text)
YearlyIncome = Decimal.Parse(TextBox2.Text)
ArrayofDataIndex += 1
ArrayOfData(ArrayofDataIndex).IDVal = IDVal
ArrayOfData(ArrayofDataIndex).HouseholdNum = HouseholdNum
ArrayOfData(ArrayofDataIndex).YearlyIncome = YearlyIncome
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim PrintFont As New Font("Arial", 12)
Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold)
Dim LineHeightSingle = PrintFont.GetHeight + 2
Dim Column1HorizontalLocationSingle As Single = e.MarginBounds.Left
Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top
Dim Column2HorizontalLocationSingle As Single = 300
Dim Column3HorizontalLocationSingle As Single
Dim PrintLineString As String
Dim FontSizeF As New SizeF
Dim FormattedPriceString As String
PrintLineString = "Income Survey-Listing All Input Data"
e.Graphics.DrawString(PrintLineString, HeadingFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
VerticalPrintLocationSingle += LineHeightSingle
e.Graphics.DrawString(PrintLineString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
For Each ElementString As String In ArrayOfData
e.Graphics.DrawString(ElementString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
Column2HorizontalLocationSingle += 200
Next
Column1HorizontalLocationSingle = e.MarginBounds.Left
VerticalPrintLocationSingle += LineHeightSingle
End Sub
Private Sub AllDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AllDataToolStripMenuItem.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
答案 0 :(得分:0)
查看你的for循环你的ArrayOfData类型为IncomeRecord,这就是为什么它在抱怨。如果在Project Propertys Compile选项卡中打开Option Strict,它将在您输入时捕获上述问题。试试这样的东西
For Each ElementIncome As IncomeRecord In ArrayOfData
e.Graphics.DrawString(ElementIncome.IDVal.ToString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
Column2HorizontalLocationSingle += 200
e.Graphics.DrawString(ElementIncome.HouseholdNum.ToString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
Column2HorizontalLocationSingle += 200
e.Graphics.DrawString(ElementIncome.YearlyIncome.ToString, PrintFont, _
Brushes.Black, Column2HorizontalLocationSingle, VerticalPrintLocationSingle)
Column2HorizontalLocationSingle += 200
Next