在c#中显示列表或数组中的项

本文关键字:数组 列表 显示 | 更新日期: 2023-09-27 18:18:57

在数组中显示项目/对象列表时遇到麻烦。

我要做的是将学生的名字、姓氏、年龄和科目(属性)的标记添加到数组中。如果我以这种方式添加了4名学生到列表中,我想在点击显示按钮时在richTextBox中再次显示所有学生及其属性。

我如何去它在"displayAll_Click"功能??我必须使用某种形式的for循环吗?如有任何帮助,不胜感激。

我有以下代码:

在这个函数中,我将学生添加到数组中:
private void btnAdd_Click(object sender, EventArgs e)
    {
        // Create new student and assign name etc provided by user
        Student _Student = new Student();
        _Student.Name = txtName.Text;
        _Student.Surname = txtSurname.Text;
        _Student.Age = Convert.ToInt32(txtAge.Text);
        _Student.ITMark = Convert.ToInt32(txtIT.Text);
        _Student.MathMark = Convert.ToInt32(txtMath.Text);
        _Student.EngMark = Convert.ToInt32(txtEng.Text);
        MessageBox.Show("Student added.");
        // Increase counter and display how many students added so far
        CountStudents++;
        Average = Convert.ToInt32(txtIT.Text) + Convert.ToInt32(txtMath.Text) + Convert.ToInt32(txtEng.Text);
        _Student.AverageMark = Average / 3;
        //Display Student's properties in the richBox
        richTextBox1.Text = ("Student: " + Convert.ToString(CountStudents) +
                             "'nName: " + _Student.Name +
                            "'nSurname: " + _Student.Surname +
                             "'nAge: " + _Student.Age +
                             "'nStudent Average: " + Convert.ToString(_Student.AverageMark));
        //Add the newly added student to the ClassList array
        ClassList[CountStudents - 1] = _Student;
        //Clear the list after student is added
        txtAge.Clear();
        txtName.Clear();
        txtSurname.Clear();
        txtIT.Value = 0;
        txtMath.Value = 0;
        txtEng.Value = 0;
        txtName.Focus();
    }

在这个函数中,我想显示所有已添加的学生,但如何呢?

private void displayAll_Click(object sender, EventArgs e)
    {
        List<Student> _StudentList = new List<Student>();
        Student _Student = new Student();
        _Student.Name = txtName.Text;
        _Student.Surname = txtSurname.Text;
        _Student1.Age = Convert.ToInt32(txtAge.Text);
        _Student.ITMark = Convert.ToInt32(txtIT.Text);
        _Student.MathMark = Convert.ToInt32(txtMath.Text);
        _Student.EngMark = Convert.ToInt32(txtEng.Text);
        for (int i = 0; i < _StudentList; i++)
        {
            richTextBox1.Text = ("Student: " + Convert.ToString(CountStudents) +
                             "'nName: " + _Student.Name +
                            "'nSurname: " + _Student.Surname +
                             "'nAge: " + _Student.Age +
                             "'nStudent Average: " + Convert.ToString(_Student.AverageMark));
            i++;
        }
        //Displaying all the students
        MessageBox.Show("Display all students.");
    }

在c#中显示列表或数组中的项

每次为richtextbox分配新文本:

使用

richTextBox1.Text +=

而不是

richTextBox1.Text =

完整代码:

for (int i = 0; i < _StudentList; i++)
{
    Student currentStudent = ClassList[i];    //get student from array
    richTextBox1.Text += ("Student: " + Convert.ToString(CountStudents) +
                         "'nName: " + currentStudent .Name +
                         "'nSurname: " + currentStudent .Surname +
                         "'nAge: " + currentStudent .Age +
                         "'nStudent Average: " + Convert.ToString(currentStudent .AverageMark));
            i++;    //you don't need to do it, let the loop handle it
}

它将把新字符串添加到其中,而不删除之前的字符串。

试试这样写:

foreach(var student in _StudentList)
{
   richTextBox1.Text += ...
}

您的第二个函数看起来像一个大的复制&粘贴错误。您正在创建一个空的_StudentList,然后继续迭代(尽管您将int与for中的List进行比较)。然后,您将显示刚刚从表单数据创建的一个学生。

试试这个:

private void displayAll_Click(object sender, EventArgs e)
{
    richTextBox1.Clear();
    for (int i = 0; i < ClassList.Count; i++)
    {
        richTextBox1.Text += ("Student: " + Convert.ToString(i) +
                         "'nName: " + ClassList[i].Name +
                        "'nSurname: " + ClassList[i].Surname +
                         "'nAge: " + ClassList[i].Age +
                         "'nStudent Average: " + Convert.ToString(ClassList[i].AverageMark));
    }
    //Displaying all the students
    MessageBox.Show("Display all students.");
}

每次运行时都会覆盖文本框,同时还会出现其他一些错误。我将使用StringBuilder和foreach循环

 StringBuilder sb = new StringBuilder();
 foreach (var student in _StudentList)
 {
      sb.Append(Environment.NewLine + "Name: " + student.Name);
      sb.Append(Environment.NewLine + "Surname: " + student.Surname);
      sb.Append(Environment.NewLine + "Age: " + student.Age);
      sb.Append(Environment.NewLine + "Student Average: " + student.AverageMark);
      sb.Append(Environment.NewLine);
 }
 richTextBox1.Text = sb.ToString();

同样,您的第一行"Student: " + Convert.ToString(CountStudents)没有多大意义,CountStudents是学生的数量,而不是特定学生的唯一标识符。