2015-09-20 34 views
0

我正在處理窗體窗體應用程序。我有一個組合框和一個列表框。在我的組合框中,我有5個項目,我想顯示從組合框到列表框中選擇的相應項目。如何顯示從組合框到列表的對應項目框

假設我選擇第1項,它會顯示項目1.如果我選擇項目2,它將顯示項目2和項目1將消失,反之亦然。到目前爲止,我已經嘗試了這個代碼

listBox1.Items.Add(name) 

此列表框添加statment添加到列表框中的新項目,如項目1,項目2,項目3等等,這不是我想要的。

using System.IO; 
using System; 

class Program 
{ 
    static void Main() 
    { 
     Console.WriteLine("Hello, World!"); 


     private void fill_checkListBox() 
     { 
      myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True"); 

      try 
      { 
       myConn.Open(); 

       int index = applicationComboBox.SelectedIndex + 1; 
       string query = " SELECT td.chineseName, ad.applicationId, aud.applicationId, ad.applicationName FROM[AppUser_Detail] as aud LEFT OUTER JOIN[Teacher_Detail] as td ON aud.teacherId = td.teacherId LEFT OUTER JOIN[Application_Detail] as ad ON aud.applicationId = ad.applicationId LEFT OUTER JOIN[Class_Detail] as cd ON aud.classId = cd.classId where aud.applicationId = '" + index + "' AND NOT(td.teacherId IS NULL AND cd.classId IS NULL)"; 
       myCommand = new SqlCommand(query, myConn); 

       SqlDataReader dr = myCommand.ExecuteReader(); 

       //Reading all the value one by one 
       while (dr.Read()) 
       { 
        //column is 1 in Application_Detail Data 



         //string value = applicationComboBox.GetItemText(applicationComboBox.Items[i]); 
         string name = dr.GetString(0); 
teacherCheckListBox.Items.Clear();\ updated this line teacherCheckListBox.Items.Add(name); 

       } 
       //teacherCheckListBox.DataSource = dt; 
       myConn.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 
} 

回答

1

試試這個代碼...

private void fill_checkListBox() 
{ 
    myConn = new SqlConnection("Server = localhost; Initial Catalog= ChungSingDB; Trusted_Connection = True"); 

    try 
    { 
     myConn.Open(); 

     int index = applicationComboBox.SelectedIndex; 
     string query = " SELECT td.chineseName, ad.applicationId, aud.applicationId, ad.applicationName,class, secondary FROM[AppUser_Detail] as aud LEFT OUTER JOIN[Teacher_Detail] as td ON aud.teacherId = td.teacherId LEFT OUTER JOIN[Application_Detail] as ad ON aud.applicationId = ad.applicationId LEFT OUTER JOIN[Class_Detail] as cd ON aud.classId = cd.classId where aud.applicationId = '" + index + "' AND NOT(td.teacherId IS NULL AND cd.classId IS NULL)"; 
     myCommand = new SqlCommand(query, myConn); 

     SqlDataReader dr = myCommand.ExecuteReader(); 

     teacherCheckListBox.Items.Clear(); 


     while (dr.Read()) 
     { 

      string name = ""; 

      name = dr.GetString(0); 

      if (applicationComboBox.SelectedIndex == index) 
      { 

       teacherCheckListBox.Items.Add(name); 

      } 

     } 
     myConn.Close(); 
    } 
    catch (Exception ex) 
    { 
    MessageBox.Show(ex.Message); 
    } 
} 
+0

謝謝大家的幫助,這是正確的答案:) – RedRocket

1

使用listbox1.Items.Clear(),這將清除整個列表,然後將新鮮的項目添加到列表框中。

1
alot of ways to do this 
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     listBox1.Items.Clear(); 
     listBox1.Items.Add(comboBox1.Text); 
    } 
相關問題