Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows checkbox ListBox , +
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.
Posted 11-May-12 0:27am
Edited 11-May-12 12:09pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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");
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");
  Permalink  
v2
Comments
Sandeep Mewara at 11-May-12 6:02am
   
My 5! Cool.
VJ Reddy at 11-May-12 6:24am
   
Thank you, Sandeep.
MR. AngelMendez at 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 at 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.
losmac at 11-May-12 17:11pm
   
Very good answer! Linq is a powerful tool! My 5!
VJ Reddy at 11-May-12 19:58pm
   
Thank you, losmac.
MR. AngelMendez at 11-May-12 18:26pm
   
After switching code around I have been able to get my timer running but the problem now is that when the timer runs out and my process list updates it always gives me the messagebox("not found"); when the process is still running. Is there some way to post my project so you can download it and see whats wrong? Just need a little more help, thanks.

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



Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 11 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100