5

I have tried this code to add from textbox1.text and textbox2.text into textbox3.text

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

         { 
             textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
         }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

        {
        textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
        }
    }

please Help ... and is there anything like to change 'format 'property of textbox to general number?

3
  • Could you specify the problem? Commented Feb 22, 2012 at 12:42
  • 1
    Is this a homework question? If so, then please use the homework tag. Commented Feb 22, 2012 at 12:47
  • 1
    Zortkuns answer will do the trick but you don´t need two eventhandlers for this. They are equal. Bind one handler to both texboxes instead. Commented Feb 22, 2012 at 12:57

4 Answers 4

13

You made a mistake || should be replace with && so it will check both the text boxes are filled with value.

You have used .ToString() method wrongly which applies only to textbox2, check the brackets properly.

textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());

should be

textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();

Try This Tested Code.

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
  textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();
 }
    
 private void textBox2_TextChanged(object sender, EventArgs e)
 {
  if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
  textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

i entered 25 in textbox1 and when i entered 25 in textbox2 .. textbox3 displayed '2525'
5

Your current expression is missing the negation (!) in the second part of your condition

Also, it should be a && not ||

As for your error, string was not in the correct format, you will get this with any unsafe code whenever the input string cannot be converted to an int. Surround it with a try catch or use Int32.TryParse:

private void **textBox_TextChanged**(object sender, EventArgs e)
{
     int first = 0;
     int second= 0;
     if(Int32.TryParse(textBox2.Text, out second) && Int32.TryParse(textBox1.Text, out first))
         textBox3.Text = (first + second ).ToString();
     }
}

Btw, like Glenn has pointed out, you can use only one event handler like in this example.

3 Comments

Shouldn't that be !string.IsNullOrEmpty() && !string.IsNullOrEmpty() ?
sorry zortkun for misguiding u but i have written d code proper in my program here ive done a mistake . ive tried but having the above error
yeah this worked ... but instead textbox3.Text = (first + second).ToString(); i tried textbox3.Text =Convert.ToString(first + second); \\ worked too
0

you can do like this:

if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

         { 

             textBox3.Text =convert.toString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).toString();
         }

3 Comments

ive also tried this riwan . but it is showing me error 'Input string was not in a correct format'
Where you are getting this error? i have tried it myself and it works and this code which i'm using if (!string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)) { TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text)).ToString(); }
@HMcompfreak just replace your condition with TextBox1.Text!="" && TextBox2.Text!="" and it works fine
0

use this.

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
        {
            textBox3.Text = Convert.ToString((Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)));
        }
    }
private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
        {
            textBox3.Text = Convert.ToString((Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)));
        }
    }

3 Comments

As per comments from @Glenn - no need to use two event handlers - also - could you comment what you've changed from the original code?
instead of if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) change to if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
instead of if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text)) change to if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text)) . For Glenn comment its true you can use single event. Select the text box, go to properties tab then events. Go to TextChanged event and assign select same event.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.