Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 3 TextBox and 1 Button

First TextBox Value=2
Second TextBox Value=2
Then Click Button i want Answer
Third TextBox Value = 4

AND


First TextBox Value=ab
Second TextBox Value=cd
Then Click Button i want Answer
Third TextBox Value = abcd

How to Solve its, Please Help Me
Posted
Comments
thatraja 30-Oct-13 5:11am    
What have you tried?
BillWoodruff 30-Oct-13 5:21am    
Having TextBoxes serve both as sources of numeric values and strings is generally a poor design choice. Consider using NumericUpDown Counters for your numeric inputs, or a MaskedTextBox.

Try parsing the content into integers, if that works add otherwise concatenate.

C#
private void button_Click(object sender, EventArgs e)
{
    int a;
    int b;

    if (Int32.TryParse(textBox1.Text, out a) && Int32.TryParse(textBox2.Text, out b))
        textBox3.Text = Convert.ToString(a + b);
    else
        textBox3.Text = textBox1.Text + textBox2.Text;
}



Hope this helps,
Fredrik
 
Share this answer
 
Comments
Thanks7872 30-Oct-13 5:38am    
+5
C#
string val1 = "", val2 = "", val3 = "";
try
{
    // as a digits
    val1 = text1.Value;
    val2 = text2.Value;
    val3 = (Convert.ToInt32(val1) + Convert.ToInt32(val2)).ToString();
    txt3.Value = val3;
}
catch (Exception ex)
{
    // as a string
    val1 = text1.Value;
    val2 = text2.Value;
    val3 = val1 + val2;
    txt3.Value = val3;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900