我对这段代码有两个问题。我遇到了麻烦,因为提交按钮事件无法识别在文本框事件中计算的变量,并且因为文本框事件未将我的if语句识别为语句。你可以在下面的评论中看到我遇到麻烦的地方。
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 WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int points;
int userInput = int.Parse(textBox1.Text);
if (userInput == 0)
{
points == 5; //I CANNOT COMPILE BECAUSE APPARENTLY I AM NOT ALLOWED TO USE
THIS AS A STATEMENT?
}
if (userInput == 1)
{
points == 10;
}
if (userInput == 2)
{
points == 20;
}
if (userInput ==3)
{
points == 30;
}
else
{
points == 40;
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show = ("You have been awarded" + textBox1.points + "points");
} //I WANT TO BE ABLE TO RETRIEVE THE POINTS CALCULATED USING THE CALCULATION IN
TEXT BOX, BUT I CANNOT COMPILE THE BUTTON EVENT DOES NOT RECOGNIZE THE POINTS
VARIABLE
private void label1_Click(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:5)
==
符号是比较符号而非分配符号
您需要使用
if (userInput == 2) // this is a comparison
{
points = 20; // this is an assignment
}
答案 1 :(得分:1)
首先,您已声明TextChanged
事件的本地点,因此无法在按钮点击事件中访问它。
textBox1.points
不正确,因为int points
声明与TextBox
无关,您可以将点声明为类变量,例如
public partial class Form1 : Form
{
int points =0;
public Form1()
{
InitializeComponent();
}
//......//
然后这将成为
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show( string.Format("You have been awarded {0} points",this.points));
}
您也可以使用=
符号进行作业,因此points = 5;
将是正确的选择
答案 2 :(得分:0)
要为变量添加值,您应该写:
points = 5;
答案 3 :(得分:0)
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 WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int points=0;
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int userInput = int.Parse(textBox1.Text);
if (userInput == 0)
{
points = 5;
}
if (userInput == 1)
{
points = 10;
}
if (userInput == 2)
{
points = 20;
}
if (userInput ==3)
{
points = 30;
}
else
{
points = 40;
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show = ("You have been awarded" + points.ToString() + "points");
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
答案 4 :(得分:0)
如前所述 - 您混淆了赋值运算符和全局/局部变量。
但是代码中还有其他几个“错误”。用户输入可能是简单的文本 - 因此会有异常,您应该使用int.TryParse而不是int.Parse。你的代码中也有很多ifs - 但它们不能同时解雇,我建议使用switch。当然,您应该尝试以某种方式命名常量,它将使您的代码更具可读性!
您的代码可能如下所示:
int pointsAvarded = 0;
private void textBox1_TextChanged(object sender, EventArgs e)
{
pointsAvarded = 0; //so you can be sure to use the latest input
int userInput = 0;
if (int.TryParse(textBox1.Text, out userInput))
switch (userInput)
{
case 0:
points = 5;
break;
case 1:
points = 10;
break;
...
default:
points = 40;
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (pointsAvarded != 0)
MessageBox.Show("You have been awarded" + pointsAvarded + "points");
}