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 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();
}
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.
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)));
}
}
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.