如何检查列表框是否不包含复选框中的任何项目? [英] how to check if a listbox does not contain any items from a checklistbox?

查看:172
本文介绍了如何检查列表框是否不包含复选框中的任何项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个列表框和一个包含项目的复选框.当列表框不包含复选框列表中的任何项目时,我想显示一个消息框,谢谢.

我正在制作一个自动关闭应用程序,该应用程序将在完成处理后关闭.用户单击要监视的复选框上的项目将其添加到列表框中,然后单击按钮以启动计时器.然后,当计时器计时到时,它将通过将清单框与清单框项目进行比较来检查进程是否仍在运行.问题是即时通讯无法比较两个列表.我不确定这可能是计时器还是列表.请看一下我的代码.

Hi,

I have a listbox and a checklistbox with items in it. I want to show a messagebox when the listbox does not contain any items form the checklistbox, Thanks.

I''m making an auto shutdown application that will shutdown when a process is done. The user clicks the items on a checklistbox that they want to monitor the item is added to a listbox.Then you click on a button to start the timer. Then when the timer is up it will check to see if the processes is still running by compairing the listbox with the checklistbox items. The problem is im unable to compare both list. I''m not sure if it could be the timer or the lists. please take a look at my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int timer;
        bool timerstart = false;

        Process[] pro = Process.GetProcesses();

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Process prolist in pro)
            {
                checkedListBox1.Items.Add(prolist.ProcessName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled.Equals(true);
            timer1.Start();
            timerstart = true;
            MessageBox.Show("timer started");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timerstart == true)
            {
                timer = timer = -1;
            }
            if (timer == 0)
            {
                checkedListBox1.Items.Clear();

                foreach (Process prolist in pro)
                    checkedListBox1.Items.Add(prolist.ProcessName);
                {
                    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");
                    {
                        timer = timer + 5;
                    }
                }
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timerstart = false;
            MessageBox.Show("timer stopped");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Clear();

            Process[] pro = Process.GetProcesses();
            foreach (Process prolist in pro)
            {
                checkedListBox1.Items.Add(prolist.ProcessName);
            }
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            listBox1.Items.Add(checkedListBox1.SelectedItem);
        }
    }
}






我知道它很凌乱,有更好的方法可以做到,但是我是个初学者,谢谢.






I know its messy and there is a better way to do this but I''m a beginner, Thanks.

推荐答案

以下代码可以在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");


这篇关于如何检查列表框是否不包含复选框中的任何项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