Beispiel #1
0
        /// <summary> method btnInsert_Click
        /// When user clickes on "Insert" button, validate the data in the lower textbox and insert a new line in the calculation
        /// immediately before the selected line.
        /// </summary>
        private void btnInsert_Click(object sender, EventArgs e)
        {
            string insertStr = txtSelect.Text;
            int    n         = lstDisplay.SelectedIndex;

            validCalcuLine = true;

            // If the selected line in the listbox is the first line, mark the firstline.
            if (n == 0)
            {
                firstLine = true;
            }
            else
            {
                firstLine = false;
            }

            // Validate the data in the lower textbox.
            CalcLineInvalidation(insertStr, insertStr.Length);

            // If the data in the lower textbox is valid, insert a new line in the calculation immediately before the selected line.
            if (validCalcuLine)
            {
                currentCalcLine = new CalcLine(insertStr);
                theCalculation.Insert(currentCalcLine, n);
                lstDisplay.SelectedIndex = n;
            }
        }
        /// <summary> method LoadFromFile
        /// Clear the ArrayList and then open the given file and convert the lines of the file
        /// to a set of CalcLine objects held in the ArrayList. Then redisplay the calculations.
        /// Return a bool variable giving the result.
        /// </summary>
        public bool LoadFromFile(string filename)
        {
            // Create a new StreamReader object to read data from the given file
            StreamReader sr   = new StreamReader(filename);
            string       temp = sr.ReadLine();

            // Clear the ArrayList
            theCalcs.Clear();

            try
            {
                // If current line is not null, creat a new CalcLine object from current line and
                // add the CalcLine object to ArrayList. Then read next line.
                while (temp != null)
                {
                    CalcLine cl = new CalcLine(temp);
                    theCalcs.Add(cl);
                    temp = sr.ReadLine();
                }

                // Redisplay the listbox with latest ArrayList and return true.
                Redisplay();
                calcFilename = filename;
                return(true);
            }
            catch (Exception)
            {
                // If an exception occur, return false;
                return(false);
            }
            finally
            {
                sr.Close(); // Close the StreamReader object at the end of the method.
            }
        }
Beispiel #3
0
        /// <summary> method btnUpdate_Click
        /// When user clickes on "Update" button, validate the data in the lower textbox and update the Calculation object and the listbox.
        /// </summary>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string selectStr = txtSelect.Text;
            int    n         = lstDisplay.SelectedIndex;

            validCalcuLine = true;

            // If the selected line in the listbox is the first line, mark the firstline.
            if (n == 0)
            {
                firstLine = true;
            }
            else
            {
                firstLine = false;
            }

            // Validate the data in the lower textbox.
            CalcLineInvalidation(selectStr, selectStr.Length);

            // If the data in the lower textbox is valid, update the Calculation object and the listbox.
            if (validCalcuLine)
            {
                currentCalcLine = new CalcLine(txtSelect.Text);
                theCalculation.Replace(currentCalcLine, n);
                lstDisplay.SelectedIndex = n;
            }
        }
Beispiel #4
0
        /// <summary>
        /// method : txt_KeyPress
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="KeyPressEventArgs"/> instance containing the event data.</param>
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            // If they press the backspace let them.
            if (e.KeyChar == '\b')
            {
                return;
            }
            CalcLine calcLine = null;

            // 1st char in the line
            if (txtValue.Text.Length == 0)
            {
                if (e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '\n')
                {
                }
                else
                {
                    e.Handled = true;
                    MessageBox.Show("You must enter a +, - or the Enter key as the first value", "Error");
                }
            }
            else
            {
                // Not the first char
                // Is it a terminating char
                if (e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '*' || e.KeyChar == '/' || e.KeyChar == '#' || e.KeyChar == '=' || e.KeyChar == '\r')
                {
                    changed  = true;
                    calcLine = new CalcLine(txtValue.Text);
                    calculation.Add(calcLine);
                    // Is is a char that needs to go to the start of the line
                    if (e.KeyChar == '+' || e.KeyChar == '-' || e.KeyChar == '*' || e.KeyChar == '/')
                    {
                        txtValue.Text = e.KeyChar.ToString();
                        txtValue.Select(1, 1);
                        e.Handled = true;
                        return;
                    } // Is it a subtotal
                    if (e.KeyChar == '#')
                    {
                        calcLine = new CalcLine(Operator.subtotal);
                        calculation.Add(calcLine);
                    } // Is it a total
                    else if (e.KeyChar == '=')
                    {
                        calcLine = new CalcLine(Operator.total);
                        calculation.Add(calcLine);
                    }
                    txtValue.Text = "";
                }
                else if (!char.IsDigit(e.KeyChar))
                {
                    e.Handled = true;
                    MessageBox.Show("You must enter a digit or #, +, -, *. /, or =", "Error");
                }
            }
        }
