Example #1
0
        }//end

        /// <summary>
        /// Event handler for the Conversion > Base conversion
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == string.Empty)
            {
                MessageBox.Show("Please consider inputting a number before pressing that button.");
            }//end if
            else
            {
                textBox1.Text = BaseConverter.ToDecimal(textBox2.Text, (int)numericUpDown1.Value).ToString();
            }//end else
            //Set ForeColors
            textBox1.ForeColor = Color.Red;
            textBox2.ForeColor = Color.Blue;
        }//end
Example #2
0
        /// <summary>
        /// If buttonToDecimal is clicked, convert the text in textBoxNewBase
        /// into a new value of base 10 in textBoxDecimalVal.
        /// </summary>
        private void buttonToDecimal_Click(object sender, EventArgs e)
        {
            // Ensure that no digit in the textbox is too large for the specified base
            bool isValid = true;

            string toConvert = textBoxNewBase.Text;

            for (int i = 0; i < toConvert.Length && isValid; i++)
            {
                int digit = int.Parse(toConvert[i].ToString(), NumberStyles.HexNumber);

                if (digit > numericUpDownBase.Value - 1)
                {
                    isValid = false;
                }
            }

            // Convert the number to decimal
            if (isValid)
            {
                try
                {
                    this.textBoxDecimalVal.Text =
                        BaseConverter.ToDecimal(textBoxNewBase.Text.TrimStart('0'),
                                                (int)numericUpDownBase.Value,
                                                (int)numericUpDownDigits.Value).ToString();
                }
                catch (OverflowException)
                {
                    MessageBox.Show("The base " + numericUpDownBase.Value +
                                    " number is too large for an integer!");
                }
            }
            // Else display an error if the input was not valid
            else
            {
                MessageBox.Show("Invalid number of base " + numericUpDownBase.Value);
            }
        }