【问题标题】:How can i check if items in the listBox already exist when i update it?更新时如何检查列表框中的项目是否已存在?
【发布时间】:2014-08-24 00:23:19
【问题描述】:

这就是我刷新和更新 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()

如果有新的窗口(项目)将它们添加到列表框中,如果上次删除了一些项目,则将它们从列表框中删除而不清除列表框,图片框只是根据 WindowSnap 的方式和是否添加/删除项目。 GetAllWindows(true, true).ToArray() 已更改。

所以我可以稍后删除/删除这两行:

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

编辑:

另一个问题是,如果我删除了一个窗口,例如我打开了一个新的 chrome 窗口(不是选项卡,而是窗口),列表框已更新我点击了这个项目并看到它的快照,但是如果我关闭这个窗口我该怎么办?这个item里面有图片的pictureBox怎么会知道现在不显示呢?我不想做:

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

因为它在不停地闪烁。我想以某种方式根据 WindowSnap.GetAllWindows(true, true).ToArray()

的变化来更新 listBox 和 pictureBoxSnap

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    删除旧的:

    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))
    

    我是在脑海中写下这个 - 我不确定演员阵容是否正常等等。

    【讨论】:

      【解决方案2】:

      您可以搜索该项目是否存在并替换它。

      // 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多