简体   繁体   中英

How can i check if items in the listBox already exist when i update it?

This is how i refresh and update the listBox List:

private void RefreshWindowsList()
        {
            ClearGraphics = true;
            this.listBoxSnap.Items.Clear();
            this.pictureBoxSnap.Image = null;
            buttonSnap.Enabled = false;
            this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
            buttonSnap.Enabled = true;
            for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
            {
                string tt = listBoxSnap.Items[i].ToString();
                if (tt.Contains(" ,"))
                {
                    listBoxSnap.Items.RemoveAt(i);
                }
            }
            rectangles = new Rectangle[listBoxSnap.Items.Count];
            textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
            if (this.listBoxSnap.Items.Count > 0)
                this.listBoxSnap.SetSelected(0, true);
            listBoxSnap.Select();
        }

I'm clearing the listbox i'm clearing the pictureBox and then i'm adding again the items to the listbox:

this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());

I'm calling this method once in the form1 constructor then in other two places: A click button event and in a timer tick event that count back.

Instead i want to change this method so it will check if there are any new items or removed items from :

WindowSnap.GetAllWindows(true, true).ToArray()

If there are new windows(items) add them to the listBox if some items removed from the last time then remove them from the listBox without clearing the listBox and the pictureBox just add/remove items according to how and if the WindowSnap.GetAllWindows(true, true).ToArray() was changed.

So i can remove/delete this two lines later:

this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;

EDIT:

Another problem is if i remove a window for example i opened a new chrome window(not tab but window) the listBox was updated i clicked on this item and see it's snap shot taken but what do i do if i closed this window ? How the pictureBox with the image inside of this item will know now not to show it ? I don't want to make:

this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;

Cause it's making a huge blinking. I want to somehow else to update the listBox and pictureBoxSnap according to changes in WindowSnap.GetAllWindows(true, true).ToArray()

Remove old ones:

IEnumerable<TypeIDontKnow> someCollection = WindowSnap.GetAllWindows(true, true).ToArray();
foreach (var item in listBoxSnap.Items)
{
     if (!someCollection.Contains(item))
          listBoxSnap.Items.Remove(item);
}

Add new ones:

listBoxSnap.Items.AddRange(someCollection.Where(x => !listBoxSnap.Contains(x))

I write this out of my head - I'm not sure if the casts are ok etc.

You could search if the item exists and replace it.

// Set the search string:
string myString = "ITEM NAME";
// Search starting from index -1:
int index = listBox1.FindString(myString, -1);
if (index != -1)
{
    // Select the found item:
    listBox1.SetSelected(index, true);
    // Send a success message:
    MessageBox.Show("Found the item \"" + myString + "\" at index: " + index);
}
else
{
    MessageBox.Show("Item not found.");
}

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM