Ch7 01
'Project: Exercise 7.1
'Programmer: xxxx
'Date: xxxxx
'Description: The user can enter information about a student and then print the
' information from the form. The user can also choose to print the
' list of high schools.
'Folder: EX0701
Option Strict On
Public Class studentForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Dim studentClassString As String
Private Sub highSchoolComboBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles highSchoolComboBox.Validating
'The instructions for this exercise ask you to write the code
'to add to the high school list using the Validate event procedure.
'The "Validating event" is discussed in Chapter 13.
Dim schoolIndexInteger As Integer
Dim schoolFoundBoolean As Boolean
If highSchoolComboBox.Text > "" Then
Do Until schoolIndexInteger = highSchoolComboBox.Items.Count
If highSchoolComboBox.Text = highSchoolComboBox.Items(schoolIndexInteger).ToString() Then
schoolFoundBoolean = True
End If
schoolIndexInteger += 1
Loop
If schoolFoundBoolean = False Then
highSchoolComboBox.Items.Add(highSchoolComboBox.Text)
End If
Else
MessageBox.Show("Enter a school to add", _
"Missing data", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
highSchoolComboBox.Focus()
End If
End Sub
Private Sub freshmanRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles freshmanRadioButton.CheckedChanged
'Set the student class value to Freshman
studentClassString = "Freshman"
End Sub
Private Sub sophomoreRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sophomoreRadioButton.CheckedChanged
'Set the student class value to Sophomore
studentClassString = "Sophomore"
End Sub
Private Sub juniorRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles juniorRadioButton.CheckedChanged
'Set the student class value to Junior
studentClassString = "Junior"
End Sub
Private Sub seniorRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles seniorRadioButton.CheckedChanged
'Set the student class value to Senior
studentClassString = "Senior"
End Sub
Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
'Add the users school to the list if is not already there.
Dim schoolFoundBoolean As Boolean
Dim schoolIndexInteger As Integer
If highSchoolComboBox.Text > "" Then
Do Until schoolFoundBoolean Or schoolIndexInteger = highSchoolComboBox.Items.Count
If highSchoolComboBox.Text = highSchoolComboBox.Items(schoolIndexInteger).ToString() Then
schoolFoundBoolean = True
End If
schoolIndexInteger += 1
Loop
If schoolFoundBoolean Then
MessageBox.Show("Duplicate School Name.", "Add Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
'If it's not in the list, add it
highSchoolComboBox.Items.Add(highSchoolComboBox.Text)
End If
Else
MessageBox.Show("Enter a Name to Add to the List", _
"Missing data", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End If
highSchoolComboBox.Focus()
End Sub
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
'Clear the form
freshmanRadioButton.Checked = False
sophomoreRadioButton.Checked = False
juniorRadioButton.Checked = False
seniorRadioButton.Checked = False
majorListBox.SelectedIndex = -1
highSchoolComboBox.SelectedIndex = -1
highSchoolComboBox.Text = ""
unitsTextBox.Clear()
deanCheckBox.Checked = False
studentNameTextBox.Clear()
studentNameTextBox.Focus()
End Sub
Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
'Validate all data then start the printing process.
Dim unitsDecimal As Decimal
'Make sure that the student information is completely filled in
If studentNameTextBox.Text > "" Then
Try
unitsDecimal = Decimal.Parse(unitsTextBox.Text)
If freshmanRadioButton.Checked = True Or sophomoreRadioButton.Checked = True _
Or juniorRadioButton.Checked = True Or seniorRadioButton.Checked = True Then
If majorListBox.SelectedIndex > -1 Then
If highSchoolComboBox.Text > "" Then
'All validation has been passed, print the student information
PrintPreviewDialog1.Document = printStudentPrintDocument
PrintPreviewDialog1.ShowDialog()
Else
MessageBox.Show("Please select a school.", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Please select a major.", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Please select a class.", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch
MessageBox.Show("Invalid number of units", "Input error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
unitsTextBox.SelectAll()
unitsTextBox.Focus()
End Try
Else
MessageBox.Show("Please enter a student name.", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
studentNameTextBox.Focus()
End If
End Sub
Private Sub filePrintSchoolsMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles filePrintSchoolsMenuItem.Click
'Print the list of highschools
PrintPreviewDialog1.Document = printAllSchoolsPrintDocument
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub fileExitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fileExitMenuItem.Click
'End the project
Me.Close()
End Sub
Private Sub helpAboutMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles helpAboutMenuItem.Click
'Display the about form
Dim aboutFormInstance As New aboutForm
aboutFormInstance.ShowDialog()
End Sub
Private Sub printAllSchoolsPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printAllSchoolsPrintDocument.PrintPage
'Handles printing and print previews when printing all schools in listbox
Dim printFont As New Font("Arial", 12)
Dim headingFont As New Font("Arial", 14, FontStyle.Bold)
Dim lineHeightSingle As Single = printFont.GetHeight + 2
Dim horizontalPrintLocationSingle As Single = e.MarginBounds.Left
Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top
Dim printLineString As String
Dim listIndexInteger As Integer
'Display the programmer's name at the top of the school list and leave 2 blank lines
printLineString = "Programmed by: Theresa Berry"
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle * 2
'Print a heading and then the school list
printLineString = "School Name"
e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
printLineString = "______"
e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
'Loop through the entire list and print each item in the list
For listIndexInteger = 0 To highSchoolComboBox.Items.Count - 1
'Set up a line.
printLineString = highSchoolComboBox.Items(listIndexInteger).ToString()
'Send the line to the graphics page object
e.Graphics.DrawString(printLineString, printFont, _
Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
'Increment the Y position for the next line.
verticalPrintLocationSingle += lineHeightSingle
Next listIndexInteger
End Sub
Private Sub printStudentPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printStudentPrintDocument.PrintPage
'Set up printer output
Dim printFont As New Font("Arial", 12)
Dim headingFont As New Font("Arial", 14, FontStyle.Bold)
Dim lineHeightSingle As Single = printFont.GetHeight + 2
Dim horizontalPrintLocationSingle As Single = e.MarginBounds.Left
Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top
Dim printLineString As String
'Print headings
printLineString = "Student Information"
e.Graphics.DrawString(printLineString, headingFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
printLineString = "by: Theresa Berry"
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
'Leave a blank line between the headings and the student information
verticalPrintLocationSingle += lineHeightSingle * 2
'Print the student information
printLineString = "Student Name: " & studentNameTextBox.Text.ToString
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
printLineString = "Number of Units: " & unitsTextBox.Text.ToString
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
printLineString = "Class: " & studentClassString
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
printLineString = "Major: " & (majorListBox.SelectedItem).ToString
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
printLineString = "High School: " & highSchoolComboBox.Text.ToString
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
verticalPrintLocationSingle += lineHeightSingle
'Check to see if they are on the Dean's list
If deanCheckBox.Checked = True Then
printLineString = "Dean's List: Yes"
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
Else
printLineString = "Dean's List: No"
e.Graphics.DrawString(printLineString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle)
End If
End Sub
End Class