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();

    }
4

4 回答 4

0

将您的消息分配给Text标签的属性:

lblnoOfStudents.Text = string.Format("Students: {0}", counter);
于 2013-03-16T11:41:32.287 回答
0
lblnoOfStudents.Text = counter.ToString();

这会将标签上的文本更改为该数字。如果您想将其附加到现有文本的末尾,请使用+=

lblnoOfStudents.Text += counter.ToString();
于 2013-03-16T11:41:43.643 回答
0

您可以使用控件.Text的属性。Label像;

获取或设置 Label 控件的文本内容。

lblnoOfStudents.Text = counter.ToString();

counter这将使用变量的字符串表示来更改您的标签文本。

于 2013-03-16T11:43:00.340 回答
0

这是您的代码的工作示例

//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();
    }
}
  • using 将自动为您关闭阅读器
  • ==false是多余的,因为您已经可以从该运算符的左侧获得布尔值。
  • switch 语句将使您可以轻松添加多个不同的选项,而不是冗长的 else if
于 2013-03-16T11:47:35.927 回答