I'm currently doing an assignment to input student name, English mark, math mark, and science mark. I have use the listbox as the code below. Then I also need to display the highest scorer and lowest scorer. I have tried and I have no idea how can I do it. Can somebody help me out?
And another thing is, how can I align the listbox items?
Public Class StudentsGradeForm
Dim StdDetails As String = "{0, -10}{1, -20}{2, -20}{3, -20}{4, -20}{5, -20}"
Dim num1integer, num2integer, num3integer As Integer
Dim resultdouble As Double
Dim grade As String
Private Sub exitbtn_Click(sender As Object, e As EventArgs) Handles exitbtn.Click
Dim iExit As DialogResult
iExit = MessageBox.Show("Confirm you want to exit the system", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If iExit = DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub Calcbtn_Click(sender As Object, e As EventArgs) Handles Calcbtn.Click
num1integer = num1txt.Text
num2integer = num2txt.Text
num3integer = num3txt.Text
resultdouble = (num1integer num2integer num3integer) / 3
outlbl.Text = resultdouble
If resultdouble >= 80 And resultdouble <= 100 Then
grade = "A"
ElseIf resultdouble >= 70 And resultdouble <= 79 Then
grade = "B"
ElseIf resultdouble >= 60 And resultdouble <= 69 Then
grade = "C"
ElseIf resultdouble >= 50 And resultdouble <= 59 Then
grade = "D"
ElseIf resultdouble >= 40 And resultdouble <= 49 Then
grade = "E"
Else
grade = "F"
End If
outgradelbl.Text = grade
End Sub
Private Sub StudentsGradeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.Items.Add(String.Format(StdDetails, "Name", "Eng", "Math", "Sci", "Average", "Grade"))
End Sub
Private Sub updatebtn_Click(sender As Object, e As EventArgs) Handles updatebtn.Click
Dim StudentName, EnglishMark, MathMark, ScienceMark As String
Dim Average As Double
Dim Grade As Char
StudentName = nametxt.Text
EnglishMark = num1txt.Text
MathMark = num2txt.Text
ScienceMark = num3txt.Text
Average = outlbl.Text
Grade = outgradelbl.Text
ListBox1.Items.Add(String.Format(StdDetails, StudentName, EnglishMark, MathMark, ScienceMark, Average, Grade))
End Sub
Private Sub clrbtn_Click(sender As Object, e As EventArgs) Handles clrbtn.Click
nametxt.Clear()
num1txt.Clear()
num2txt.Clear()
num3txt.Clear()
ListBox1.Items.Clear()
End Sub
End Class
CodePudding user response:
Create a class student:
Public Class Student
Dim _Name as String
Dim _Eng as Integer
etc...
Then you write your properties in your class:
Property Name as String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
And you do the same for the other five fields you have. Then you'll have to convert/parse your string into a numeric value (I assume you will use Integer). So you can do either:
CInt(myString)
Integer.TryParse(myString)
And when you want to convert back to string you just do:
myInteger.ToString()
And if you want to store values into your student Object:
Dim myStudent As New Student
myStudent.Name = Field1txt.Text 'the string that should represent the student name
Long story short, it's easier and faster, if you need to do some math, to store your values as Integer instead of String. If you have a mix of the two and you want to keep them together in one entity, just create a custom Class.
CodePudding user response:
Turn on Option Strict. Project Properties -> Compile tab. Also for future projects Tools -> Options -> Projects and Solutions -> VB Defaults This should ALWAYS be on. (actually there are rare occasions where it needs to be off)
Public Class StudentGrade
Public Property StudentName As String
Public Property EnglishMark As Integer
Public Property MathMark As Integer
Public Property ScienceMark As Integer
Public Property Average As Double
Public Property Grade As String
Public Sub New(Name As String, EngMark As Integer, MthMark As Integer, SciMark As Integer)
StudentName = Name
EnglishMark = EngMark
MathMark = MthMark
ScienceMark = SciMark
CalculateAverage()
CalculateGrade()
End Sub
Private Sub CalculateAverage()
Dim resultdouble = (EnglishMark MathMark ScienceMark) / 3
Average = resultdouble
End Sub
Private Sub CalculateGrade()
Select Case Average
Case >= 80
Grade = "A"
Case >= 70
Grade = "B"
Case >= 60
Grade = "C"
Case >= 50
Grade = "D"
Case >= 40
Grade = "E"
Case Else
Grade = "F"
End Select
End Sub
End Class
First, let's consider the class. Classes can have Properties and Methods among other things. The classes data (like Name
or MathMark
) is contained in the properties. The methods act on this data. The class is the cookie cutter and an instance of the class is the cookie. To create an instance of StudentGrade
, call Sub New
. This Sub New
has parameters that are used to set the values of several properties. It also calls Private
methods (visible only in the class) that set the other properties. Now we have a fully fleshed out instance of the StudentGrade
class.
The Average can have values between 79 - 80, 69 - 70, 59 - 60, and 49 - 50. Your code would have missed these values and failed the student I changed the code to a Select Case for readability.
Back to the Form class. We have a form level variable StuList to hold the instances of the StudentGrade class that we create.
Private StuList As New List(Of StudentGrade)
The Form load sets the DataSource
property of a DataGridView
. This should be much easier than trying to make a ListBox line up. You fill in the text boxes with appropriate data and click Button1 to create a new student.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = StuList
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateNewStudent()
End Sub
In the CreateNewStudent
method we first validate the user input. The TryParse
method checks the first parameter to see if it can be converted to the indicated type. It returns True
or False
. If it returns True
it fills in the second parameter with the converted value. If it returns False we don't want to continue with the method so we inform the user and exit the sub.
We call the New
method with Dim stu As New StudentGrade
and pass the required parameters to fill the properties of this instance. Add this instance of the class to the list. You can use the properties of StudentGrade
to display in your Labels.
Finally we remove the DataSource
of the DataGridView
and reset it to the list with the new addition. This is sort of cludgy way to do this but it works without introducing more complicated approaches.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim max = StuList.Max(Function(stu) stu.Average)
MessageBox.Show($"The highest average is {max:N0}")
Dim min = StuList.Min(Function(stu) stu.Average)
MessageBox.Show($"The lowest average is {min:N0}")
End Sub
After you have created the students you want to display the minimum and maximum average. You can use an extension method of the List(Of T) class.