C#的textbox问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 05:57:26
这是计算器中1的代码,要求按按键1 btn1 后textbox1 显示1.
private void btn1_Click(object sender, EventArgs e)
{
this.textBox1.Text += 1;
}
其中为什么this.textbox1.text要哦加上一个“+”??什么意思
如果不加的话就会提示
错误 1 无法将类型“int”隐式转换为“string” C:\C#\calc\calc\Form1.cs 45 34 calc

this.textBox1.Text += 1
可以理解为
this.textBox1.Text=this.textBox1.Text+1,是一个的string型加上int型,通过隐式转换可以编译通过
但是如果是
this.textBox1.Text=1,就是把int型值直接赋给string型的对象,这显然是不行的

this.textBox1.Text=this.textBox1.Text+1,是一个的string型加上int型,系统会自动进行类型转换,而直接的int赋值给string是不行的

表示this.textBox1.Text = this.textBox1.Text+"1";
this.textbox1.text为"string"型,1为"int"型,syntax error