Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListBox.Contains does not work as expected

Tags:

c#

listbox

i have 2 errors in my code and can not figure out how to solve this.

This is my code:

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

        }
    }

These are the errors:

Error 1

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

Error 2

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

Please help me. ]

List_selection and list_selected are ListBoxes.

like image 522
The_Monster Avatar asked Dec 26 '22 02:12

The_Monster


1 Answers

You need to write:

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

Otherwise you check the controls collection of the listView/Listbox (whatever control that could contain other controls)

like image 133
Romano Zumbé Avatar answered Jan 12 '23 14:01

Romano Zumbé