使用它的成员搜索对象并将其删除

时间:2014-02-17 10:48:03

标签: c# wpf list

我有一个comboBox,它显示了一个对象的名称。这些对象是从列表中添加的。

     private List<Tool> toolList = new List<Tool>();

     private void buttonAdd_Click(object sender, RoutedEventArgs e)
    {
        InputDialog input = new InputDialog();
        input.ShowDialog();
        inputNewTool = input.enteredTxt;

        if (inputNewTool != null)
        {
            System.Windows.Forms.MessageBox.Show("Chose the Tool's directory");
            dlg.DefaultExt = ".exe";
            dlg.Filter = "Application (.exe)|*.exe";

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Tool tool = new Tool();
                tool.Name = inputNewTool;
                tool.Path = dlg.FileName;
                toolList.Add(tool);
                comboBoxTools.Items.Add(tool.Name);
            }                
       }            
     }

现在我想要一个删除按钮,不仅从comboBox中删除了对象名称。必须从列表中删除具有此名称的对象。

private void buttonRemove_Click(object sender, RoutedEventArgs e)
    {
        int position = 0;
        for (int i = 0; i < comboBoxTools.Items.Count; i++)
        {
            position = comboBoxTools.SelectedIndex;
            removeTool = comboBoxTools.Items[position].ToString();
        }
        if (position != -1)
        {
            for (int i = toolList.Count - 1; i >= 0; --i)
            {
                if (Condition?)
                {
                    toolList.RemoveAt(i);
                }
            }
            //foreach (Tool t in toolList)
            //{

            //}
            comboBoxTools.Items.RemoveAt(position);
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("No Item choosed");
        }
    }

我不知道如何实现这一目标 编辑:可能是comboBox没有提供DataSource或DisplayMember吗?我需要包含哪些内容才能使用它们?

3 个答案:

答案 0 :(得分:3)

不是按索引删除项目,只需按值删除项目:

string name = comboBoxTools.SelectedItem;
toolList.RemoveAll(t => t.Name == name);
comboBoxTools.Items.Remove(name);

我还建议您查看BindingList<T>对工具的使用情况,并将此列表直接绑定到ComboBox:

BindingList<Tool> tools = new BindingList<Tool>();

private void YourForm_Load(object sender, EventArgs e)
{
    comboBoxTools.DisplayMember = "Name"; // set display member
    comboBoxTools.DataSource = tools; // bind tools to comboBox
}

private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
    // ... create tool 

    // that will add tool both to tools list and comboBox
    tools.Add(tool);
}

private void buttonRemove_Click(object sender, RoutedEventArgs e)
{
    Tool tool = comboBoxTools.SelectedItem as Tool;
    if(tool == null)
       return;

    // that will remove tool both from tools list and comboBox
    tools.Remove(tool);
}   

答案 1 :(得分:2)

问题:您正在使用RemoveAt()方法从ComboBox中删除项目。

RemoveAt()方法的问题是它总会重新调整Itmes指数的位置。

解决方案:您可以使用ComboBox.Items.Remove()方法删除SelectedItem而不会头疼。

试试这个:您可以使用Remove()方法代替RemoveAt()

private void buttonRemove_Click(object sender, RoutedEventArgs e)
{
   comboBoxTools.Items.Remove(comboBoxTools.SelectedItem);
   toolList.Remove(comboBoxTools.SelectedItem);
}

答案 2 :(得分:1)

我认为你可以使用 ObservableCollection<StatisticsItem> Tools;

http://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

并设置:

comboBoxTools.ItemsSource = Tools

在你的构造函数中。

您的按钮添加:

  private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
    InputDialog input = new InputDialog();
    input.ShowDialog();
    inputNewTool = input.enteredTxt;

    if (inputNewTool != null)
    {
        System.Windows.Forms.MessageBox.Show("Chose the Tool's directory");
        dlg.DefaultExt = ".exe";
        dlg.Filter = "Application (.exe)|*.exe";

        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Tool tool = new Tool();
            tool.Name = inputNewTool;
            tool.Path = dlg.FileName;
            toolList.Add(tool);
            Tools.add(tool)
        }                
   }            
 }

将自动更新您的组合框和删除按钮:

private void buttonRemove_Click(object sender, RoutedEventArgs e)
{
    if(combobox.SelectedItem != null)
        Tools.remove((Tool) combobox.SelectedItem);
    else
        System.Windows.Forms.MessageBox.Show("No Item choosed");
}