为何一按Enter 总会弹出输入字串格式不正确
using System;
using System.Windows.Forms;
namespace ArrayTestResult {
public partial class Form1 : Form {
int[] marks;
int totalTest;
public Form1() {
InitializeComponent();
}
private void start_Click(object sender, EventArgs e)
{
try
{
totalTest = int.Parse(txtSize.Text);
if (totalTest <= 0)
throw new Exception("Number of Test must be > 0");
marks = new int[totalTest];
ShowTestResult(totalTest);
grpInput.Enabled = true;
btnEnter.Enabled = true;
grpReplaceAll.Enabled = true;
btnReplaceAll.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name);
}
}
private void ShowTestResult(int num)
{
try
{
txtTestResults.Clear();
for (int i = 0; i < totalTest; i++)
{
txtTestResults.Text += "Test" + i + ":" + marks;
txtTestResults.Text += "\r\n";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name);
}
}
private void btnEnter_Click(object sender, EventArgs e)
{
try
{
int index = int.Parse(txtIndex.Text);
int mark = int.Parse(txtOriginalMark.Text);
if (mark <= 0)
throw new Exception("Tes test mark shold be within 0 and 100");
marks[index] = mark;
ShowTestResult(totalTest);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name);
}
}
}
}
int index = int.Parse(txtIndex.Text);
int mark = int.Parse(txtOriginalMark.Text);
楼主,错误就在第二行,txtOriginalMark.text应该是Replace All面板OriginalMark后的文本框的属性,楼主你粗心把它当做Marks后的文本框了。当你按下Enter时,txtOriginalMark.text为空,所以转为int时会出错。 |