简体   繁体   English

如何从组合框中删除对象?

[英]How to delete object from combobox?

I have a combobox with objects of Foo type, here is the Foo class:我有一个带有Foo类型对象的组合框,这是Foo类:

public class Foo
{
    public string name { get; set; }
    public string path { get; set; }
}

The Foo.name is the displayed text in the combobox and Foo.path is the value of the selected option. Foo.name是组合框中显示的文本, Foo.path是所选选项的值。

I want to delete an option from the combobox after some operation I made.我想在我做了一些操作后从组合框中删除一个选项。

I've tried these ways:我试过这些方法:

  • 1 1

     comboBox2.Items.Remove(@comboBox2.Text);
  • 2 2

     comboBox2.Items.Remove(@comboBox2.SelectedValue.ToString());
  • 3 3

     Foo ToDelete = new Foo(); ToDelete.name = @comboBox2.Text; ToDelete.path = @comboBox2.SelectedValue.ToString(); comboBox2.Items.Remove(ToDelete);

Nothing works for me.没有什么对我有用。 : / How to do this? : / 这个怎么做?

UPDATE更新

This is how I'm initializing my combobox:这就是我初始化组合框的方式:

    string[] filePaths = Directory.GetFiles(sites.paths[comboBox1.SelectedIndex]);

        List<Foo> combo2data = new List<Foo>();

        foreach (string s in filePaths)
        {
            Foo fileInsert = new Foo();
            fileInsert.path = s;
            fileInsert.name = Path.GetFileName(s);
            combo2data.Add(fileInsert);
        }

        comboBox2.DataSource = combo2data;
        comboBox2.ValueMember = "path";
        comboBox2.DisplayMember = "name";

comboBox2.Items.Remove(comboBox2.SelectedValue); comboBox2.Items.Remove(comboBox2.SelectedValue); will only remove from the combobox, not from the datasource bound to the combobox.只会从组合框中删除,而不是从绑定到组合框的数据源中删除。 You may remove it from the datasource and re-bind the datasource.您可以将其从数据源中删除并重新绑定数据源。

Use ComboBox.SelectedIndex property.使用ComboBox.SelectedIndex属性。

For example: let me have comboBox1 added to the form.例如:让我将comboBox1添加到表单中。 In the delete button:在删除按钮中:

if (comboBox1.SelectedIndex >= 0)
    comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
combox1.Remove(takes an object)
Object selectedItem = comboBox1.SelectedItem;

So you cna do it this way combox1.Remove(selectedItem);所以你可以这样做combox1.Remove(selectedItem);

Suppose you want to Remove Items by Index:假设您想按索引删除项目:

    combo2data.RemoveAt(0); //Removing by Index from the dataSource which is a List

    //Rebind
    comboBox2.DataSource = null;
    comboBox2.DataSource = combo2data;  
    comboBox2.ValueMember = "path";  
    comboBox2.DisplayMember = "name";  

Suppose you want to Remove by seraching for a member value假设您想通过搜索成员值来删除

    Foo item = combo2data.Where(f => f.name.Equals("Tom")).FirstOrDefault();
    if (item != null)
    {
        combo2data.Remove(item);
        comboBox2.DataSource = null;
        comboBox2.DataSource = combo2data;  
        comboBox2.ValueMember = "path";  
        comboBox2.DisplayMember = "name";  
    }

These 2 commands will remove an item from your data source.这 2 个命令将从您的数据源中删除一个项目。

list.Remove((Foo)comboBox1.SelectedItem);

or要么

list.Remove(list.Find(P=>P.name == comboBox1.SelectedText));

I think the secret is to first attribute null to the datasource and after rebind to a modified collection:我认为秘诀是首先将 null 属性分配给数据源,然后重新绑定到修改后的集合:

int idToRemove = 1;
var items = (cbx.DataSource as List<MyEntity>);
items.RemoveAll(v => v.Id == idToRemove);
rebindCombobox(cbx, items, "Name", "Id");


private void rebindCombobox(ComboBox cbx, IEnumerable<Object> items, String displayMember, String valueMember)
{
    cbx.DataSource = null;
    cbx.DisplayMember = displayMember;
    cbx.ValueMember = valueMember;
    cbx.DataSource = items;
}

也许用comboBox.Items.Clear();删除组合框中的所有项目comboBox.Items.Clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM