![](/img/trans.png)
[英]Remove the item from ComboBox 2 that was selected from ComboBox 1
[英]remove selected item from combobox in winform application
我正在处理 windows 表格申请..
我有两个组合框..我将我的组合框drop down style
属性更改为DropDownList
保存数据后我想清除组合框中的项目..所以我给出了这样的代码:
CmbDepartment.Text = "";
cmbvisitpurpose.Text = "";
但这并没有从我的 combobox 中清除所选项目..所以我改变了这样的代码:
cmbvisitpurpose.Items.RemoveAt(cmbvisitpurpose.SelectedIndex = -1)
CmbDepartment.Items.RemoveAt(CmbDepartment.SelectedIndex = -1)
这是从我的 combobox 中永久删除特定项目..如果我想获取组合框中的所有项目..agian 我想加载页面..我只想清除所选项目..我该怎么做?
这只会从组合框中删除,而不会从数据源中删除。
如果要保留项目,最好使用本地集合。
CmbDepartment.Items.Remove(CmbDepartment.SelectedItem);
这是有关如何将值分配给集合的示例
List<string> DepartmentsPermanent;
List<string> DepartmentsTemporary;
public Form1()
{
InitializeComponent();
DepartmentsPermanent = new List<string>();
DepartmentsPermanent.Add("EEE");
DepartmentsPermanent.Add("CSE");
DepartmentsPermanent.Add("E&I");
DepartmentsPermanent.Add("Mechanical");
comboBox1.DataSource = DepartmentsPermanent;
//here you assign the values to other List
DepartmentsTemporary = DepartmentsPermanent.ToList();
}
//Now if you have selected EEE from the list and you want to remove on selection
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null && DepartmentsTemporary != null)
{
DepartmentsTemporary.Remove(comboBox1.SelectedItem.ToString());
comboBox1.DataSource = DepartmentsTemporary;
}
//If you want to assign the default values again you can just assign the PermanentList
//comboBox1.DataSource = DepartmentsPermanent;
}
如果要清除选定的项目,则应将ComboBox.SelectedIndex
属性设置为-1
一种方法是:
ComboBox1.SelectedItem = null;
我想这种方法不仅会删除文本,还会删除整个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.