How to build a CGPA Calculator in C# using Window Forms ?
Building A CGPA Calculator in C# using Window Form:
Hello Friends! In this article we will learn that how to build or make a CGPA Calculator in C# using Window Forms. This will help you to get motivated to become a c# developer. So our output will be like this:
CGPA Calculator Final Interface |
So this will be our final CGPA Calculator screenshot. Lets start to build this:
Designing The Interface using Window Forms:
So to design the interface i used labels, dropdown box, numeric updown, buttons and some background pictures to make my design beautiful and user friendly. Here I also uses grading criteria labeled as "Grading Values" so that user of this application can easily understand the criteria to calculate CGPA. So here is the screenshot of different parts of this interface:
So this is the interface design let's try to code.
CGPA Calculator Code in C#:
Coding this CGPA calculator is not difficult but need to understand the basics of Programming. Here is the code for this CGPA calculator:
using System;
using System.Windows.Forms;
namespace TR_CGPA_Calculator
{
public partial class Form1 : Form
{
//declaring varibales
String lettergrd;
double credit;
double caltimes = 0;
double totalcal = 0;
double totalcredit = 0;
double finalgpa = 0;
double A = 4.00;
double AMINUS = 3.70;
double BPLUS = 3.40;
double B = 3.0;
double BMINUS = 2.50;
double CPLUS = 2.00;
double C = 1.50;
double D = 1.00;
double F = 0.0;
double points=0;
string crc ;
string tc ;
double tp = 0;
double tcre = 0;
double finalcgpa = 0;
//declaring combobox object
ComboBox defaultcombo = new ComboBox();
//declaring NumericUpDown object
NumericUpDown defaultnumeric = new NumericUpDown();
//declaring TextBox Object
TextBox crrcgpa = new TextBox();
TextBox tchr = new TextBox();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//event that will occur when Calculate GPA button is clicked
private void btncalculategpa_Click(object sender, EventArgs e)
{
try
{
//sound a beep
Console.Beep(650, 700);
lettergrd = "";
credit = 0;
caltimes = 0;
totalcal = 0;
totalcredit = 0;
finalgpa = 0;
defaultcombo = new ComboBox();
defaultnumeric = new NumericUpDown();
points = 0;
crrcgpa = new TextBox();
tchr = new TextBox();
for (int i = 0; i < 10; i++)
{
switch (i)
{
case 1:
defaultcombo = cmbCourse1;
defaultnumeric = numCourse1;
break;
case 2:
defaultcombo = cmbCourse2;
defaultnumeric = numCourse2;
break;
case 3:
defaultcombo = cmbCourse3;
defaultnumeric = numCourse3;
break;
case 4:
defaultcombo = cmbCourse4;
defaultnumeric = numCourse4;
break;
case 5:
defaultcombo = cmbCourse5;
defaultnumeric = numCourse5;
break;
case 6:
defaultcombo = cmbCourse6;
defaultnumeric = numCourse6;
break;
case 7:
defaultcombo = cmbCourse7;
defaultnumeric = numCourse7;
break;
case 8:
defaultcombo = cmbcourse8;
defaultnumeric = numcourse8;
break;
case 9:
defaultcombo = cmbcourse9;
defaultnumeric = numcourse9;
break;
case 10:
defaultcombo = cmbcourse10;
defaultnumeric = numcourse10;
break;
}
lettergrd = defaultcombo.Text;
credit = Double.Parse(defaultnumeric.Value.ToString());
if (lettergrd != String.Empty && credit != 0)
{
switch (lettergrd)
{
case "A+": caltimes = credit * A;
break;
case "A": caltimes = credit * AMINUS;
break;
case "B+": caltimes = credit * BPLUS;
break;
case "B": caltimes = credit * B;
break;
case "B-": caltimes = credit * BMINUS;
break;
case "C+": caltimes = credit * CPLUS;
break;
case "C": caltimes = credit * C;
break;
//case "C-": caltimes = credit * CMINUS;
// break;
case "D": caltimes = credit * D;
break;
case "F": caltimes = credit * F;
break;
}
totalcredit = totalcredit + credit;
totalcal = totalcal + caltimes;
}
}
// calcuylating gpa
finalgpa = totalcal / totalcredit;
lblgpa.Text = finalgpa.ToString("#.00");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//resetting all the textboxes
private void btnreset_Click(object sender, EventArgs e)
{
try
{
Console.Beep(650, 700);
cmbCourse1.ResetText();
cmbCourse2.ResetText();
cmbCourse3.ResetText();
cmbCourse4.ResetText();
cmbCourse5.ResetText();
cmbCourse6.ResetText();
cmbCourse7.ResetText();
numCourse1.ResetText();
numCourse2.ResetText();
numCourse3.ResetText();
numCourse4.ResetText();
numCourse5.ResetText();
numCourse6.ResetText();
numCourse7.ResetText();
ccgpa.ResetText();
tch.ResetText();
lblgpa.ResetText();
lblcgpa.ResetText();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//calculating the CGPA
private void btnnew_Click(object sender, EventArgs e)
{
try
{
Console.Beep(650, 700);
crc = ccgpa.Text;
tc = tch.Text;
double crcg = Convert.ToDouble(crc);
double tcr = Convert.ToDouble(tc);
points = crcg * tcr;
tp = points + totalcal;
tcre = tcr + totalcredit;
finalcgpa = tp / tcre;
lblcgpa.Text = finalcgpa.ToString("#.00");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
So this is code for CGPA calculator. Note: Here I have used one criteria that I have mentioned previously you can use your own criteria enjoy you own CGPA Calculator application.
Here is the video for further help:
🔀 Please post your comments and share this article and also give your suggestion.
Comments
Post a Comment