-1

I am doing a new integer box and I want it to accept only integer numbers and the exponential E. I know that do the Exponential E you have to do Math.Exp(x) but I can't seem to find the right place to put it. Could someone help me please?

namespace IntegerBox
{
    public partial class IntBox : TextBox
    {
        private double intNum;

        public double IntNum
        {
            get { return intNum; }
            set { intNum = value; }
        }

        public IntBox()
        {   // set initial values to prperty Text, Event handlers properties TextChanged, Leave
            this.Text = "0";

            this.TextChanged += new EventHandler(IntBox_TextChanged);
            this.Leave += new EventHandler(IntBox_Leave);
            this.Math.Exp();
            InitializeComponent();
        }

        protected void IntBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                IntNum = Convert.ToDouble(this.Text);

                Math.Exp(intNum);
            }
            catch (FormatException)
            {
                  MessageBox.Show("Other than Integer number entered",
                   "Your error was",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        protected void IntBox_Leave(object sender, EventArgs e)
        {
            intNum = Convert.ToDouble(this.Text);
            Math.Exp(intNum);
            //intNum = Math.Exp(this.Text);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void IntBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                intNum = Convert.ToDouble(this.Text);
                Math.Exp(intNum);
                // intNum = Math.Exp(this.Text);
            }

        }
    }
}
11
  • Math.Exp returns new value. You do nothing with this value Dec 24, 2015 at 14:55
  • So what should I do to let it accept the Exponential e? cause I was searching everywhere and couldn't find anything
    – S.M
    Dec 24, 2015 at 14:56
  • Check MSDN for the return type of Math.Exp(), assign it to a variable and display it on your control.
    – CodeCaster
    Dec 24, 2015 at 14:58
  • What do you want to do with the result of the calculation anyway? Display it somewhere?
    – Glubus
    Dec 24, 2015 at 14:59
  • Yes, then i made a form to only let the user enter integer numbers and exponential e
    – S.M
    Dec 24, 2015 at 15:01

1 Answer 1

0

First of all, since you want to obtain an integer value, you should declare it so:

// use Int64 to allow larger numbers. Otherwise stick to the classic int type
private Int64 intNum;

public Int64 IntNum
{
    get { return intNum; }
    set { intNum = value; }
}

I assume that you want to allow integer values that are near what the scientific notation (i.e. numbers with exponent E) would give you, as E numbers are double values.

try
{
    IntNum = Convert.ToDouble(this.Text);
    Math.Exp(intNum);
}

can become

try
{
    IntNum = ConvertToInt64(Convert.ToDouble(this.Text));
}
catch (...)
{

}

You will obtain different exceptions for out of range values and invalid format, so handling FormatException alone is not enough.

Another aspect of the solution as a whole is its complexity. Consider using Binding, as this will automatically handle data transfers (from property to control and viceversa).

1
  • Double values are also acceptable that's why I did double (sorry I didn't explain myself enough thats my fault), the problem that i have is that of the exponential.
    – S.M
    Dec 24, 2015 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.