本文介绍了int和String计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个TextBox和1个按钮



第一个TextBox值= 2

第二个TextBox值= 2

然后单击按钮我想要答案

第三个TextBox值= 4



AND





第一个TextBox值= ab

第二个TextBox值= cd

然后点击按钮我想要答案

第三个TextBox值= abcd



如何解决它,请帮助我

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

推荐答案

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;
}







希望这会有所帮助,

Fredrik




Hope this helps,
Fredrik


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;

}


这篇关于int和String计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:52