【问题标题】:How to delete object from combobox?如何从组合框中删除对象?
【发布时间】:2012-07-19 07:52:11
【问题描述】:

我有一个带有Foo 类型对象的组合框,这里是Foo 类:

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

Foo.name 是组合框中显示的文本,Foo.path 是所选选项的值。

我想在完成一些操作后从组合框中删除一个选项。

我试过这些方法:

  • 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); 
    

没有什么对我有用。 : / 如何做到这一点?

更新

这就是我初始化组合框的方式:

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

【问题讨论】:

  • 您是在放入对象吧?那么你为什么要删除 .Text 和 .ToString() 呢?尝试使用 Items.Find (of FindExact) 找到正确的对象并尝试删除其结果。伪:comboBox2.Items.Remove(comboBox2.Items.Find(fooItem.Text));
  • 这里的另一件事是您如何填充组合框?如果您是 dataBinding,那么请尝试从底层源而不是从组合中删除该项目...
  • @Gerald Versluis 我尝试通过以下方式做到这一点:comboBox2.Items.RemoveAt(comboBox2.Items.IndexOf(comboBox2.Text));comboBox2.Items.RemoveAt(comboBox2.Items.IndexOf(comboBox2.SelectedValue)); - 每次我看到一个错误..
  • System.ArgumentException: Items collection cannot be modified when the DataSource property is set. - 与我的删除命令一致
  • 这就是我问“你如何填充你的 ComboBox”的原因。您将需要从 DataSource 中删除该项目并重新绑定 ComboBox,直接从组合中删除将不起作用...

标签: c# winforms combobox


【解决方案1】:

comboBox2.Items.Remove(comboBox2.SelectedValue);只会从组合框中删除,而不是从绑定到组合框的数据源中删除。您可以将其从数据源中删除并重新绑定数据源。

【讨论】:

  • 无需重新绑定数据源,只需从数据源中删除,组合框会自动更新
  • @Stig 如果源有合适的事件
  • 在带有.NET Framework 4.8的Visual Studio 2019中,如果绑定了ComboBox.DataSource(即所谓的绑定模式),那么ComboBox.Items.Remove或RemoveAt会导致System.ArgumentException。在这种情况下,您必须重新绑定 ComboBox.DataSource。
【解决方案2】:

使用ComboBox.SelectedIndex 属性。

例如:让我将comboBox1 添加到表单中。在删除按钮中:

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

【讨论】:

    【解决方案3】:
    combox1.Remove(takes an object)
    Object selectedItem = comboBox1.SelectedItem;
    

    所以你可以这样做combox1.Remove(selectedItem);

    【讨论】:

      【解决方案4】:

      假设您要按索引删除项目:

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

      假设您想通过搜索成员值来删除

          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】:

        这 2 个命令将从您的数据源中删除一个项目。

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

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

        【讨论】:

          【解决方案6】:

          我认为秘诀是首先将 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;
          }
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-01-16
              • 1970-01-01
              • 2010-10-19
              • 2017-06-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多