Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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.
Posted
Updated 11-May-12 11:09am
v3

1 solution

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.
C#
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");

Alternatively, Any extension method can be used as follows:
VB
if (!checkedListBox1.Items.Cast<string>().Any (s =>
            (listBox1.Items.Cast<string>().Any (ls => ls.Equals(s, StringComparison.InvariantCultureIgnoreCase)))))
           MessageBox.Show("Not found");
 
Share this answer
 
v2
Comments
Sandeep Mewara 11-May-12 6:02am    
My 5! Cool.
VJ Reddy 11-May-12 6:24am    
Thank you, Sandeep.
MR. AngelMendez 11-May-12 6:23am    
I tried the code but it wont work. I updated my question with all of my explanation and code. Think you can figure out the problem? thanks.
VJ Reddy 11-May-12 8:44am    
The reason may be that in button1_click method
timer1.Enabled.Equals(true); does not enable the timer. It is to be
timer1.Enabled=true; and in timer1_tick method

if (timerstart == true)
{
timer = timer = -1;
}
assigns -1 twice to timer and the final value is -1, so that the code in the if block
if (timer == 0)

is not executed.
Maciej Los 11-May-12 17:11pm    
Very good answer! Linq is a powerful tool! My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900