我遇到了檢查文本框並確保其中只有整數的問題。只允許文本框中的整數
到目前爲止我能確認有一個在文本框中的文本,但檢查如果他們整數無法正常工作。這是我迄今爲止的代碼。
if (textBox1.Text.Length == 0)
{
errorProvider1.SetError(textBox1, "need Cost of Disks");
return;
}
if (textBox2.Text.Length == 0)
{
errorProvider2.SetError(textBox2, "need Total disks in package");
return;
}
if (textBox3.Text.Length == 0)
{
errorProvider3.SetError(textBox3, "need the Gigabyte per disk");
return;
}
try
{
Double[] myValues = new Double[3];
myValues[0] = Double.Parse(textBox1.Text);
myValues[1] = Double.Parse(textBox2.Text);
myValues[2] = Double.Parse(textBox3.Text);
Double ppd = myValues[0]/myValues[1]/myValues[2];
ppd = Math.Round(ppd, 3, MidpointRounding.AwayFromZero);
label4.Text = ppd.ToString();
}
catch (FormatException)
{
//errorProvider1.SetError(label4, "testing1");
//errorProvider2.SetError(label4, "testing2");
//errorProvider3.SetError(label4, "testing3");
return;
}
我會推薦使用正則表達式。 http://www.codeproject.com/Articles/93804/Using-Regular-Expressions-in-C-NET – Spacemancraig 2012-07-25 18:31:14