Example #1
0
        /// <summary>
        /// Updata a selected item in listbox
        /// check the reasonablility of the input data,if rules are violated, warn the user.
        /// </summary>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int selected = lstOutoput.SelectedIndex;

            if (txtSelect.Text.Length > 0)
            {
                if (txtSelect.Text.Length == 0)
                {
                    MessageBox.Show("Please enter a valid data", "Error");
                }
                else if (txtSelect.Text.Length == 1)
                {
                    if (txtSelect.Text.Contains("#") || txtSelect.Text.Contains("="))
                    {
                        CalcLine calcline = new CalcLine(this.txtSelect.Text);
                        calculation.Replace(calcline, selected);
                        txtSelect.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Incorrect data format", "Invalid Data Format");
                    }
                }
                else
                {
                    char ch  = txtSelect.Text[0];                         //first character in the text box
                    char ch1 = txtSelect.Text[1];                         //second character,numeric
                    char ch2 = txtSelect.Text[txtSelect.Text.Length - 1]; //last charac, +-*/=#
                    if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                    {
                        if (((ch1 >= (char)48) && (ch1 <= (char)57)) && ((ch2 >= (char)48) && (ch2 <= (char)57)))
                        {
                            CalcLine calcline = new CalcLine(this.txtSelect.Text);
                            calculation.Replace(calcline, selected);
                            txtSelect.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Incorrect data format", "Invalid Data Format");
                    }
                }
            }
            else
            {
                MessageBox.Show("Please enter valid value.");
            }
        }
Example #2
0
        /// <summary>
        /// delete a selected item in listbox, give warning before deleting.
        /// </summary>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            int selected = lstOutoput.SelectedIndex;

            if (txtSelect.Text != null)
            {
                CalcLine calcline = new CalcLine(this.txtSelect.Text);
                if (MessageBox.Show("Do you want to delete this item?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    calculation.Delete(selected);
                    txtSelect.Text = "";
                }
                else
                {
                    return;
                }
            }
        }
Example #3
0
 /// <summary> method Replace
 /// Replace the nth CalcLine object in the ArrayList with the newCalc object, and then redisplay the calculations.
 /// </summary>
 public void Replace(CalcLine newCalc, int n)
 {
     this.theCalcs[n] = newCalc;
     Redisplay();
     lstDisplay.SelectedIndex = n;
 }
Example #4
0
 /// <summary> method Add
 /// Add a CalcLine object to the ArrayList then redisplay the calculations.
 /// </summary>
 public void Add(CalcLine cl)
 {
     this.theCalcs.Add(cl);
     Redisplay();
 }
Example #5
0
 /// <summary> method Insert
 /// Insert the newCalc CalcLine object in the ArrayList immediately before the nth object, and then redisplay the
 /// calculations.
 /// </summary>
 public void Insert(CalcLine newCalc, int n)
 {
     this.theCalcs.Insert(n, newCalc);
     Redisplay();
     lstDisplay.SelectedIndex = n;
 }
