항목이 추가 될 때 ListView의 중복 항목을 피하기 위해 해시 테이블을 사용하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/2407143

문제

항목이 추가 될 때 ListView의 중복성을 피하는 방법은 무엇입니까 .. Im Winforms C#.NET을 사용하고 있습니다. ListView1의 항목과 ListView2의 항목간에 비교할 수있는 방법을 의미합니다. 하나의 목록보기는 다른 목록보기에 이미 입력 한 항목을 입력 할 수 없습니다. 한 목록에서 다른 목록에서 다른 항목을 추가 할 수 있지만 Auplication 항목을 추가하고 있습니다. ?

도움이 되었습니까?

해결책

당신은 다음과 같은 것을 생각할 수 있습니다.

Hashtable openWith = new Hashtable();
// Add some elements to the hash table. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is 
// already in the hash table.
try
{
    openWith.Add("txt", "winword.exe");
}
catch
{
    Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// ContainsKey can be used to test keys before inserting 
// them.
if (!openWith.ContainsKey("ht"))
{
    openWith.Add("ht", "hypertrm.exe");
    Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}

편집 후 문제의 변경 사항을 충족 시키려면 다음과 같이 할 수 있습니다.

if(!ListView2.Items.Contains(myListItem))
{
    ListView2.Items.Add(myListItem);
}

비슷한 문제를 참조 할 수도 있습니다 선택한 항목을 한 목록보기에서 다른 목록에서 다른 것으로 복사하는 방법 C#Net의 클릭?

다른 팁

제안한 바와 같이, 해시 테이블은 그러한 중복성을 막는 좋은 방법입니다.

Dictonary ... 모든 배열 .. 가능한 모든 목록, 단지 루프는 항목/서브 시템을 던지고 "배열"에 추가 한 다음 루프를 배열을 던져 Otehr 목록에 대해 확인하십시오 ...

다음은 버튼 클릭에서 DUP를 제거하는 데 사용하는 예입니다. 그러나 귀하의 요구에 맞게 코드를 쉽게 변경할 수 있습니다.

아래를 사용하여 버튼 클릭의 ListView에서 "dups"를 제거했습니다. 자체 사용을 위해 코드를 편집 할 수있는 subitem을 검색하고 있습니다 ...

사전과 약간 쉬운 "업데이트"클래스를 사용합니다.

private void removeDupBtn_Click(object sender, EventArgs e)
    {   

        Dictionary<string, string> dict = new Dictionary<string, string>();

        int num = 0;
        while (num <= listView1.Items.Count)
        {
            if (num == listView1.Items.Count)
            {
                break;
            }

            if (dict.ContainsKey(listView1.Items[num].SubItems[1].Text).Equals(false))
            {
                dict.Add(listView1.Items[num].SubItems[1].Text, ListView1.Items[num].SubItems[0].Text);
            }     

            num++;
        }

        updateList(dict, listView1);

    }

그리고 약간의 updateList () 클래스를 사용합니다 ...

   private void updateList(Dictionary<string, string> dict, ListView list)
    {
        #region Sort
        list.Items.Clear();

        string[] arrays = dict.Keys.ToArray();
        int num = 0;
        while (num <= dict.Count)
        {
            if (num == dict.Count)
            {
                break;
            }

            ListViewItem lvi;
            ListViewItem.ListViewSubItem lvsi;

            lvi = new ListViewItem();
            lvi.Text = dict[arrays[num]].ToString();
            lvi.ImageIndex = 0;
            lvi.Tag = dict[arrays[num]].ToString();

            lvsi = new ListViewItem.ListViewSubItem();
            lvsi.Text = arrays[num];
            lvi.SubItems.Add(lvsi);

            list.Items.Add(lvi);

            list.EndUpdate();

            num++;
        }
        #endregion
    }

행운을 빕니다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top