Friday, May 9, 2014

Simple program that gets the sum of the numbers 1 to N

This is perfect for those who need a refresher...
**Note: I'm doing these in Visual Studio Express 2013

 The formula for completing this task

This formula is pretty straight-forward and translate to: [ maxNum * (maxNum)]/2 = [(maxNum)2]/2 + maxNum/2
Requirements for this program

  • A TextBox (txtMaxNum)
  • A label (lblMaxNum)
  • Another label (lblSum)
  • A button (btnSum)
  • (everything else is optional)
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_Playground
{
    public partial class Form1 : Form
    {
        private int maxNum;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            resetTextBox(txtMaxNum, "Insert the Maximum Number");
            lblMaxNum.Text = "(Nothing yet)";
        }

        private void txtMaxNum_Enter(object sender, EventArgs e)
        {
            //Make sure the textbox is empty and ready for typing
            insertTextMode(txtMaxNum);
            lblMaxNum.Text = "Typing...";
        }

        /*
       * Reset the textbox so that there is a default value already
       * inside then clears the textbox
       */
        public void resetTextBox(TextBox textbox, string defaultText)
        {

            textbox.Clear();//Makes sure there is nothing in there
            textbox.ForeColor = Color.Gray; //Gives the font a grayish color
            textbox.Text = defaultText;

        }

        /*
         * Changes the font color for text insertion
         */
        public void insertTextMode(TextBox t)
        {
            t.Clear();
            t.ForeColor = Color.Black;
            t.BackColor = Color.White;
        }

        public int sum(int max){
            int sum = (max * (1 + max) / 2); //Algorithm for getting the sum of the 
                                             //numbers 1 + 2 + 3 + ... + n
            return sum;
        }

        public void warning(string text) { 
            System.Windows.Forms.MessageBox.Show(text, "Error", MessageBoxButtons.OK ,MessageBoxIcon.Error);
            //This is a long statement
        }

        private void btnSum_Click(object sender, EventArgs e)
        {
            /*
             * Even if you don't enter anything in the textbox, it will still count the length
             * of the default text which is what we want to prevent.
             */
            if (txtMaxNum.Text.Length > 0 && txtMaxNum.Text != "Insert the Maximum Number")
            {
               //Now we need to make sure that a we are working with integers
               if(txtMaxNum.Text.All(char.IsDigit)){
                   lblMaxNum.Text = txtMaxNum.Text;

                   maxNum = Int32.Parse(txtMaxNum.Text);
                   lblSum.Text = "1 + 2 + 3 + ....... + " + maxNum + " is " + sum(maxNum);
                   resetTextBox(txtMaxNum, "Want to do another one?");
               }else{
                   //If someone decides to add some letters anyway they will be stopped by this error message
                    warning("Why are you goofing around? You know we're only working with integers");
                    lblMaxNum.Text = "Try again";
               }
                
            }
            else{
                txtMaxNum.BackColor = Color.Red;
                txtMaxNum.ForeColor = Color.White;
                txtMaxNum.Text = "Please enter a Number";
                lblMaxNum.Text = "Try again";

                warning("Sum calculations cannot take place with an empty textbox. Please specify a maximum number");

                if (txtMaxNum.Focused == true) {
                    insertTextMode(txtMaxNum);
                }

            }
        }

    }
}


Here are the images that I have (I should have a demonstration video up and going in the next couple of days).





No comments:

Post a Comment