Reputation: 171
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?
Upvotes: 5
Views: 151413
Reputation: 36591
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();
}
Upvotes: 13
Reputation: 59
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)));
}
}
Upvotes: 0
Reputation: 7228
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.
Upvotes: 5
Reputation: 2814
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();
}
Upvotes: 0