Hi, I need help with a Visual Basic Programming assignment please. The assignmen
ID: 3661501 • Letter: H
Question
Hi, I need help with a Visual Basic Programming assignment please. The assignment is "Student Test Scores form" in Ch.9 Challenge 3 in Starting Out With Visual Basic 2012 6th addition. I need help with the code for printing a report and opening the file. I originally had a code to open the file but the teacher said when he opened it the program couldn't find the file. Thanks for your help!
STUDENT TEST SCORES:
Public Class MainForm
Dim std(6) As Student
Public Function IsValid(ByVal num As Integer) As Integer
If num >= 0 And num <= 100 Then
Return (num)
Else
Return ("Please try again,")
End If
End Function
Private Sub mnuHelpAbout_Click(sender As Object, e As EventArgs) Handles mnuHelpAbout.Click
'about program
MessageBox.Show("Student test score calculator")
End Sub
Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
' Close(program)
Me.Close()
End Sub
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Dim filename As String = InputBox("Enter filename : ", "Filename prompt", "output.txt")
Dim sw As IO.StreamWriter = IO.File.CreateText(filename)
For i = 0 To 5
sw.WriteLine(std(i).studentName)
sw.WriteLine(std(i).testScores(0))
sw.WriteLine(std(i).testScores(1))
sw.WriteLine(std(i).testScores(2))
sw.WriteLine(std(i).testScores(3))
sw.WriteLine(std(i).testScores(4))
sw.WriteLine(std(i).average)
Next
sw.Close()
MessageBox.Show(filename & " file created successfully", "DONE")
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
For intCount = 0 To 5
std(intCount) = New Student()
ReDim std(intCount).testScores(4)
Next
Try
std(0).studentName = txtStdName1.Text
std(0).testScores(0) = IsValid(Convert.ToInt32(txtScore1Std1.Text))
std(0).testScores(1) = IsValid(Convert.ToInt32(txtScore2Std1.Text))
std(0).testScores(2) = IsValid(Convert.ToInt32(txtScore3Std1.Text))
std(0).testScores(3) = IsValid(Convert.ToInt32(txtScore4Std1.Text))
std(0).testScores(4) = IsValid(Convert.ToInt32(txtScore5Std1.Text))
std(1).studentName = txtStdName2.Text
std(1).testScores(0) = IsValid(Convert.ToInt32(txtScore1Std2.Text))
std(1).testScores(1) = IsValid(Convert.ToInt32(txtScore2Std2.Text))
std(1).testScores(2) = IsValid(Convert.ToInt32(txtScore3Std2.Text))
std(1).testScores(3) = IsValid(Convert.ToInt32(txtScore4Std2.Text))
std(1).testScores(4) = IsValid(Convert.ToInt32(txtScore5Std2.Text))
std(2).studentName = txtStdName3.Text
std(2).testScores(0) = IsValid(Convert.ToInt32(txtScore1Std3.Text))
std(2).testScores(1) = IsValid(Convert.ToInt32(txtScore2Std3.Text))
std(2).testScores(2) = IsValid(Convert.ToInt32(txtScore3Std3.Text))
std(2).testScores(3) = IsValid(Convert.ToInt32(txtScore4Std3.Text))
std(2).testScores(4) = IsValid(Convert.ToInt32(txtScore5Std3.Text))
std(3).studentName = txtStdName4.Text
std(3).testScores(0) = IsValid(Convert.ToInt32(txtScore1Std4.Text))
std(3).testScores(1) = IsValid(Convert.ToInt32(txtScore2Std4.Text))
std(3).testScores(2) = IsValid(Convert.ToInt32(txtScore3Std4.Text))
std(3).testScores(3) = IsValid(Convert.ToInt32(txtScore4Std4.Text))
std(3).testScores(4) = IsValid(Convert.ToInt32(txtScore5Std4.Text))
std(4).studentName = txtStdName5.Text
std(4).testScores(0) = IsValid(Convert.ToInt32(txtScore1Std5.Text))
std(4).testScores(1) = IsValid(Convert.ToInt32(txtScore2Std5.Text))
std(4).testScores(2) = IsValid(Convert.ToInt32(txtScore3Std5.Text))
std(4).testScores(3) = IsValid(Convert.ToInt32(txtScore4Std5.Text))
std(4).testScores(4) = IsValid(Convert.ToInt32(txtScore5Std5.Text))
std(5).studentName = txtStdName6.Text
std(5).testScores(0) = IsValid(Convert.ToInt32(txtScore1Std6.Text))
std(5).testScores(1) = IsValid(Convert.ToInt32(txtScore2Std6.Text))
std(5).testScores(2) = IsValid(Convert.ToInt32(txtScore3Std6.Text))
std(5).testScores(3) = IsValid(Convert.ToInt32(txtScore4Std6.Text))
std(5).testScores(4) = IsValid(Convert.ToInt32(txtScore5Std6.Text))
Catch ex As Exception
MessageBox.Show("Please Enter a Value from 0-100.")
End Try
For intCount = 0 To 5
std(intCount).average = 0
For intCount1 = 0 To 4
std(intCount).average += std(intCount).testScores(intCount1)
Next
std(intCount).average /= 5
Next
txtAvg1.Text = std(0).average.ToString("n2")
txtAvg2.Text = std(1).average.ToString("n2")
txtAvg3.Text = std(2).average.ToString("n2")
txtAvg4.Text = std(3).average.ToString("n2")
txtAvg5.Text = std(4).average.ToString("n2")
txtAvg6.Text = std(5).average.ToString("n2")
For count = 0 To 5
Dim stdRecord As String
stdRecord = std(count).studentName
For count1 = 0 To 4
stdRecord += " " +
std(count).testScores(count1).ToString("n2")
Next
stdRecord += "" + std(count).average.ToString()
Next
End Sub
Private Sub PrintReportToolStripMenuItem_Click(sender As Object, e As EventArgs)
Dim str As String = ""
For i = 0 To 5
str &= std(i).studentName & vbTab
str &= std(i).testScores(0) & vbTab
str &= std(i).testScores(1) & vbTab
str &= std(i).testScores(2) & vbTab
str &= std(i).testScores(3) & vbTab
str &= std(i).testScores(4) & vbTab
str &= std(i).average & vbNewLine
Next
MessageBox.Show(str, "Report")
End Sub
End Class
Public Class Student
Public Property studentName() As String
Public testScores() As Integer
Public Property average() As Decimal
End Class
Explanation / Answer
First some observations of your code:
1. Your code is not complete.
2. Items (menuitems, button etc.) for which event handlers you have defined have not been declared and added to the form.
3. It is not runnable due to incompleteness.
You can use the following tips for your assignment:
1. To save the file, do as following:
-------------------------------------
Dim sw As IO.StreamWriter
sw = IO.File.CreateText(filename)
sw.WriteLine("Hello World")
------------------------------------------
But for filename, you will need to specify the full path where you want to save your file. If you specify only the filename, it will be saved at default location (inside Debug folder of your solution).
2. To locate, select and open a file, you can use OpenFileDialog class:
--------------------------------------
Dim stream As IO.Stream = Nothing
Dim openFileDialog As New OpenFileDialog()
openFileDialog.InitialDirectory = IO.Directory.GetCurrentDirectory()
openFileDialog.Filter = "All files (*.*)|*.*"
openFileDialog.RestoreDirectory = True
If openFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
stream = openFileDialog.OpenFile()
If (stream IsNot Nothing) Then
MessageBox.Show("File is open.") 'for example only.
'you can read data from file here.
End If
Catch Ex As Exception
MessageBox.Show("Error reading file!")
Finally
If (stream IsNot Nothing) Then
stream.Close()
End If
End Try
End If
---------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.