c# - ListBox.Contains 无法按预期工作

标签 c# listbox

我的代码中有 2 个错误,不知道如何解决。

这是我的代码:

    private void add_button_Click(object sender, EventArgs e)`
    {
        try
        {

            if (list_selected.Contains(List_selection.SelectedItem))
            {
                MessageBox.Show("Can't add the same type twice");
            }
            else
            {
                list_selected.Items.Add(List_selection.SelectedItem);
            }
        }
        catch 
        {

            {
                MessageBox.Show("No type selected");
            }

        }
    }

这些是错误:

错误 1

The best overloaded method match for 'System.Windows.Forms.Control.Contains(System.Windows.Forms.Control)' has some invalid arguments

错误 2

Argument 1: cannot convert from 'object' to 'System.Windows.Forms.Control' C:\Projects\flashloader2013\mainapplication\Form1.cs 467 44 Main

请帮帮我。 ]

List_selectionlist_selectedListBoxes

最佳答案

你需要写:

if (list_selected.Items.Contains(List_selection.SelectedItem))

否则你检查 listView/Listbox 的控件集合(任何可能包含其他控件的控件)

关于c# - ListBox.Contains 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17104033/

相关文章:

c# - SqlBulkCopy ColumnMapping 错误

c# - 在 WPF 的代码隐藏中为 ListBox 创建 ItemTemplate - 第 II 部分

c# - 如何以编程方式选择列表框中的项目?

c# - 错误 TS1219 : Experimental support for decorators is a feature that is subject to change in a future release

c# - 如何在机器人构建器框架 4.0 中发送不按顺序的消息和回复

c# - 单击链接事件 webbrowser c#?

长度为零时的 HTML 列表框宽度

c# - 如何使用 Math.Max、Math.Min 找到三个最大和三个最小的小数

vb.net - 检查 listbox1 中是否已存在某个项目

jQuery 下拉列表