Beispiel #5
0
        // Clear the ArrayList
        //Then open the given file and convert lines of the file to a set of CalcLine objects held in ArrayList
        // Redisplay Calculations
        public void LoadFromFile(string filename)
        {
            Clear();
            StreamReader sr   = new StreamReader(filename);
            string       temp = sr.ReadLine();

            while (temp != null)
            {
                var calc = new CalcLine(temp);
                Add(calc);
                temp = sr.ReadLine();
            }
            sr.Close();
        }
        public void AddExercise(Exercise exercise, Calculation calculation, Plate plate)
        {
            CalcLine line = lineCollection
                            .Where(c => c.Exercise.ExerciseID == exercise.ExerciseID)
                            .FirstOrDefault();

            if (line == null)
            {
                lineCollection.RemoveAll(e => e.Exercise.ExerciseID != exercise.ExerciseID);
                lineCollection.Add(new CalcLine
                {
                    Exercise    = exercise,
                    Calculation = calculation,
                    Plate       = plate
                });
            }
        }
Beispiel #7
0
        /// <summary> method AddCalcLine
        /// Convert the text in the upper textbox into a CalcLine object and add the object to the Calculation object.
        /// </summary>
        private void AddCalcLine(char ch)
        {
            currentCalcLine = new CalcLine(txtInput.Text);
            theCalculation.Add(currentCalcLine);
            txtInput.Text = "";

            // If the character ch is "#" or "=", create a new CalcLine object from ch and add to the Calculation object.
            if ((ch == '#') || (ch == '='))
            {
                currentCalcLine = new CalcLine(ch.ToString());
                theCalculation.Add(currentCalcLine);
            }

            // If the character ch is ENTER, create a new CalcLine object from "=" and add to the Calculation object.
            else if (ch == 13)
            {
                currentCalcLine = new CalcLine("=");
                theCalculation.Add(currentCalcLine);
            }
        }
Beispiel #8
0
        /// <summary>
        /// method : btnInsert_Click
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnInsert_Click(object sender, EventArgs e)
        {
            var calcLine  = txtReplacementValue.Text;
            var firstChar = calcLine[0];

            if (firstChar == '+' || firstChar == '-')
            {
                var secondChar = calcLine[1];
                if (char.IsDigit(secondChar))
                {
                    var calc = new CalcLine(calcLine);
                    calculation.Insert(calc, lstCalculationLines.SelectedIndex + 1);
                }
                else
                {
                    MessageBox.Show("The characters immediately following the first character must be numeric", "Error");
                }
            }
            else
            {
                MessageBox.Show("The first character must be a + or - ", "Error");
            }
        }
 /// <summary> method Add
 /// Add a CalcLine object to the ArrayList then redisplay the calculations, and mark the
 /// Calculation as modified.
 /// </summary>
 public void Add(CalcLine cl)
 {
     theCalcs.Add(cl);
     Redisplay();
     isModified = true;
 }
 /// <summary> method Insert
 /// Insert the newCalc CalcLine object in the ArrayList immediately before the nth object, and then
 /// redisplay the calculations.
 /// Mark the caculation as modified
 /// </summary>
 public void Insert(CalcLine newCalc, int n)
 {
     theCalcs.Insert(n, newCalc);
     Redisplay();
     isModified = true;
 }
 /// <summary> method Replace
 /// Replace the nth CalcLine object in the ArrayList with the newCalc object passed by parameter,
 /// and then redisplay the calculations.
 /// Mark the caculation as modified
 /// </summary>
 public void Replace(CalcLine newCalc, int n)
 {
     theCalcs[n] = newCalc;
     Redisplay();
     isModified = true;
 }
Beispiel #12
0
 /// <summary>
 ///     method : Replace
 ///     Repalce the nth CalcLine object in the ArrayList wwith new calc object
 ///     Redisplay the calculations
 /// </summary>
 /// <param name="newCalc">The new calculate.</param>
 /// <param name="n">The n.</param>
 public void Replace(CalcLine newCalc, int n)
 {
     theCalcs[n] = newCalc;
     Redisplay();
 }
Beispiel #13
0
 /// <summary>
 ///     method : Add
 ///     Clear the ArrayList and the ListBox
 /// </summary>
 /// <param name="cl">The cl.</param>
 public void Add(CalcLine cl)
 {
     theCalcs.Add(cl);
     Redisplay();
 }
Beispiel #14
0
 /// <summary>
 ///     method : Insert
 ///     Insert new CalcLine object ArrayList immediately before the nth object
 ///     Redisplay the claculations
 /// </summary>
 /// <param name="newCalc">The new calculate.</param>
 /// <param name="n">The n.</param>
 public void Insert(CalcLine newCalc, int n)
 {
     theCalcs.Insert(n - 1, newCalc);
     Redisplay();
 }