更新时间:2022-01-01 07:29:08
以下代码可以在Click
中使用Button
的事件,以查找ListBox
中是否有CheckedListBox
中的任何项目.如果可用,则布尔标志notFound
设置为false
.
The following code can be used in theClick
event of aButton
, to find if any of the items inCheckedListBox
is available in theListBox
. If available then the boolean flagnotFound
is set tofalse
.
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");