Mohan Mahajan
Mohan Mahajan

Reputation: 1651

Addition using a single TextBox

I am writing a c# windowsform application for addition of two entries in a single text box.

I have 1 text box, 1 add button and 1 result button.

I need to achieve following algo,

  1. User enter the no in text box. It will be stored in int n1.

  2. User will press add button. At this time the content of the textbox.text will be get cleared and user must be able to enter the secound value in it.

  3. This secound value will get stored in int n2.

  4. Now when user click on result button he should get int result=n1+n2

I have written following in add buttons's click event,

n1=convert.Int32(textbox1.text); 

textbox1.text=""; 


n2=convert.Int32(textbox1.text);//exception occures here 

And result button's click event has,

textbox1.text=result.tostring(); 

I leanred the working of this program and find out that it is due to NULL value assignment to int n2.

I am bit confused how can I achieve above problem using single textbox?

Is there any way to provide the textbox.txt value through user through textbox field only?

Is there any property of textbox which I need to set?

I am very new to .net! Please help!

Thanks in advance for your kind attention!

Upvotes: 1

Views: 8502

Answers (5)

Albireo
Albireo

Reputation: 11095

You code contains a couple of errors:

  1. You are using Int32 Convert.ToInt32(String) which is bad, because will throw an exception if the user enters something that can't be cast to an Int32, you should use Boolean Int32.TryParse(String, out Int32) instead, and check if the conversion is successful.

  2. You are calling n2 = Convert.ToInt32(textbox1.Text); right after you set textbox1.Text = ""; and this is also bad, because it won't wait for a user input, it will try to convert an empty string to an integer right away, which will yield an exception.

  3. You can't know when the user has finished inserting his number, so you have to rely to something external to the TextBox to let you know you should perform the addition.

Try the following:

  • Place a TextBox.
  • Place a Button.
  • Initialize a "grandTotal" integer variable to zero.
  • When the user press the button, parse the content of the TextBox in a temporary "submittedValue" integer variable, then sum this temporary variable to your "grandTotal" variable, and display the resulting "grandTotal" variable value.

This way you'll be able to continuously perform the addition operation on the previous total.

Example

The global variable:

protected Int32 GrandTotal = 0;

The Button click event handler:

Int32 currentValue = 0;

if (Int32.TryParse(userValue.Text, out currentValue))
{
    GrandTotal += currentValue;

    MessageBox.Show(String.Format("The new total is: {0}", GrandTotal));
}
else
{
    MessageBox.Show("Invalid value!");
}

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

I have no idea why you are doing it in this fashion.

Still you can do this as Declare n1, n2 as int? at the form level

Then in your Add click event do it in this fashion

if (! n1.HasValue)
{
    n1 = int.Parse(textBox1.Text);                
}
n2 = int.Parse(textBox1.Text);
textBox1.Text = "";

if you are going to do this for lotz of number then use List<int?> myIntList = new List<int?>() and your add event would be

myIntList.Add(int.Parse(textBox1.Text));
textBox1.Text = "";

and the result is int? result = myIntList.Sum();

Upvotes: 0

Sergey K
Sergey K

Reputation: 4114

look at the folloing code

      private int i = 0;
    private int[] a = new int[2];
    private void button1_Click(object sender, EventArgs e)
    {
        int b;
        if(Int32.TryParse(textBox1.Text, out b))
        {
            a[i] = b;
            i++;
            textBox1.Text = "";
        }
        else
        {
            MessageBox.Show(@"Incorrect number");
        }


    }

    private void resultbutton2_Click(object sender, EventArgs e)
    {
        int sum = a[0] + a[1];
        MessageBox.Show("Sum: " + sum);
    }
}

Upvotes: 1

danyolgiax
danyolgiax

Reputation: 13116

try something like this:

private int result=0;

addMethod()
{
    //here you can use also int.TryParse
    if(string.isnullorempty(textbox1.text.trim()))
    textbox1.text="0";

    result+=convert.toint32(textbox1.text);

    textbox1.text=string.empty;
}

resultMethod()
{
    textbox1.text=result.tostring();
}

Upvotes: 1

Bolu
Bolu

Reputation: 8786

move n2 = Convert.Int32(textbox1.Text); to your result button's click event

Add button click event:

n1=convert.Int32(textbox1.text);   
textbox1.text=""; 

result button's click event:

n2=convert.Int32(textbox1.text);
result=n1+n2;
textbox1.text=result.tostring(); 

Upvotes: 3

Related Questions