如何检查,如果该项目[名称和放大器;子项的文本]已在其它的ListView的存在? [英] how to check if the item [name & subItem's text] is already exists in another listView?

查看:226
本文介绍了如何检查,如果该项目[名称和放大器;子项的文本]已在其它的ListView的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做文件传输(服务器 - 客户端)应用程序..结果我有两个列表视图探索本地PC和远程PC ..之前发送/接收的项目..结果我需要检查,如果有另一个文件或文件夹具有目标路径相同的名字..
当我美元的按钮p $ PSS [发送或接收]的项目添加到列表..然后当我在按钮preSS [开始传输] ..它开始。结果

i making a file transfer (server-client) application ..
i have two listviewS to explore Local PC and Remote PC .. before send/receive the items..
i need to check if there's another file or folder has the same name at the destination path.. when i press on the button [send or receive] the item added to a list.. then when i press on button [Start Transfer] .. it starts.

所以为addItems方法调用时我preSS按钮,接收或发送..我得到SelectedItems 的从源头的ListView ..和的项目目的地的ListView ...然后我检查每个项目 SelectedItems 如果存在,它在项目

so the AddItems Method called when i press the button Receive or Send .. i get the SelectedItems from the source ListView .. and the Items of the destination ListView ... then i check for each item in SelectedItems if it is exists in Items

我试图用

items.Contain(item)

但它没有工作,它总是让即使该项目已经存在,我的不实。结果
所以我用items.ContainKey和它的工作..但在情况下我有一个文件名为temp的无扩展名和目标路径的文件夹也名为temp的..它会返回真..这是我的问题。

but it didn't work it always gave me false even if the item is already exists.
so i used items.ContainKey and it worked .. but in case that i have a file named "Temp" with no extension and a folder in destination path also named "Temp" .. it will returns True .. and that's my problem ..

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

当用户在确认DialogBox的[全部都是]按钮点击YesToAll设置为true。结果
以TransferType只是标记,如果它要使用该项目SendMethod或ReceiveMethod

YesToAll is set to true when the user clicked on [Yes to all] button in the confirm dialogbox.
TransferType is just to mark the item if it's going to use SendMethod or ReceiveMethod

public enum TransferType
    {
        Send , Receive
    };

让我怎么解决这个问题..我应该使用自定义的方法,而不是[包含]来检查的名称和类型(文件或文件夹),因为每个项目都已经有一个子项,告诉它是否是文件夹或文件

so how do i fix that .. should i use a custom method instead of [Contains] that checks for the name and for the type (file or folder) because each item is already has a subItem which tell if it is a folder or a file

先谢谢了。

推荐答案

请试试这个

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

这篇关于如何检查,如果该项目[名称和放大器;子项的文本]已在其它的ListView的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