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?