在列表框中打印内容

时间:2013-03-28 02:44:38

标签: vb.net

我正在尝试在程序运行后打印列表框中的内容(输入的课程,总小时数,数量点数,总GPA)。它打开了我的打印机选项,但每次都打印一个空白页......

Public Class Form1

Dim QualityPoints As Double
Dim CourseName, hours As String

Private Sub btnTotalHours_Click(sender As Object, e As EventArgs) Handles btnTotalHours.Click

    txtBoxTotalHours.Text = NumericUpDown1.Value + NumericUpDown2.Value + NumericUpDown3.Value + NumericUpDown4.Value + NumericUpDown5.Value + NumericUpDown6.Value + NumericUpDown7.Value + NumericUpDown8.Value

End Sub

Private Sub btnTotalQualityPoints_Click(sender As Object, e As EventArgs) Handles btnTotalQualityPoints.Click

    Select Case txtBoxGrade1.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown1.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown1.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown1.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown1.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown1.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown1.Value)

    End Select

    Select Case txtBoxGrade2.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown2.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown2.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown2.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown2.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown2.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown2.Value)

    End Select

    Select Case txtBoxGrade3.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown3.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown3.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown3.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown3.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown3.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown3.Value)

    End Select

    Select Case txtBoxGrade4.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown4.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown4.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown4.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown4.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown4.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown4.Value)

    End Select

    Select Case txtBoxGrade5.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown5.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown5.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown5.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown5.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown5.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown5.Value)

    End Select

    Select Case txtBoxGrade6.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown6.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown6.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown6.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown6.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown6.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown6.Value)

    End Select

    Select Case txtBoxGrade7.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown7.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown7.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown7.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown7.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown7.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown7.Value)

    End Select

    Select Case txtBoxGrade8.Text
        Case "A"
            QualityPoints += (4 * NumericUpDown8.Value)
        Case "B"
            QualityPoints += (3 * NumericUpDown8.Value)
        Case "C"
            QualityPoints += (2 * NumericUpDown8.Value)
        Case "D"
            QualityPoints += (1 * NumericUpDown8.Value)
        Case "F"
            QualityPoints += (0 * NumericUpDown8.Value)
        Case " "
            QualityPoints += (0 * NumericUpDown8.Value)

    End Select

    txtBoxTotalQualityPoints.Text = QualityPoints

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

    txtBoxCourse1.Clear()
    txtBoxCourse2.Clear()
    txtBoxCourse3.Clear()
    txtBoxCourse4.Clear()
    txtBoxCourse5.Clear()
    txtBoxCourse6.Clear()
    txtBoxCourse7.Clear()
    txtBoxCourse8.Clear()

    txtBoxGrade1.Clear()
    txtBoxGrade2.Clear()
    txtBoxGrade3.Clear()
    txtBoxGrade4.Clear()
    txtBoxGrade5.Clear()
    txtBoxGrade6.Clear()
    txtBoxGrade7.Clear()
    txtBoxGrade8.Clear()

    txtBoxTotalGPA.Clear()
    txtBoxTotalHours.Clear()
    txtBoxTotalQualityPoints.Clear()

    NumericUpDown1.Value = 0
    NumericUpDown2.Value = 0
    NumericUpDown3.Value = 0
    NumericUpDown4.Value = 0
    NumericUpDown5.Value = 0
    NumericUpDown6.Value = 0
    NumericUpDown7.Value = 0
    NumericUpDown8.Value = 0


    lstBoxResults.Items.Clear()

End Sub

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

    Dim semester As String
    Dim totalGPA As Double = QualityPoints / txtBoxTotalHours.Text

    semester = CStr(lstBoxResults.Text)
    txtBoxTotalGPA.Text = CStr(totalGPA)
    txtBoxTotalGPA.Text = Math.Round(totalGPA, 3)

    If radButtonFall.Checked Then

        semester = "Fall"

    ElseIf radButtonSpring.Checked Then

        semester = "Spring"

    ElseIf radButtonSummer.Checked Then

        semester = "Summer"

    End If


    lstBoxResults.Items.Add(semester & " " & "Semester : ")
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add("Total Hours : " & txtBoxTotalHours.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add("Total Qualit Points : " & txtBoxTotalQualityPoints.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add("Total " & semester & " " & "GPA : " & txtBoxTotalGPA.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add("Enrolled Courses : ")
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse1.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse2.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse3.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse4.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse5.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse6.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse7.Text)
    lstBoxResults.Items.Add(" ")
    lstBoxResults.Items.Add(txtBoxCourse8.Text)

End Sub

Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click

    End

End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim font1 As New Font("Arial", 16, FontStyle.Regular)
    e.Graphics.DrawString(lstBoxResults.Text, font1, Brushes.Black, 100, 100)

End Sub

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

    PrintDialog1.ShowDialog()
    PrintDocument1.Print()

End Sub

结束班

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

试用本教程http://www.knowdotnet.com/articles/printform.html 这完全是关于印刷品。您的问题只打印空白,因为您的print_Page仍然没有得到任何对象

答案 1 :(得分:0)

虽然这对您的打印没有帮助,但您应该用这些内容替换Select Case

在班级:

Private GradeToValue As New Dictionary(Of String, Integer) _
    From {{"A", 4}, {"B", 3}, {"C", 2}, {"D", 1}, {"F", 0}, {" ", 0}}

在你的按钮处理程序中:

QualityPoints += (GradeToValue(txBoxGrade1.Text) * NumericUpDown1.Value)
QualityPoints += (GradeToValue(txBoxGrade2.Text) * NumericUpDown2.Value)
' etc.