-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);
            }

        }
    }
}
Bart
  • 9,925
  • 7
  • 47
  • 64
S.M
  • 1
  • 3
  • `Math.Exp` returns new value. You do nothing with this value – Konstantin Zadiran Dec 24 '15 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 '15 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 '15 at 14:58
  • What do you want to do with the result of the calculation anyway? Display it somewhere? – Glubus Dec 24 '15 at 14:59
  • Yes, then i made a form to only let the user enter integer numbers and exponential e – S.M Dec 24 '15 at 15:01
  • Still not working.. using the public static double Exp( double d ) and system.double.. – S.M Dec 24 '15 at 15:06
  • You do know that `Math.Exp` is different from exponent entered as a number as in, for example `1.23e4` don't you? – Graham Dec 24 '15 at 15:22
  • I know but I've searched MSDN and all the other websites and my books and I don't know what I need to do. I really tried everything. If you don't want to help that's fine don't worry about it. @Graham – S.M Dec 24 '15 at 15:28
  • Just checking! It's not clear what you are trying to do. At the moment, you enter a number and whenever the number changes you calculate e to the power of your number and discard the result. – Graham Dec 24 '15 at 15:46
  • cause then that will become a .DLL file and I can use that in the form. It's an assignment I'm working on. – S.M Dec 24 '15 at 15:49
  • In the program I want that the when the user enters the E it doesn't come as an error but the program lets double values and the E. the Math.Exp is wrong I know but I tried to use it to see if there would be any luck – S.M Dec 24 '15 at 15:50

1 Answers1

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).

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • 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 '15 at 15:11