我刚接触C#,基本了解Java,但无法使这段代码正常运行。
这只是一个基本的计算器,但是当我运行程序时,VS2008会给出这个错误:
我几乎做了相同的Java程序,使用JSwing也完美运行了。
这是C#的窗体:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace calculadorac
{
public partial class Form1 : Form
{
int a, b, c;
String resultado;
public Form1()
{
InitializeComponent();
a = Int32.Parse(textBox1.Text);
b = Int32.Parse(textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
add();
result();
}
private void button2_Click(object sender, EventArgs e)
{
substract();
result();
}
private void button3_Click(object sender, EventArgs e)
{
clear();
}
private void add()
{
c = a + b;
resultado = Convert.ToString(c);
}
private void substract()
{
c = a - b;
resultado = Convert.ToString(c);
}
private void result()
{
label1.Text = resultado;
}
private void clear()
{
label1.Text = "";
textBox1.Text = "";
textBox2.Text = "";
}
}
有什么问题可能会出现?有没有解决方法?
PS:我也尝试了其他方法
a = Convert.ToInt32(textBox1.text);
b = Convert.ToInt32(textBox2.text);
但它没有起作用。