protected void Page_Load(object sender, EventArgs e)
        {
            // configure the TextBoxes based on the property value
            foreach (TextBox textBox in new [] { LapsText, LengthText, MinText, CalsText })
            {
                textBox.AutoPostBack = EnableTextBoxAutoPostBack;
            }

            int laps, length, mins, cals;

            if (int.TryParse(LapsText.Text, out laps) &&
                int.TryParse(LengthText.Text, out length) &&
                int.TryParse(MinText.Text, out mins) &&
                int.TryParse(CalsText.Text, out cals))
            {
                float[] newinputs = new float[] { laps, length, mins, cals };
                // perform the calculations only if one of the inputs has changed
                if (lastInputs == null || !lastInputs.SequenceEqual(newinputs))
                {
                    SwimCalcResult result = SwimCalculator.PerformCalculation(laps, length, mins, cals);

                    // invoke the event
                    OnCalcPerformed(new SwimCalcEventArgs(result));
                    lastInputs = newinputs;
                }
            }
        }
Esempio n. 2
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            int laps, length, mins, cals;

            if (int.TryParse(TextBox1.Text, out laps) &&
                int.TryParse(TextBox2.Text, out length) &&
                int.TryParse(TextBox3.Text, out mins) &&
                int.TryParse(TextBox4.Text, out cals))
            {
                SwimCalcResult calcResult = SwimCalculator.PerformCalculation(laps, length, mins, cals);

                // compose the resuls
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat("Distance: {0:F2} miles\n", calcResult.Distance);
                stringBuilder.AppendFormat("Calories Burned: {0:F0}\n", calcResult.Calories);
                stringBuilder.AppendFormat("Pace: {0:F0} sec/lap \n", calcResult.Pace);

                // set the results text
                TextBox5.Text = stringBuilder.ToString();
            }
            else
            {
                TextBox5.Text = "";
            }
        }