今天早上我创建了一个 Windows 窗体项目,以根据我所学的知识制作一个计算器,并自学一些编程。
一切都很好,我可以让数字按钮在文本框中打印特定的数字,并且操作员按钮会清除文本框,这样你就可以写第二个数字,但是当我按下等于“=”时,我得到了这个运行时错误:
值不能为空。参数名称:字符串
我将向您展示 form1 类中的代码(像往常一样,它是我必须编辑的唯一类):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> theNumberList = new List<string>();
public string numbers = null;
int number1, number2;
string num1, num2, operat;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
theNumberList.Add("1");
ShowNumbers(theNumberList, numbers, textBox1);
}
public static void ShowNumbers(List<string> theList, string aString, TextBox textBox)
{
foreach(string number in theList)
{
aString = aString + number;
textBox.Text = aString;
}
}
private void button2_Click(object sender, EventArgs e)
{
theNumberList.Add("2");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button3_Click(object sender, EventArgs e)
{
theNumberList.Add("3");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button4_Click(object sender, EventArgs e)
{
theNumberList.Add("4");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button5_Click(object sender, EventArgs e)
{
theNumberList.Add("5");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button6_Click(object sender, EventArgs e)
{
theNumberList.Add("6");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button7_Click(object sender, EventArgs e)
{
theNumberList.Add("7");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button8_Click(object sender, EventArgs e)
{
theNumberList.Add("8");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button9_Click(object sender, EventArgs e)
{
theNumberList.Add("9");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void button0_Click(object sender, EventArgs e)
{
theNumberList.Add("0");
ShowNumbers(theNumberList, numbers, textBox1);
}
private void Plus_Click(object sender, EventArgs e)
{
num1 = numbers;
theNumberList.Clear();
num2 = numbers;
operat = "+";
}
private void Subtract_Click(object sender, EventArgs e)
{
}
private void Multiply_Click(object sender, EventArgs e)
{
}
private void Divide_Click(object sender, EventArgs e)
{
}
private void Equal_Click(object sender, EventArgs e)
{
int result;
switch (operat)
{
case "+":
number1 = int.Parse(num1);
number2 = int.Parse(num2);
result = number1 + number2;
theNumberList.Clear();
textBox1.Text = result.ToString();
break;
}
}
}
所以这是代码,错误出现在“number1 = int.parse(num1);”中的 Equal_click 这里 :
private void Equal_Click(object sender, EventArgs e)
{
int result;
switch (operat)
{
case "+":
number1 = int.Parse(num1);
number2 = int.Parse(num2);
result = number1 + number2;
theNumberList.Clear();
textBox1.Text = result.ToString();
break;
}
}
我不知道如何解决它,有什么问题?