You are on page 1of 3

Below is a more comprehensive student information program in Visual Basic 2010 (VB 10.0).

This
program allows users to add, edit, delete, and display student information using a simple
Windows Form Application. It includes error handling and basic validation for input fields.

Controls Used:
 3 Labels: Change the Text Property into Name, Age, Grade
 3 TextBox: Change the Name Property into txtName, txtAge, txtGrade
 1 ListBox: Change the Name Property into lstStudents
 4 Buttons: Change the Text Property into Add, Edit, Delete, Display
Change the Name Property into btnAdd, btnEdit, btnDelete,
btnDisplay

CODE:

Public Class Form1

' Creating a structure to hold student information


Structure Student
Dim Name As String
Dim Age As Integer
Dim Grade As String
End Structure

' Creating a list to store multiple students


Dim studentsList As New List(Of Student)
' Function to display all students in the list
Private Sub DisplayStudents()
lstStudents.Items.Clear()
For Each stud As Student In studentsList
lstStudents.Items.Add(stud.Name & " - " & stud.Age & " years - " & stud.Grade)
Next
End Sub

' Function to clear text fields


Private Sub ClearFields()
txtName.Text = ""
txtAge.Text = ""
txtGrade.Text = ""
End Sub

' Function to validate input


Private Function ValidateInput() As Boolean
If String.IsNullOrEmpty(txtName.Text) Or String.IsNullOrEmpty(txtAge.Text) Or
String.IsNullOrEmpty(txtGrade.Text) Then
MessageBox.Show("Please fill in all the fields.")
Return False
End If

Dim age As Integer


If Not Integer.TryParse(txtAge.Text, age) Then
MessageBox.Show("Please enter a valid age.")
Return False
End If

Return True
End Function

' Event handler for the "Save" button


Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
If ValidateInput() Then
' Creating a new student structure and assigning values from input
Dim newStudent As Student
newStudent.Name = txtName.Text
newStudent.Age = Convert.ToInt32(txtAge.Text)
newStudent.Grade = txtGrade.Text

' Adding the new student to the list


studentsList.Add(newStudent)

' Displaying the updated list of students


DisplayStudents()

' Clearing the text boxes after saving


ClearFields()
End If
End Sub

' Event handler for the "Edit" button


Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
If lstStudents.SelectedIndex <> -1 Then
If ValidateInput() Then
' Updating the selected student's information
Dim selectedStudent As Student = studentsList(lstStudents.SelectedIndex)
selectedStudent.Name = txtName.Text
selectedStudent.Age = Convert.ToInt32(txtAge.Text)
selectedStudent.Grade = txtGrade.Text

' Displaying the updated list of students


DisplayStudents()

' Clearing the text boxes after editing


ClearFields()
End If
Else
MessageBox.Show("Please select a student to edit.")
End If
End Sub

' Event handler for the "Delete" button


Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
If lstStudents.SelectedIndex <> -1 Then
' Removing the selected student from the list
studentsList.RemoveAt(lstStudents.SelectedIndex)

' Displaying the updated list of students


DisplayStudents()

' Clearing the text boxes after deleting


ClearFields()
Else
MessageBox.Show("Please select a student to delete.")
End If
End Sub

' Event handler for the "Display" button


Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles
btnDisplay.Click
DisplayStudents()
End Sub
End Class

You might also like