I am trying to print the contents of a structure onto a print page in Visual Basic(visual studio 20开发者_StackOverflow08). However my for each loop generates a conversion error? Could someone help me figure out whats wrong? Thanks
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
looking at your for loop your ArrayOfData is of type IncomeRecord that is why it is complaining. If turn on Option Strict in Project Propertys Compile tab it will catch problems like above as you enter them. Give something like this a try
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
精彩评论