So, our group was assigned a project to calculate the GPA of a student. We are using visual Studios 2010. Anyway, all seems to be good but I just cant figure out how to link the out put the GPA to its corresponding boxes. If you scroll down to the very bottom, you'll see the "private void calculateButton_Click(object sender, EventArgs e" everything below that is where I'm having problems.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GPA_Calculator.Resources;

namespace GPA_Calculator
{
    public partial class Form1 : Form
    {
        private TextBox[] grid = new TextBox[27]; //Contains all the textboxes inside the groupBox1
        public Form1()
        {
            InitializeComponent();
            initializeGrid();
            nameTextBox.Focus();
        }
        /*Makes visible the next set of Text boxes if the user gave a valid input */
        private void makeVisible_TextChanged(object sender, EventArgs e)
        {
            int i = ((TextBox)sender).TabIndex;
            int j = 0;
            //checks if the textbox is not empty and is a valid input
            if (((TextBox)sender).Text != "" && isValidGrade(((TextBox)sender).Text, (TextBox)sender))
            {
                i++;
                while (j < 3)
                {
                    grid[i].Visible = true;
                    j++; i++;
                }
            }
            else
            {
                //((TextBox)sender).Focus();
                ((TextBox)sender).SelectAll();

            }
        }
        /*Returns an Object based on the given index
         * If the index is not found it will return null*/
        private TextBox getTextBox(int index)
        {
            int tab;
            foreach (Control ctrl in gradesBox.Controls)
            {
                if (ctrl is TextBox)
                {
                    tab = ((TextBox)ctrl).TabIndex;
                    if (ctrl is TextBox && tab == index)
                    {
                        return (TextBox)ctrl;
                    }
                }
            }
            return null;
        }
        /*Gets all the Textboxes based on the index and put's them into an ascending array*/
        private void initializeGrid()
        {
            for (int i = 0; i < 27; i++)
            {
                this.grid[i] = getTextBox(i);
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm aAboutForm = new AboutForm();
            aAboutForm.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            closeButton_Click(sender, e);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult responseDialogResult;
            string messageString = "This will open the web browser to access your current GPA." +
                                    " Login into Goldmine and click on Student Records." +
                                    " Click YES to continue or NO to exit.";
            responseDialogResult = MessageBox.Show(messageString,
                "Verify",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            if (responseDialogResult == DialogResult.Yes)
            {
                BrowserForm aBrowserForm = new BrowserForm();
                aBrowserForm.ShowDialog();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            // Disables text boxes for name, class, gpa, hours.
            nameTextBox.Enabled = false;
            classTextBox.Enabled = false;
            gpaTextBox.Enabled = false;
            totalHoursTextBox.Enabled = false;


        }
        /*Clears all the textboxes within the gradesbox groupbox */
        private void clearButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < grid.Length; i++)
            {
                //hides the textboxes for more than 4 courses
                if (i > 11)
                {
                    grid[i].Visible = false;
                }
                grid[i].Text = "";
            }
        }
        /*Checks if the given grade is a valid letter grade */
        private Boolean isValidGrade(String grade, TextBox t)
        {
            switch (grade.ToUpper())//compare the garde with valid cases
            {
                case "A": { return true; }
                case "B": { return true; }
                case "C": { return true; }
                case "D": { return true; }
                case "F": { return true; }
                default://if is not valid show a message error and ask for input again
                    {
                        DialogResult invalid;
                        String s = "Invalid letter grade. Please enter A, B, C, D, or F";
                        invalid = MessageBox.Show(s, "Error", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        return false;
                    }
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Starts new form for a new user.
            clearButton_Click(sender, e);
            nameTextBox.Enabled = true;
            nameTextBox.Clear();
            classTextBox.Enabled = true;
            classTextBox.Clear();
            gpaTextBox.Enabled = true;
            gpaTextBox.Clear();
            totalHoursTextBox.Enabled = true;
            totalHoursTextBox.Clear();
        }

        private void gettingStartedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Concatenate the text for message.
            string instructionString = "First type your Name, Class, Current GPA (if you don't know your GPA click on the question mark), and total hours completed. "
                + "\n" + "Then fill in your Course, Hours, Grade and click on Calculate."
                + "\n\n" + "To clear form, click on the Clear button."
                + "\n\n" + "To start a new form, go to File in the menu bar and click on New.";

            // Display the message box.
            MessageBox.Show(instructionString, "Getting Started.",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            String letterGrade;
            int hours;
            char A, B, C, D, F;

            
           
                switch (letterGrade)
                {
                    case 'A':
                        points = hours * 4;
                        break;
                    case 'B':
                        points = hours * 3;
                        break;
                    case 'C':
                        points = hours * 2;
                        break;
                    case 'D':
                        points = hours * 1;
                        break;
                    default:
                        Console.WriteLine("Either you typed a lower-case letter, or you got an F!");
                        break;
                }//switch




           }
        }
    }
}

Recommended Answers

All 2 Replies

i dont understand how you expect the calculation to work but did you mean to overwrite the points value. should you have maybe added to it or averaged them together...

and also you could change the switch statement to know if you got an 'f' or not

switch (letterGrade)
                {
                    case 'a':
                    case 'A':
                        points = hours * 4;
                        break;
                    case 'b':
                    case 'B':
                        points = hours * 3;
                        break;
                    case 'c':
                    case 'C':
                        points = hours * 2;
                        break;
                    case 'd':
                    case 'D':
                        points = hours * 1;
                        break;
                    default:
                        Console.WriteLine("you got an F!");
                        break;
                }//switch

LetterGrade is a local String variable of your Click method.
It is undefined as far as I can see.
You are using it in a switch statement and testing on characters.
A string is of type string, this is not equal to a char type.

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.