Lab Manual: GPA Calculator in C#
Objective: Build a GPA Calculator using C# and Windows Forms. Understand event handling, data
validation, and working with DataGridView.
Pre-requisites:
Basic knowledge of C# programming.
Visual Studio installed on your machine.
Lab Task 1: Setting Up the Project
Open Visual Studio and create a new Windows Forms Application project.
Name the project "GPACalculatorSE5" and create the main form (Form1).
Lab Task 2: Designing the UI
Drag and drop the required controls onto Form1:
TextBox (textBox1) for Name
ComboBox (comboBox1) for Credit Hours
TextBox (textBox2) for Marks
TextBox (textBox3) for Grade (set ReadOnly property to true)
TextBox (textBox4) for Points (set ReadOnly property to true)
Button (button1) for calculating and adding the result to the DataGridView
DataGridView (dataGridView1) to display the results
Button (button2) to calculate and display the GPA
Lab Task 3: Implementing GPA Calculation Logic
Double-click on the textBox2 control to create an event handler for the TextChanged event.
Implement the GPA calculation logic inside the event handler based on the provided code. Make sure
to handle different credit hours.
int marks;
int credithours = Convert.ToInt32 (comboBox1.Text);
bool flag = int.TryParse(textBox2.Text,out marks);
if (flag == true)
{
if (credithours == 1)
{
if (marks >= 16 && marks <= 20)
{
textBox4.Text = "4.0";
textBox3.Text = "A";
}
else if (marks == 15)
{
textBox4.Text = "3.7";
textBox3.Text = "B";
}
else if (marks == 14)
{
textBox4.Text = "3.3";
textBox3.Text = "B";
}
else if (marks == 13)
{
textBox4.Text = "3.0";
}
else if (marks == 12)
{
textBox4.Text = "2.7";
}
else if (marks == 11)
{
textBox4.Text = "2.3";
}
else if (marks == 10)
{
textBox4.Text = "2.0";
}
else if (marks == 9)
{
textBox4.Text = "1.5";
}
else if (marks == 8)
{
textBox4.Text = "1.0";
}
else
{
textBox4.Text = "0.0";
}
}
else if (credithours == 2)
{
}
else if (credithours == 3)
{
Double-click on the button1 control to create an event handler for the Click event.
Implement the logic to add the entered data to the DataGridView.
string Name = textBox1.Text;
string Crdhours = comboBox1.Text;
string Marks = textBox2.Text;
string Grade = textBox3.Text;
string Points= textBox4.Text;
string[] row = { Name, Crdhours,Marks,Grade,Points };
dataGridView1.Rows.Add(row);
Lab Task 4: Implementing Total GPA Calculation
Double-click on the button2 control to create an event handler for the Click event.
Implement the logic to calculate the total GPA by iterating through the DataGridView rows.
double points=0.0;
int tcrdhrs=0;
for(int i=0;i<dataGridView1.Rows.Count-1;i++)
{
tcrdhrs += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
points += Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
}
label6.Text=Convert.ToString(points / tcrdhrs);
Lab Task 5: Testing the Application
Run the application and test the GPA calculator functionality.
Enter various sets of data to check if the GPA calculation and DataGridView updates are working
correctly.