-4

このコードには 2 つの問題があります。送信ボタン イベントがテキスト ボックス イベントで計算された変数を認識せず、テキスト ボックス イベントが 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)
    {

    }
 }
}
4

5 に答える 5

5

記号は==代入記号ではなく比較記号です

使用する必要があります

if (userInput == 2) // this is a comparison
{ 
    points = 20; // this is an assignment
} 
于 2012-09-27T07:11:57.873 に答える
1

まず、イベントに対してローカルなポイントを宣言したTextChangedので、ボタン クリック イベントではアクセスできません。

textBox1.pointsint 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;、正しいことになります

于 2012-09-27T07:11:06.233 に答える
0

すでに述べたように、代入演算子とグローバル/ローカル変数を混同しました。

しかし、コードには他にもいくつかの「エラー」があります。ユーザー入力は単純なテキストの場合があります。そのため、例外が発生します。int.Parse の代わりに int.TryParse を使用する必要があります。また、コードには多数の if がありますが、それらをまとめて起動することはできません。スイッチの使用をお勧めします。そしてもちろん、何らかの方法で定数に名前を付けようとする必要があります。これにより、コードがはるかに読みやすくなります!

コード全体は次のようになります。

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");
}
于 2012-09-27T07:25:45.023 に答える
0

変数に値を追加するには、次のように記述します。

points = 5;
于 2012-09-27T07:12:25.710 に答える
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)
    {

    }
 }
}
于 2012-09-27T07:14:54.683 に答える