组合框数组选择显示

本文关键字:显示 选择 数组 组合 | 更新日期: 2023-09-27 17:58:12

嗨,我创建了一个学生数组,并对我的问题进行了评分如果我从索引0中选择学生Sam,并在组合框中显示他的第一个成绩数组,我该如何做到这一点。我该写什么代码把它们联系在一起。如果你能帮助我,我将不胜感激。

    private void Form1_Load(object sender, EventArgs e)
    {
        string[] student = { "Sam", "Dean", "Nick", "Shara", "Kat" };
        cboStudent.Items.AddRange(student);
        cboStudent.SelectedIndex = 0;
        string[] S1Grade = { "Math: 78", "English: 56", "Bio: 94", "Art: 67", "Science: 64", "PE: 85", "Social: 89" };
        cboGrades.Items.AddRange(S1Grade);
        string[] S2Grade = { "Math: 45", "English: 84", "Bio: 56", "Art: 67", "Science: 78", "PE: 45", "Social: 76" };
        cboGrades.Items.AddRange(S2Grade);
        string[] S3Grade = { "Math: 68", "English: 34", "Bio: 98", "Art: 56", "Science: 55", "PE: 65", "Social: 56" };
        cboGrades.Items.AddRange(S3Grade);
        string[] S4Grade = { "Math: 38", "English: 78", "Bio: 76", "Art: 67", "Science: 34", "PE: 45", "Social: 56" };
        cboGrades.Items.AddRange(S4Grade);
        string[] S5Grade = { "Math: 88", "English: 84", "Bio: 56", "Art: 45", "Science: 56", "PE: 85", "Social: 89" };
        cboGrades.Items.AddRange(S5Grade);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        string selectedStudent = cboStudent.Text;
        txtSelected.Text = selectedStudent;
    }
    private void cboGrades_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedGrade = cboGrades.Text;
    }

组合框数组选择显示

我会使用这种方法:

 public Form1()
    {
        InitializeComponent();
        string[] student = { "Sam", "Dean", "Nick", "Shara", "Kat" };
        cboStudent.Items.AddRange(student);
        cboStudent.SelectedIndex = 0;           
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        cboGrades.Items.Clear();
        cboGrades.Items.AddRange(Selecting(cboStudent.SelectedIndex));
    }

    private string[] Selecting(int index)
    {
        string[] arr = null;
        switch (index)
        {
            case 0: arr = new[] { "Math: 78", "English: 56", "Bio: 94", "Art: 67", "Science: 64", "PE: 85", "Social: 89" }; break;
            case 1: arr = new[] { "Math: 45", "English: 84", "Bio: 56", "Art: 67", "Science: 78", "PE: 45", "Social: 76" }; break;
            case 2: arr = new[] { "Math: 68", "English: 34", "Bio: 98", "Art: 56", "Science: 55", "PE: 65", "Social: 56" }; break;
            case 3: arr = new[] { "Math: 38", "English: 78", "Bio: 76", "Art: 67", "Science: 34", "PE: 45", "Social: 56" }; break;
            case 4: arr = new[] { "Math: 88", "English: 84", "Bio: 56", "Art: 45", "Science: 56", "PE: 85", "Social: 89" }; break;
            default: arr = null; break;//no correct index;
        }
        return arr;
    }

它有什么好处吗?并将DropDownStyle的comboBoxes属性设置为DropDownList,这样用户就无法编辑它,比如(加载时):

   cboStudent.DropDownStyle = ComboBoxStyle.DropDownList;