2014-04-14 162 views
0

您好我創建了學生和檔次的數組我的問題是 我如何使它所以如果我從指數0選擇學生山姆和展示自己的成績的下拉框裏面的第一陣列。我要寫什麼代碼才能將它們結合在一起。如果你能幫助我,我會非常感激。組合框陣列選擇顯示

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; 
    } 
+1

你可能會更好做了'Student'類和創造他們的名字和等級屬性。 –

+0

我有兩個組合框,當我選擇一個學生時,有沒有第二個組合框顯示他們評分的數組的方法? – TheBoringGuy

+0

沒人能幫助我嗎? – TheBoringGuy

回答

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爲DropDownList的組合框屬性,因此用戶不能編輯它,像(上加載時間):

cboStudent.DropDownStyle = ComboBoxStyle.DropDownList; 
+1

哇,你驚人的人感謝很多:) – TheBoringGuy

+0

沒問題。隨時交配。 –

相關問題