How to delete object from combobox?

☆樱花仙子☆ 提交于 2019-12-20 04:16:28

问题


I have a combobox with objects of Foo type, here is the Foo class:

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.

I want to delete an option from the combobox after some operation I made.

I've tried these ways:

  • 1

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

    comboBox2.Items.Remove(@comboBox2.SelectedValue.ToString());  
    
  • 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";

回答1:


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.




回答2:


Use ComboBox.SelectedIndex property.

For example: let me have comboBox1 added to the form. In the delete button:

if (comboBox1.SelectedIndex >= 0)
    comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);



回答3:


combox1.Remove(takes an object)
Object selectedItem = comboBox1.SelectedItem;

So you cna do it this way combox1.Remove(selectedItem);




回答4:


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



回答5:


These 2 commands will remove an item from your data source.

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

or

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



回答6:


I think the secret is to first attribute null to the datasource and after rebind to a modified collection:

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


来源:https://stackoverflow.com/questions/11556216/how-to-delete-object-from-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!