如何将项目从组合框显示到列表框
How to display an item from comboBox to a list Box
我正在开发 window form
应用程序。我有一个 comboBox
和一个 listBox
。在我的组合框中,我有 5 个项目,我想显示我从组合框 select 编辑到列表框的相应项目。
假设我 select 项目 1,它将显示项目 1。如果我 select 项目 2,它将显示项目 2,而项目 1 将消失,反之亦然。到目前为止,我已经尝试过这段代码
listBox1.Items.Add(name)
这个ListBox add语句向listBox中添加item1、item2、item3等新项,这不是我想要的。
代码。
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();\ 更新了这一行 teacherCheckListBox.Items.Add(name);
}
//teacherCheckListBox.DataSource = dt;
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
有什么想法吗?
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;
}
您可以使用以下代码清除列表框并仅插入一项。
private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
{
lstSample.Items.Clear();
lstSample.Items.Add(cmbSample.SelectedItem);
lstSample.SelectedItem = cmbSample.SelectedItem;
}
然而这里 lstSample.Items.Clear();
清除了列表框中的任何内容。如果你不想要那个,那么你可以避开这一行并继续剩下的两行,这将在现有列表框中插入一个新项目,并将 select 新添加的项目。
private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
{
lstSample.Items.Add(cmbSample.SelectedItem);
lstSample.SelectedItem = cmbSample.SelectedItem;
}
我正在开发 window form
应用程序。我有一个 comboBox
和一个 listBox
。在我的组合框中,我有 5 个项目,我想显示我从组合框 select 编辑到列表框的相应项目。
假设我 select 项目 1,它将显示项目 1。如果我 select 项目 2,它将显示项目 2,而项目 1 将消失,反之亦然。到目前为止,我已经尝试过这段代码
listBox1.Items.Add(name)
这个ListBox add语句向listBox中添加item1、item2、item3等新项,这不是我想要的。
代码。
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();\ 更新了这一行 teacherCheckListBox.Items.Add(name);
}
//teacherCheckListBox.DataSource = dt;
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
有什么想法吗?
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;
}
您可以使用以下代码清除列表框并仅插入一项。
private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
{
lstSample.Items.Clear();
lstSample.Items.Add(cmbSample.SelectedItem);
lstSample.SelectedItem = cmbSample.SelectedItem;
}
然而这里 lstSample.Items.Clear();
清除了列表框中的任何内容。如果你不想要那个,那么你可以避开这一行并继续剩下的两行,这将在现有列表框中插入一个新项目,并将 select 新添加的项目。
private void cmbSample_SelectedValueChanged(object sender, EventArgs e)
{
lstSample.Items.Add(cmbSample.SelectedItem);
lstSample.SelectedItem = cmbSample.SelectedItem;
}