您好我创建了一系列学生和成绩我的问题是 如果我从索引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;
}
答案 0 :(得分:0)
我会用这种方法:
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;