2013-03-16 54 views
0

如何添加一個整數,即。一個計數變量用於顯示目的?將一個整數添加到標籤用於顯示目的

int counter = 0; 
    private void btnDisplay_Click(object sender, EventArgs e) 
    { 

     StreamReader myReader = new StreamReader("StudentRecords.txt"); 

     while (myReader.EndOfStream == false) 
     { 
      string[] storageArray = myReader.ReadLine().Split('#'); 
      if (storageArray[0] == "S") 
      { 
       lstDisplay.Items.Add(""); 
       lstDisplay.Items.Add("Student Name: " + storageArray[1]); 
       lstDisplay.Items.Add("Student Number: " +storageArray[2]); 
       lstDisplay.Items.Add("Attendance: " + storageArray[5]); 
       lstDisplay.Items.Add("Modules: "); 
       counter++; 
      } 
      else if (storageArray[0] == "M") 
      { 
       lstDisplay.Items.Add(storageArray[1]); 

      } 

     } 

     //label to be used to display the number of students 
     lblnoOfStudents. ?? 
     myReader.Close(); 

    } 
+0

考慮看着我的編輯答案安德烈學習一些其他的方式,你可以改善你的函數 – Sayse 2013-03-16 12:04:36

回答

0
lblnoOfStudents.Text = counter.ToString(); 

這將改變標籤,以這個數字上的文字。如果您想在現有文本的末尾追加它使用+=

lblnoOfStudents.Text += counter.ToString(); 
+0

謝謝你的作品! – 2013-03-16 11:45:23

+1

@AndreSmith - 如果您單擊顯示按鈕兩次,第二個將無法按預期工作 – Sayse 2013-03-16 11:54:04

+0

沒有憂慮,我用第一個...它的所有我需要反正:) – 2013-03-16 16:03:30

0

將您的信息給你的標籤Text屬性:

lblnoOfStudents.Text = string.Format("Students: {0}", counter); 
+1

作爲注意:'string.Format'將用'counter'變量替換'{0}',所以它會導致類似'Students:1024' – Zbigniew 2013-03-16 11:44:47

+1

如果你想擺脫*這樣的東西,通過'CultureInfo.InvariantCulture'作爲Format()的第一個參數。這樣你不會以'1024','1024'或類似的而不是'1024'結束。 – 2013-03-16 11:49:20

0

您可以使用Label控制的.Text財產。喜歡;

獲取或設置Label控件的文本內容。

lblnoOfStudents.Text = counter.ToString(); 

這將與您counter變量的字符串表示更改標籤的文本。

0

繼承人aworking例如爲您的代碼

//int counter = 0; may as well move inside as it looks to be localised 
private void btnDisplay_Click(object sender, EventArgs e) 
{ 

    StreamReader myReader = new StreamReader("StudentRecords.txt"); 
    int counter = 0; 
    while (myReader.EndOfStream == false) 
    { 
     string[] storageArray = myReader.ReadLine().Split('#'); 
     if (storageArray[0] = "S") 
     { 
      lstDisplay.Items.Add(""); 
      lstDisplay.Items.Add("Student Name: " + storageArray[1]); 
      lstDisplay.Items.Add("Student Number: " +storageArray[2]); 
      lstDisplay.Items.Add("Attendance: " + storageArray[5]); 
      lstDisplay.Items.Add("Modules: "); 
      counter++; 
     } 
     else if (storageArray[0] == "M") 
     { 
      lstDisplay.Items.Add(storageArray[1]); 

     } 

    } 

    //label to be used to display the number of students 
    lblnoOfStudents.Text = counter.ToString(); 
    myReader.Close(); 

} 

編輯:我覺得我應該提高自己的答案,所以我想我只是提高你的代碼。使用將自動關閉讀者

//int counter = 0; may as well move inside as it looks to be localised 
private void btnDisplay_Click(object sender, EventArgs e) 
{ 

    using(StreamReader myReader = new StreamReader("StudentRecords.txt")) 
    { 
     int counter = 0; 
     while (!myReader.EndOfStream) 
     { 
      string[] storageArray = myReader.ReadLine().Split('#'); 
       switch(storageArray[0]) 
       { 
       case "S": 

        lstDisplay.Items.Add(""); 
        lstDisplay.Items.Add("Student Name: " + storageArray[1]); 
        lstDisplay.Items.Add("Student Number: " +storageArray[2]); 
        lstDisplay.Items.Add("Attendance: " + storageArray[5]); 
        lstDisplay.Items.Add("Modules: "); 
        counter++; 
        break; 
       case "M": 
        lstDisplay.Items.Add(storageArray[1]); 
        break; 
       default: 
        break; 
      } 
     } 
    } 

    //label to be used to display the number of students 
    lblnoOfStudents.Text = counter.ToString(); 
    } 
} 
  • ==false是 多餘的,因爲你已經可以從 這個操作符左側得到一個布爾值。
  • switch語句會很容易讓你增添 多種不同的選擇,而不是長別人的,如果
+0

downvote爲什麼?.. – Sayse 2013-03-16 11:48:44

+0

因爲完全相同的答案是已經提前幾分鐘提供。 – MarcinJuraszek 2013-03-16 11:49:40

+0

我仍然看不到相同的答案。我不僅在內部移動櫃檯,因爲它似乎沒有必要在外面,我還提供了代碼,可以代替他的功能 – Sayse 2013-03-16 11:50:56

相關問題