【问题标题】:how to check if the item [name & subItem's text] is already exists in another listView?如何检查项目 [name & subItem's text] 是否已存在于另一个 listView 中?
【发布时间】:2012-03-12 08:05:58
【问题描述】:

我制作了一个文件传输(服务器-客户端)应用程序 ..
我有两个 listviewS 来探索本地 PC 和远程 PC .. 在发送/接收项目之前..
我需要检查是否还有另一个文件或文件夹在目标路径中具有相同的名称.. 当我按下按钮 [发送或接收] 添加到列表中的项目.. 然后当我按下按钮 [开始传输] .. 它开始。

所以当我按下按钮 Receive 或 Send 时调用 AddItems 方法 .. 我从源 ListView 获得 SelectedItems .. 和目标 ListView 的 Items 。 ..然后我检查 SelectedItems 中的每个项目是否存在于 Items

我尝试使用

items.Contain(item)

但它不起作用,即使该项目已经存在,它总是给我错误。
所以我使用了 items.ContainKey 并且它有效.. 但是如果我有一个名为“Temp”的文件,没有扩展名,并且目标路径中的文件夹也名为“Temp”..它将返回 True ..这就是我的问题。 .

bool YesToAll = false;
public void AddItems(ListView.SelectedListViewItemCollection selectedItems, ListView.ListViewItemCollection items,TransferType type,string destPath)
{
        foreach(ListViewItem item in selectedItems)
        {
            if (items.ContainsKey(item.Name) && !YesToAll)
            {
                MyMessageBox msgbox = new MyMessageBox("Item is already exists .. Do you want to replace (" + item.Text + ") ?");
                msgbox.ShowDialog();
                if (msgbox.DialogResult == DialogResult.Yes)
                {
                    Add(item, type, destPath);
                }
                else if (msgbox.DialogResult == DialogResult.OK)
                {
                    YesToAll = true;
                    Add(item, type, destPath);
                }
                else if (msgbox.DialogResult == DialogResult.No)
                {
                    continue;
                }
                else
                {
                    return;
                }
            }
            else
            {
                Add(item, type, destPath);
            }
        }
        YesToAll = false;
    }
    private void Add(ListViewItem item,TransferType type,string path)
    {
        ListViewItem newItem = (ListViewItem)item.Clone();
        newItem.ImageIndex = imageList1.Images.Add(item.ImageList.Images[item.ImageIndex],Color.Transparent);
        newItem.SubItems.Add(type.ToString());
        newItem.SubItems.Add(path);
        newItem.Tag = type;
        listView1.Items.Add(newItem);
    } 

当用户点击确认对话框中的 [Yes to all] 按钮时,YesToAll 设置为 true。
TransferType 只是用于标记项目是否要使用 SendMethod 或 ReceiveMethod

public enum TransferType
    {
        Send , Receive
    };

那么我该如何解决这个问题.. 我应该使用自定义方法而不是 [Contains] 来检查名称和类型(文件或文件夹),因为每个项目已经有一个 subItem 来判断它是否是文件夹或文件

提前致谢。

【问题讨论】:

  • 您需要添加另一个函数来获取类型(文件或文件夹),然后在同一个 if 语句中使用它。
  • @PraVn 每个项目都有 subItem[1] .. 如果它是一个文件,它将是 [15 M.B] 的大小 .. 如果它是一个文件夹,它将是 [Folder] .. 我的意思是我已经有项目类型。
  • @TimSchmelter 项目的类型是 ListViewItem :)

标签: c# listview contains listviewitem


【解决方案1】:

一个快速的想法。

您可以利用您的标签属性来包含不仅仅是传输类型。

由于它可以包含对象,您可以创建一个自定义类,其中包含您的传输类型以及有关条目的更多信息。以 IsDirectory 为例,您可以在以后使用它。

希望有所帮助 萨沙

【讨论】:

  • 是的,实际上.. 我已经在所有项目中设置了 subItem[1] 的大小,如果它是一个文件,则为 ["15 M.B"],如果它是一个文件夹,则为 ["Folder"] .. 所以我可以检查 item.subItem[1] == "Folder" .. 但我试图避免为此创建自定义方法而不是 Contains。
  • 恐怕你可能不得不考虑你的方法。如果您设法将临时文件并排复制到临时文件夹,您的字典中可能会有两个由键“temp”组成的条目,这是无效的。无论如何-您是否考虑过使用 Linq ?请参阅此处获取灵感stackoverflow.com/questions/4505602/…
  • 所以您的意思是 Item.Name 应该是唯一的 .. 感谢您注意到我这件事 .. 我将对代码进行修订。
【解决方案2】:

请试试这个

bool YesToAll = false;
public void AddItems(ListView.SelectedListViewItemCollection selectedItems, ListView.ListViewItemCollection items,TransferType type,string destPath)
{
        foreach(ListViewItem item in selectedItems)
        {
            if (items.ContainsKey(item.Name) && !YesToAll)
            {   
                ListViewItem lvtemp=items.Find(item.Name)[0];
if((lvTemp.SubItems[0].Text!= "[Folder]" && item.SubItem[0].Text!="[Folder]" ) or (lvTemp.SubItems[0].Text== item.SubItems[0].Text && lvTemp.SubItems[0].Text="[Folder]") )
{
                MyMessageBox msgbox = new MyMessageBox("Item is already exists .. Do you want to replace (" + item.Text + ") ?");
                msgbox.ShowDialog();
                if (msgbox.DialogResult == DialogResult.Yes)
                {
                    Add(item, type, destPath);
                }
                else if (msgbox.DialogResult == DialogResult.OK)
                {
                    YesToAll = true;
                    Add(item, type, destPath);
                }
                else if (msgbox.DialogResult == DialogResult.No)
                {
                    continue;
                }
                else
                {
                    return;
                }
}
            }
            else
            {
                Add(item, type, destPath);
            }
        }
        YesToAll = false;
    }
    private void Add(ListViewItem item,TransferType type,string path)
    {
        ListViewItem newItem = (ListViewItem)item.Clone();
        newItem.ImageIndex = imageList1.Images.Add(item.ImageList.Images[item.ImageIndex],Color.Transparent);
        newItem.SubItems.Add(type.ToString());
        newItem.SubItems.Add(path);
        newItem.Tag = type;
        listView1.Items.Add(newItem);
    }

【讨论】:

  • 好吧,它并没有完全起作用..但是这个想法很完美..非常感谢。我已经解决了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
相关资源
最近更新 更多