[英]How can i check if items in the listBox already exist when i update it?
這是我刷新和更新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();
}
我正在清除列表框,正在清除圖片框,然后將項目再次添加到列表框中:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
我在form1構造函數中然后在其他兩個地方調用此方法一次:單擊按鈕事件和倒計時的計時器滴答事件。
相反,我想更改此方法,以便它將檢查是否有任何新項目或從中刪除的項目:
WindowSnap.GetAllWindows(true, true).ToArray()
如果有新的窗口(項目),如果上次刪除了某些項目,則將它們添加到listBox中,然后將它們從listBox中刪除而不清除listBox和pictureBox只是根據WindowSnap.GetAllWindows(true)的方式添加/刪除項目。 ,true).ToArray()已更改。
所以我以后可以刪除/刪除這兩行:
this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;
編輯:
另一個問題是,例如,如果我刪除一個窗口,例如,我打開了一個新的Chrome窗口(不是選項卡,而是窗口),則listBox已更新,我單擊了該項目並看到了它的快照,但是如果我關閉了該窗口怎么辦? 現在如何知道其中包含圖像的pictureBox不顯示它? 我不想做:
this.listBoxSnap.Items.Clear();
this.pictureBoxSnap.Image = null;
因為它正在眨眼。 我想以其他方式根據WindowSnap.GetAllWindows(true,true).ToArray()的更新來更新listBox和pictureBoxSnap
刪除舊的:
IEnumerable<TypeIDontKnow> someCollection = WindowSnap.GetAllWindows(true, true).ToArray();
foreach (var item in listBoxSnap.Items)
{
if (!someCollection.Contains(item))
listBoxSnap.Items.Remove(item);
}
添加新的:
listBoxSnap.Items.AddRange(someCollection.Where(x => !listBoxSnap.Contains(x))
我把這個寫出來了-我不確定演員表是否還好等
您可以搜索該項目是否存在並替換。
// 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.");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.