0
votes

I am working on a window form application. I have a comboBox and a listBox. In my comboBox, I have 5 items in it and I want to display the according item that I selected from the comboBox to a listBox.

Let say if I select item 1, it will show item 1. If I select item 2, it will show item 2 and item 1 will disappear and vice versa. So far, I have tried this code

listBox1.Items.Add(name)

This ListBox add statment adds the new item to the listBox such as item1, item2, item3 and so on, which is not what I want.

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

3 Answers

1
votes

Use listbox1.Items.Clear(), this will clear entire list, then add your fresh item in listbox.

1
votes

Try this code...

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); 
    } 
}
1
votes
alot of ways to do this 
  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
        listBox1.Items.Add(comboBox1.Text);
    }