2012-09-27 71 views
-4

這段代碼有兩個問題。我遇到了麻煩,因爲提交按鈕事件無法識別文本框事件中計算的變量,並且因爲文本框事件不會將我的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) 
    { 

    } 
} 
} 
+1

使用單一=爲賦值運算符;當你在變量內部刪除變量 - 它是局部變量,在程序的其他部分使用它,這個變量必須是全局變量 - 在方法體外聲明它 – JleruOHeP

+2

[Assignment](http://msdn.microsoft.com/zh-cn/我們/庫/ sbkb459w.aspx)。但是這個代碼有幾個問題與基本的C#語法有關,我不知道從哪裏開始。 –

回答

5

==符號是一個比較符號不是賦值符號

您需要使用

if (userInput == 2) // this is a comparison 
{ 
    points = 20; // this is an assignment 
} 
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;將是正確的事

0

增加價值變量,你應該寫:

points = 5; 
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) 
    { 

    } 
} 
} 
+0

我可以用此編譯,但如果我將MessageBox.Show更改爲textBox2.Text按下按鈕1後不會出現任何內容。 –

+0

哈哈? .............. – m4ngl3r

0

如前所述 - 你與賦值運算符以及全局/局部變量混淆。

但是在你的代碼中還有其他幾個「錯誤」。用戶輸入可能是簡單的文本 - 所以會出現異常,您應該使用int.TryParse而不是int.Parse。你的代碼中還有很多ifs - 但它們不能一起開火,我推薦使用開關。當然,你應該嘗試以某種方式命名你的常量,它會讓你的代碼更具可讀性!

Overral你的代碼可能是這樣的:

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"); 
} 
相關問題