Example #6
0
        /// <summary>
        /// If enter is pressed and the add calculation textbox is empty,
        /// send notice to user to enter a calculation.
        /// </summary>
        private void txtEnter_KeyUp(object sender, KeyEventArgs e)
        {
            string txt = txtEnter.Text;

            if (lstOutoput.Items.Count == 0)
            {
                if (txt.Length == 0)//enter key is not allowed if no calculation have begun
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        MessageBox.Show("No calculations have begun,Enter key is not allowed", "Invalid Request");
                    }
                    else
                    {
                        return;
                    }
                }
                else if (txt.Length == 1)
                {
                    if (txt[0].ToString() == "#" || txt[0].ToString() == "=")//no total or subtotal for the first line.
                    {
                        MessageBox.Show("No calculation available. \n So no subtotal or total can be displayed", "Invalid Request");
                        txtEnter.Text = "";
                    }
                    else if (!(txt[0].ToString() == "+" || txt[0].ToString() == "-"))//first character must be "+" or "-" for the first line
                    {
                        MessageBox.Show("First data entry must be either \'+\' or \'-\' as the beginning operator", "Invalid First Data Entry");
                        txtEnter.Text = "";
                    }
                }
                else
                {
                    if (txt[1] >= (char)48 && txt[1] <= (char)57)//numeric after an operator
                    {
                        //to decide if last one is a valid operator ,then add the front data to listbox
                        char ch = txt[txt.Length - 1];
                        if (ch == '#' || ch == '=' || e.KeyCode == Keys.Enter)//# = enter
                        {
                            if (e.KeyCode == Keys.Enter)
                            {
                                calculation.Add(new CalcLine(txt.Substring(0, txt.Length)));
                            }
                            else
                            {
                                calculation.Add(new CalcLine(txt.Substring(0, txt.Length - 1)));
                            }
                            if (ch == '=' || e.KeyCode == Keys.Enter)
                            {
                                calculation.Add(new CalcLine("="));
                            }
                            else if (ch == '#')
                            {
                                calculation.Add(new CalcLine("#"));
                            }
                            txtEnter.Text = "";
                        }
                        else if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))//add the front data to listbox
                        {
                            CalcLine calcline = new CalcLine(txt.Substring(0, txt.Length - 1));
                            calculation.Add(calcline);
                            txtEnter.Text = txt[txt.Length - 1].ToString();
                            txtEnter.Focus();
                            txtEnter.SelectionStart = txtEnter.Text.Length;
                        }
                        else if (!(ch >= (char)48) && (ch <= (char)57))//numeric
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                            txtEnter.Text = "";
                        }
                    }
                    else
                    {
                        MessageBox.Show("Incorrect data format", "Invalid Data Format");
                        txtEnter.Text = "";
                    }
                }
            }
            //lstOutoput.Items.Count > 0
            else
            {
                //if last item in the list box is total,then consider as listbox is empty
                if (lstOutoput.Items[lstOutoput.Items.Count - 1].ToString().Contains("="))
                {
                    if (txt.Length == 0)
                    {
                        return;
                    }
                    else if (txt.Length == 1)
                    {
                        if (txt[0].ToString() == "#" || txt[0].ToString() == "=")
                        {
                            MessageBox.Show("No calculation available. \n So no subtotal or total can be displayed", "Invalid Request");
                            txtEnter.Text = "";
                        }
                        else if (!(txt[0].ToString() == "+" || txt[0].ToString() == "-"))
                        {
                            MessageBox.Show("First data entry must be either \'+\' or \'-\' as the beginning operator", "Invalid First Data Entry");
                            txtEnter.Text = "";
                        }
                    }
                    else
                    {
                        char ch = txt[txt.Length - 1];
                        if (txt[1] >= (char)48 && txt[1] <= (char)57)
                        {
                            if (ch == '#' || ch == '=' || e.KeyCode == Keys.Enter)//# =
                            {
                                if (e.KeyCode == Keys.Enter)
                                {
                                    calculation.Add(new CalcLine(txt.Substring(0, txt.Length)));
                                }
                                else
                                {
                                    calculation.Add(new CalcLine(txt.Substring(0, txt.Length - 1)));
                                }
                                if (ch == '=' || e.KeyCode == Keys.Enter)
                                {
                                    calculation.Add(new CalcLine("="));
                                }
                                else if (ch == '#')
                                {
                                    calculation.Add(new CalcLine("#"));
                                }
                                txtEnter.Text = "";
                            }
                            else if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))
                            {
                                CalcLine calcline = new CalcLine(txt.Substring(0, txt.Length - 1));
                                calculation.Add(calcline);
                                txtEnter.Text = txt[txt.Length - 1].ToString();
                                txtEnter.Focus();
                                txtEnter.SelectionStart = txtEnter.Text.Length;
                            }
                            else if (!(ch >= (char)48) && (ch <= (char)57))
                            {
                                MessageBox.Show("Incorrect data format", "Invalid Data Format");
                                txtEnter.Text = "";
                            }
                        }
                        else
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                            txtEnter.Text = "";
                        }
                    }
                }
                else
                {
                    if (txt.Length == 0)
                    {
                        return;
                    }
                    else if (txt.Length == 1)
                    {
                        if (!(txt[0].ToString() == "#" || txt[0].ToString() == "=" || txt[0].ToString() == "+" || txt[0].ToString() == "-" || txt[0].ToString() == "*" || txt[0].ToString() == "/" || e.KeyCode == Keys.Enter))
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                            txtEnter.Text = "";
                        }
                    }
                    else
                    {
                        if (txt[1] >= (char)48 && txt[1] <= (char)57)
                        {
                            char ch = txt[txt.Length - 1];
                            if ((ch == '#') || (ch == '=') || e.KeyCode == Keys.Enter)//# =
                            {
                                if (e.KeyCode == Keys.Enter)
                                {
                                    calculation.Add(new CalcLine(txt.Substring(0, txt.Length)));
                                }
                                else
                                {
                                    calculation.Add(new CalcLine(txt.Substring(0, txt.Length - 1)));
                                }
                                if (ch == '=' || e.KeyCode == Keys.Enter)
                                {
                                    calculation.Add(new CalcLine("="));
                                }
                                else if (ch == '#')
                                {
                                    calculation.Add(new CalcLine("#"));
                                }
                                txtEnter.Text = "";
                            }
                            else if ((ch == '+') || (ch == '-') || (ch == '*') || (ch == '/'))
                            {
                                CalcLine calcline = new CalcLine(txt.Substring(0, txt.Length - 1));
                                calculation.Add(calcline);
                                txtEnter.Text = txt[txt.Length - 1].ToString();
                                txtEnter.Focus();
                                txtEnter.SelectionStart = txtEnter.Text.Length;
                            }
                            else if (!(ch >= (char)48) && (ch <= (char)57))
                            {
                                MessageBox.Show("Incorrect data format", "Invalid Data Format");
                                txtEnter.Text = "";
                            }
                        }
                        else
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                            txtEnter.Text = "";
                        }
                    }
                }
            }
        }
