【发布时间】:2016-08-26 11:51:45
【问题描述】:
我做了一个非常简单的程序,用户需要写下他的名字和年龄。然后会弹出一个消息框并显示姓名和年龄。 (http://imgur.com/a/Kqb1r)
public partial class MySecondApplication : Form
{
public MySecondApplication()
{
InitializeComponent();
}
private void txtName_TextChanged(object sender, EventArgs e)
{
txtAge.Enabled = true;
}
private void txtAge_TextChanged(object sender, EventArgs e)
{
cmdSubmit.Enabled = true;
}
private void cmdSubmit_Click(object sender, EventArgs e)
{
var name = txtName.Text;
var age = Convert.ToByte(txtAge.Text);
MessageBox.Show($"Your name is {name} and You're {age} years old.");
}
private void cmdExit_Click(object sender, EventArgs e)
{
Close();
}
}
我该怎么做:如果年龄是一个字符串,会弹出一个消息框说“年龄不是数字,用户需要再试一次”?
【问题讨论】:
-
继续阅读
int.Parse() -
有一个 MaskedTextBox 可以拒绝无效输入。 msdn.microsoft.com/en-us/library/…