简体   繁体   English

更新时,如何检查listBox中的项目是否已经存在?

[英]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: 这是我刷新和更新listBox列表的方式:

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. 我在form1构造函数中然后在其他两个地方调用此方法一次:单击按钮事件和倒计时的计时器滴答事件。

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. 如果有新的窗口(项目),如果上次删除了某些项目,则将它们添加到listBox中,然后将它们从listBox中删除而不清除listBox和pictureBox只是根据WindowSnap.GetAllWindows(true)的方式添加/删除项目。 ,true).ToArray()已更改。

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 ? 另一个问题是,例如,如果我删除一个窗口,例如,我打开了一个新的Chrome窗口(不是选项卡,而是窗口),则listBox已更新,我单击了该项目并看到了它的快照,但是如果我关闭了该窗口怎么办? How the pictureBox with the image inside of this item will know now not to show it ? 现在如何知道其中包含图像的pictureBox不显示它? 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() 我想以其他方式根据WindowSnap.GetAllWindows(true,true).ToArray()的更新来更新listBox和pictureBoxSnap

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 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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