【问题标题】:How to display an correspond item from comboBox to a list Box如何从组合框显示对应的项目到列表框
【发布时间】:2015-12-17 00:12:00
【问题描述】:

我正在开发一个窗口窗体应用程序。我有一个组合框和一个列表框。在我的组合框中,我有 5 个项目,我想将我从组合框中选择的相应项目显示到列表框。

假设如果我选择第 1 项,它将显示第 1 项。如果我选​​择第 2 项,它将显示第 2 项,第 1 项将消失,反之亦然。到目前为止,我已经尝试过这段代码

listBox1.Items.Add(name)

这个ListBox添加语句将新的item添加到listBox如item1、item2、item3等,这不是我想要的。

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);
            }
        }
    }
}

【问题讨论】:

    标签: c# combobox


    【解决方案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); 
        } 
    }
    

    【讨论】:

    • 谢谢大家的帮助,这是正确答案:)
    【解决方案2】:

    使用listbox1.Items.Clear(),这将清除整个列表,然后将您的新鲜项目添加到列表框中。

    【讨论】:

      【解决方案3】:
      alot of ways to do this 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
          {
              listBox1.Items.Clear();
              listBox1.Items.Add(comboBox1.Text);
          }
      

      【讨论】:

        猜你喜欢
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多