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?
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 totextbox2
, check the brackets properly.should be
Try This Tested Code.
use this.
you can do like this:
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 useInt32.TryParse
:Btw, like Glenn has pointed out, you can use only one event handler like in this example.