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.

code.

using System.IO;
using System;

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

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

            try
            {
                myConn.Open();
                string query = "select * from Application_Detail";

                myCommand = new SqlCommand(query, myConn);

                //myCommand.ExecuteNonQuery();
                //reading the value from the query
                SqlDataReader dr = myCommand.ExecuteReader();
                //Reading all the value one by one
                while (dr.Read())
                {
                    //column is 1 in Application_Detail Data
                    string name = dr.GetString(1);
                    applicationComboBox.Items.Add(name);
                }
                myConn.Close();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

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

Any Ideas?

2
Do you want your list box to show only one item at a time, which was selected from combobox? and combobox will have 4 items max, as 1 will be moved to listbox?Amnesh Goel
Yeah something like that. You know that most of the comboBox in the pc? It only show the item what you selected from the comboBox.RedRocket

2 Answers

2
votes
   public Form1()
        {
            InitializeComponent();
            List<string> items = new List<string>();
            items.Add("sachin");
            items.Add("dravid");
            items.Add("ganguly");
            cmbSample.DataSource = items;
        }

        private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
        {
            lstSample.Items.Clear();
            lstSample.Items.Add(cmbSample.SelectedItem);
            lstSample.SelectedItem = cmbSample.SelectedItem;
        }
1
votes

You can use below code to clear the listbox and insert only one item.

private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
{
    lstSample.Items.Clear();
    lstSample.Items.Add(cmbSample.SelectedItem);
    lstSample.SelectedItem = cmbSample.SelectedItem;
}

However here lstSample.Items.Clear(); clears whatever was in the listbox. And if you don't want that, then you can avoid this line and continue with rest two lines which will insert a new item in existing listbox and will select the newly added item.

private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
{
    lstSample.Items.Add(cmbSample.SelectedItem);
    lstSample.SelectedItem = cmbSample.SelectedItem;
}