Example #7
0
        /// <summary>
        /// Insert a selected calculation line.
        /// Update according to the calculation rules
        /// If rules are violated, warn the user.
        /// </summary>
        private void btnInsert_Click(object sender, EventArgs e)
        {
            int selected = lstOutoput.SelectedIndex;

            if (selected == 0)//if the insert line will be the first line
            {
                if (txtSelect.Text.Length == 0)
                {
                    MessageBox.Show("Please enter a valid data", "Error");
                }
                else if (txtSelect.Text.Length == 1)
                {
                    if (txtSelect.Text.Contains("#") || txtSelect.Text.Contains("="))
                    {
                        CalcLine calcline = new CalcLine(this.txtSelect.Text);
                        calculation.Insert(calcline, selected);
                        txtSelect.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Incorrect data format", "Invalid Data Format");
                    }
                }
                else
                {
                    char ch  = txtSelect.Text[0];
                    char ch1 = txtSelect.Text[1];
                    char ch2 = txtSelect.Text[txtSelect.Text.Length - 1];
                    if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                    {
                        if (((ch1 >= (char)48) && (ch1 <= (char)57)) && ((ch2 >= (char)48) && (ch2 <= (char)57)))
                        {
                            CalcLine calcline = new CalcLine(this.txtSelect.Text);
                            calculation.Insert(calcline, selected);
                            txtSelect.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Incorrect data format", "Invalid Data Format");
                    }
                }
            }
            else
            {
                if (lstOutoput.Items[selected - 1].ToString().Contains("="))//decide if the item before selected item is total,then obry the rule of first charac.
                {
                    if (txtSelect.Text.Length == 0)
                    {
                        MessageBox.Show("Please enter a valid data", "Error");
                    }
                    else if (txtSelect.Text.Length == 1)
                    {
                        if (txtSelect.Text[0] != '#' || txtSelect.Text[0] != '=')
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                        }
                        else
                        {
                            CalcLine calcline = new CalcLine(this.txtSelect.Text);
                            calculation.Insert(calcline, selected);
                            txtSelect.Text = "";
                        }
                    }
                    else
                    {
                        if (!(txtSelect.Text[0] == '+' || txtSelect.Text[0] == '-'))
                        {
                            MessageBox.Show("First data entry must be either \'+\' or \'-\' as the beginning operator", "Invalid First Data Entry");
                            txtEnter.Text = "";
                        }
                        else
                        {
                            char ch1 = txtSelect.Text[1];
                            char ch2 = txtSelect.Text[txtSelect.Text.Length - 1];
                            if (((ch1 >= (char)48) && (ch1 <= (char)57)) && ((ch2 >= (char)48) && (ch2 <= (char)57)))
                            {
                                CalcLine calcline = new CalcLine(this.txtSelect.Text);
                                calculation.Insert(calcline, selected);
                                txtSelect.Text = "";
                            }
                            else
                            {
                                MessageBox.Show("Incorrect data format", "Invalid Data Format");
                            }
                        }
                    }
                }
                else//decide if input resonable
                {
                    if (txtSelect.Text.Length == 0)
                    {
                        MessageBox.Show("Please enter a valid data", "Error");
                    }
                    else if (txtSelect.Text.Length == 1)
                    {
                        if (txtSelect.Text[0] == '#' || txtSelect.Text[0] == '+')
                        {
                            CalcLine calcline = new CalcLine(this.txtSelect.Text);
                            calculation.Insert(calcline, selected);
                            txtSelect.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                            txtSelect.Text = "";
                        }
                    }
                    else if (txtSelect.Text[0] == '+' || txtSelect.Text[0] == '-' || txtSelect.Text[0] == '*' || txtSelect.Text[0] == '/')
                    {
                        char ch  = txtSelect.Text[0];
                        char ch1 = txtSelect.Text[1];
                        char ch2 = txtSelect.Text[txtSelect.Text.Length - 1];
                        if (((ch1 >= (char)48) && (ch1 <= (char)57)) && ((ch2 >= (char)48) && (ch2 <= (char)57)))
                        {
                            CalcLine calcline = new CalcLine(this.txtSelect.Text);
                            calculation.Insert(calcline, selected);
                            txtSelect.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("Incorrect data format", "Invalid Data Format");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Incorrect data format", "Invalid Data Format");
                    }
                }
            }
        }