求解答思路,button按钮该如何编写在文本框上显示结果,班级在哪里编写相对应的学生
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
class Stu
{
public string Name { get; set; }
public string Class { get; set; }
}
Stu[] stu = new Stu[12];
private void Form2_Load(object sender, System.EventArgs e)
{
for (int i = 0; i < stu.Length; i++)
{
stu[i] = new Stu();
stu[i].Name = $"Nmae{i}";
stu[i].Class = $"Class{i}";
listBox1.Items.Add(stu[i].Name);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
if (listBox1.SelectedIndex == -1) return;
string sel = listBox1.SelectedItem.ToString();
for (int i = 0; i < stu.Length; i++)
{
if (stu[i].Name == sel)
{
label1.Text = stu[i].Class;
break;
}
}
}
}