using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class login : Form { public login() { InitializeComponent(); } private void Login_Load(object sender, EventArgs e) { label1.Text = "+"; label2.Text = "="; button1.Text = "计算"; } private void Button1_Click_1(object sender, EventArgs e) { int a, b, c; /*try { a = Convert.ToInt32(textBox1.Text); b = Convert.ToInt32(textBox2.Text); } catch (Exception) { MessageBox.Show("请输入整数"); return;//不执行下面的语句了 退出这个方法 } a = Convert.ToInt32(textBox1.Text); b = Convert.ToInt32(textBox2.Text);*///上面的检查也是可以的 if (int.TryParse(textBox1.Text, out a) == false) { MessageBox.Show("请输入整数"); return; } if (!int.TryParse(textBox2.Text, out b)) //格式: int.TryParse(x,out y) //x是要转化的字符串,y是一个int 型的变量; { MessageBox.Show("请输入整数"); return; } c = a + b; textBox3.Text = Convert.ToString(c); } } }