0

I have the following code:

public partial class Form1 : Form
{
    const int MaxStudents = 4;

    public Form1()
    {
        InitializeComponent();
    }

   private void Form1_Load(object sender, EventArgs e)
   {
     Student[] studentList;
     studentList = new Student[4];

     studentList[0] = new Student(51584, 17);
     studentList[1] = new Student(51585, 19);
     studentList[2] = new Student(51586, 15);
     studentList[3] = new Student(51587, 20);

        for (int i = 0; i < MaxStudents; i++)
        {
            lstStudents.Items.Add(studentList.ToString()[i]);
        }
    }

EDIT: In the Student class I have:

public Student(int id, int age) {
this.id = id;
this.age = age;
}

public override string ToString()
{
    return string.Format("ID: {0} - Age: {1}", this.id, this.age);
} 

Then in the form load I have:

private void Form1_Load(object sender, EventArgs e)
{
Student[] studentList;
studentList = new Student[4];

studentList[0] = new Student(51584, 17);
studentList[1] = new Student(51585, 19);
studentList[2] = new Student(51586, 15);
studentList[3] = new Student(51587, 20);

lstStudents.Items.AddRange(studentList);
}

I was wondering how I would output the parameters of each object in the array to the listbox. How would I make it so that each object is displayed in the listbox like so:

ID: 51584 - Age: 17

I'm not too sure how to basically convert the parameters into plain text to be listed in the listbox whilst adding additional text before the parameters (Like I did with 'id:', the hyphen and 'Age:')

Sorry for the long winded question but thought I'd explain as best as I can.

4

3 回答 3

1

If you want Student to be shown that way anytime you want a string value, the easiest way is to override ToString:

public override string ToString()
{
    return string.Format("Name: {0} - Age: {1}", this.Name, this.Age);
} 

Then you can just do

lstStudents.Items.Add(studentList[i]);

The benefit of adding an object instead of a string is that the SelectedItem property of the listbox will be the object instead of just the string representation.

Or you can format the string that's sent to the listbox:

lstStudents.Items.Add(string.Format("Name: {0} - Age: {1}", studentList[i].Name, studentList[i].Age));
于 2013-10-30T18:31:01.907 回答
0
// Class student - create property
public string Display
{
   get { return string.Format("ID: {0} - Age: {1}", this.ID, this.Age); }
} 

 // In the form class - set listbox
 lstStudents.Datasource = StudentArray;
 lstStudents.DisplayMember = "Display";
 lstStudents.ValueMember = "ID";

 // The benefit of this is that later you can do this
 Student s = (Student)lstStudents.SelectedItem
 // Now you have access to full student info
于 2013-10-30T18:48:43.663 回答
0

仔细看看这一行:

lstStudents.Items.Add(studentList.ToString()[i]);

你写studentList.ToString()[i]。这意味着你打电话ToStringstudentList不是学生。它应该是lstStudents.Items.Add(studentList[i].ToString())(索引器[i]放在后面studentList)。

假设您已ToStringStudent课堂上覆盖,您可以在一次调用中添加所有学生:

private void Form1_Load(object sender, EventArgs e)
{
    Student[] studentList;
    studentList = new Student[4];

    studentList[0] = new Student(51584, 17);
    studentList[1] = new Student(51585, 19);
    studentList[2] = new Student(51586, 15);
    studentList[3] = new Student(51587, 20);

    lstStudents.Items.AddRange(studentList);
}
于 2013-10-30T19:07:34.113 回答