且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查列表框是否不包含复选框中的任何项目?

更新时间:2022-01-01 07:29:08

以下代码可以在Click 中使用Button的事件,以查找ListBox中是否有CheckedListBox 中的任何项目.如果可用,则布尔标志notFound 设置为false.
The following code can be used in the Click event of a Button, to find if any of the items in CheckedListBox is available in the ListBox. If available then the boolean flag notFound is set to false.
bool notFound = true;
checkedListBox1.Items.Cast<string>().Select (s => {
    if (listBox1.Items.Cast<string>().Any (ls => ls.Equals(s, StringComparison.InvariantCultureIgnoreCase)))
        notFound = false;
    return s; });
if (notFound)
     MessageBox.Show("Not found");


或者,可以按以下方式使用Any 扩展方法:


Alternatively, Any extension method can be used as follows:

if (!checkedListBox1.Items.Cast<string>().Any (s =>
            (listBox1.Items.Cast<string>().Any (ls => ls.Equals(s, StringComparison.InvariantCultureIgnoreCase)))))
           MessageBox.Show("Not found");