我正在写一个c#windows窗体代码
我正在写一个c#windows窗体代码来 从button1和button2中获取数字,并将它们添加到文本框中,但编译器会在convert.toint32(textbox3.text)语句中争论 而且它还增加了我正在写一个c#windows窗体代码,c#,C#,我正在写一个c#windows窗体代码来 从button1和button2中获取数字,并将它们添加到文本框中,但编译器会在convert.toint32(textbox3.text)语句中争论 而且它还增加了两个变量和三个变量的值,我怎样才能在保持不变的同时增加文本框的值呢 我需要一个解决方案 int Three = 0; int Two = 0; //int one = 0; int sum = 0; // int sum = 0; /
两个变量
和三个变量
的值,我怎样才能在保持不变的同时增加文本框的值呢
我需要一个解决方案
int Three = 0;
int Two = 0;
//int one = 0;
int sum = 0;
// int sum = 0;
//int dec = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
//Three += 3;
//textBox3.Text = sum.ToString();
Three += 3;
sum = Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text = sum.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Two += 2;
sum = Two + Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text =Convert.ToInt32(textBox3.Text) + Two.ToString();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString();
}
`改变
sum = Convert.ToInt32(textBox3.Text) + Three;
到
同时,移除
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString(); // this
}
因为它没有任何意义。您的变量属于类,可以在构造函数中进行初始化。这可以通过多种方式完成,但您需要检查文本框是否有值,然后尝试转换并添加它
private int Two;
private int Three;
private int sum;
public Form1()
{
this.Two = 0;
this.Three = 0;
this.sum = 0;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
this.Three += 3;
sum = textBox3.Text != String.Empty ? Convert.ToInt32(textBox3.Text) : 0;
textBox3.Text = Convert.ToString(sum + this.Three);
}
... same for number Two
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = "0";
}
编译器争论…
不要通过争论降低你自己:)最初文本框是空的。无法将.ToInt32空字符串初始化为3
到3
而不是零。单击按钮时不要增加其值。现在,每次单击按钮时都会增加该值。
private int Two;
private int Three;
private int sum;
public Form1()
{
this.Two = 0;
this.Three = 0;
this.sum = 0;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
this.Three += 3;
sum = textBox3.Text != String.Empty ? Convert.ToInt32(textBox3.Text) : 0;
textBox3.Text = Convert.ToString(sum + this.Three);
}
... same for number Two
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = "0";
}