Hi im trying to do this projexct for class. Here is the code

Public Class Form1

    Dim index As Integer = 0
    Structure Gradebook
        Dim ID As Integer
        Dim Exam1 As Integer
        Dim Exam2 As Integer
        Dim Exam3 As Integer
        Dim Avgg As Integer
        Dim LG As Char
    End Structure
    Dim Student(10) As Gradebook


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Student(index).ID = Val(TextBox1.Text)
        Student(index).Exam1 = Val(TextBox2.Text)
        Student(index).Exam2 = Val(TextBox3.Text)
        Student(index).Exam3 = Val(TextBox4.Text)
        Student(index).Avgg = Avg(Student(index).Exam1, Student(index).Exam2, Student(index).Exam3)
        Dim grade As Integer = Val(Student(index).Avgg)
        Student(index).LG = Lettergrade(grade)



        index = +1

        TextBox1.Text = Nothing
        TextBox2.Text = Nothing
        TextBox3.Text = Nothing
        TextBox4.Text = Nothing

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim formatstg As String = "{0,-15}{1,20}{2,20}{3,20}{4,20}{5,20}"

        ListBox1.Items.Add(String.Format(formatstg, "ID", "Exam1", "Exam2", "Exam3", "Avg", "LetterGrade"))
        For X As Integer = 0 To 10
            ListBox1.Items.Add(String.Format(formatstg, Student(X).ID, Student(X).Exam1, Student(X).Exam2, Student(X).Exam3, Student(X).Avgg, Student(X).LG))
        Next

    End Sub
    Function Avg(ByVal Grade1 As Double, ByVal Grade2 As Double, ByVal Grade3 As Double) As Double
        Return ((Grade1 + Grade2 + Grade3) / 3)
    End Function
    Function Lettergrade(ByVal Numgrade As Integer) As Char
        If Numgrade < 70 Then
            Return "F"
        ElseIf Numgrade < 80 Then
            Return "C"
        ElseIf Numgrade < 90 Then
            Return "B"
        ElseIf Numgrade <= 100 Then
            Return "A"
        End If
    End Function

End Class

Here is the Form when it is running http://gyazo.com/a5436577e6e0b703aba77f9913027d55

As you can tell the Zones are not the way it is supposto to be. I tried changing

Dim formatstg As String = "{0,-15}{1,20}{2,20}{3,20}{4,20}{5,20}"

So many times but nothing works. If any one could help i would appreciate it!!

Recommended Answers

All 2 Replies

Why are not not trying to use ListView Control instead of ListBox to display your data. ListBox is not perfect for this type of display.From my point of view ListView would be more suitable for that and more flexible to display than Listbox.

Structure Gradebook
Dim ID As Integer
Dim Exam1 As Integer
Dim Exam2 As Integer
Dim Exam3 As Integer
Dim Avgg As Integer
Dim LG As Char
End Structure

This is the Structure you declared. I just made a little change inyour structure which can give you your desired result. It should be

Structure Gradebook
        Dim ID As String        'Should be String Type
        Dim Exam1 As String
        Dim Exam2 As String
        Dim Exam3 As String
        Dim Avgg As String
        Dim LG As String


        Public Overrides Function ToString() As String
            Return String.Format("{0}{1}{2}{3}{4}{5}", {getString(ID), getString(Exam1), getString(Exam2), getString(Exam3), getString(Avgg), LG})
        End Function


        Private Function getString(ByVal str As String) As String
            'Function to get a fixed length string

            Dim cnt As Integer
            cnt = 20 - Len(Trim(str))

            Return str.PadRight(cnt) & vbTab
        End Function
End Structure

The result you can get Capture1.PNG

The full code snippet is

Public Class Form1

    Dim index As Integer = 0
    Structure Gradebook
        Dim ID As String        'Should be String Type
        Dim Exam1 As String
        Dim Exam2 As String
        Dim Exam3 As String
        Dim Avgg As String
        Dim LG As String

        'Public Overrides Function ToString() As String
        'Return String.Format("{0}{1}{2}{3}{4}{5}", {ID.PadRight(12) & vbTab, Exam1.PadRight(10) & vbTab, Exam2.PadRight(10) & vbTab, Exam3.PadRight(10) & vbTab, Avgg.PadRight(5) & vbTab, LG})
        'End Function

        Public Overrides Function ToString() As String
            Return String.Format("{0}{1}{2}{3}{4}{5}", {getString(ID), getString(Exam1), getString(Exam2), getString(Exam3), getString(Avgg), LG})
        End Function


        Private Function getString(ByVal str As String) As String
            'Function to get a fixed length string

            Dim cnt As Integer
            cnt = 20 - Len(Trim(str))

            Return str.PadRight(cnt) & vbTab
        End Function
    End Structure


    Dim Student(10) As Gradebook 'Array of 11 elements, First one is for Headings


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Adding Headings to the ListBox
        Student(index).ID = "Student's ID"
        Student(index).Exam1 = "Exam1"
        Student(index).Exam2 = "Exam2"
        Student(index).Exam3 = "Exam3"
        Student(index).Avgg = "Average"
        Student(index).LG = "Letter Grade"
        ListBox1.Items.Add(Student(index).ToString())
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        index += 1
        If index > 10 Then
            MessageBox.Show("Unable to insert new values.", "Gradation...", MessageBoxButtons.OK)
            index -= 1
            Exit Sub
        End If

        'Add and show the data
        Student(index).ID = TextBox1.Text
        Student(index).Exam1 = TextBox2.Text
        Student(index).Exam2 = TextBox3.Text
        Student(index).Exam3 = TextBox4.Text
        Student(index).Avgg = CStr(Math.Round((Val(TextBox2.Text) + Val(TextBox3.Text) + Val(TextBox4.Text)) / 3, 0))
        Student(index).LG = Lettergrade(Math.Round((Val(TextBox2.Text) + Val(TextBox3.Text) + Val(TextBox4.Text)) / 3, 0))

        ListBox1.Items.Add(Student(index).ToString())

        TextBox1.Text = Nothing
        TextBox2.Text = Nothing
        TextBox3.Text = Nothing
        TextBox4.Text = Nothing
    End Sub


    Function Lettergrade(ByVal Numgrade As Integer) As String
        Dim Result As String = ""

        If (Numgrade < 70) Then
            Result = "F"
        ElseIf (Numgrade >= 70) And (Numgrade < 80) Then
            Result = "C"
        ElseIf (Numgrade >= 80) And (Numgrade < 90) Then
            Result = "B"
        ElseIf (Numgrade >= 90) Then
            Result = "A"
        End If

        Return Result
    End Function
End Class

suppose it can help you.

commented: Nice example. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.