[問題] 如何讓使用者,只能在textbox填寫數字?
private void button1_Click(object sender, EventArgs e)
{
int name = int.Parse(textBox1.Text);
int year = int.Parse(textBox2.Text);
int savings = int.Parse(textBox3.Text);
int spend = int.Parse(textBox4.Text);
int total = savings - spend;
int n;
if (int.TryParse(textBox1.Text, out n))
{
MessageBox.Show("數字");
}
else
{
MessageBox.Show("非數字");
}
richTextBox1.Text += "姓名:" + name.ToString() + "\r\n";
richTextBox1.Text += "年齡:" + year.ToString() + "\r\n";
richTextBox1.Text += "存款;" + savings.ToString() + "\r\n";
richTextBox1.Text += "剩餘:" + total.ToString();
}
各位高手好,小弟為超級新新新新手,
因為不希望使用者在text輸入非數字的文字,所以採用tryparse。
但不太了解,我哪裡寫錯,最後系統還是會當掉。
或是可教我如何讓使用者可以輸入非數字的文字,最後顯現在richtextbox中?
懇請指教,感激不盡
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.138.108.44
推
04/29 00:03, , 1F
04/29 00:03, 1F
推
04/29 12:10, , 2F
04/29 12:10, 2F
→
04/29 12:12, , 3F
04/29 12:12, 3F
推
04/29 12:31, , 4F
04/29 12:31, 4F