列表框到列表框没有重复 [英] Listbox to listbox without duplication

查看:92
本文介绍了列表框到列表框没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我对列表的看法:



  private   void  Form1_Load( object  sender,EventArgs e)
{
List< ;串GT; _scoreboard = new List< string>();
ProSailor sailor1 = new ProSailor( 1 24 300 5 );
ProSailor sailor2 = new ProSailor( 2 23 450 6 );
ProSailor sailor3 = new ProSailor( 3 20 2000 9 );
CharitySail sailor4 = new CharitySail( 4 210 WWF);
CharitySail sailor5 = new CharitySail( 5 2048 过敏反应运动);
CharitySail sailor6 = new CharitySail( 6 325 Childs Play);
NoveltySail sailor7 = new NoveltySail( 7 450 游戏援助 蝙蝠侠 2 );
NoveltySail sailor8 = new NoveltySail( 8 1024 Extra-Life 塞尔达传说 3 );
NoveltySail sailor9 = new NoveltySail( 9 512 迪士尼 5 );

_scoreboard.Add(sailor1.ToString());
_scoreboard.Add(sailor2.ToString());
_scoreboard.Add(sailor3.ToString());
_scoreboard.Add(sailor4.ToString());
_scoreboard.Add(sailor5.ToString());
_scoreboard.Add(sailor6.ToString());
_scoreboard.Add(sailor7.ToString());
_scoreboard.Add(sailor8.ToString());
_scoreboard.Add(sailor9.ToString());

listBox2.DataSource = _scoreboard;
}





现在,每当我点击完成按钮,我想要从这个列表(listbox2)中的东西到一次转移到listbox1。目前,如果我选择一个listbox2项并多次单击完成,它会将同一项复制到listbox1中,与您单击的次数相同!我怎么能阻止这个?



我尝试从原始列表中删除但是.DataSource停止并使用

 如果(listbox1.Items.Contains()){} 

也不起作用。请帮忙吗?



干杯!

解决方案

首先,这是一个坏主意存储只是列表框中的字符串。存储您的水手类型!列表框的项目可以是任何类型的项目。但是,如果存储实际数据而不是表示数据的字符串,则可以稍后将项目类型转换为实际数据项并提取所需的任何信息。当UI显示由 System.Object.ToString 返回的字符串时,您可以按照希望在UI中显示的方式覆盖此函数。



现在,如果要删除重复项,可以将数据存储在不接受重复键的中间容器中。如果您的密钥与数据相同,则可以是类 System.Collections.Generic.HashSet< T>

http://msdn.microsoft.com/en-us/library/bb359438.aspx [ ^ ]。



再次,过滤掉你的水手类型的实例,而不是字符串。我希望你所有的水手类型都有共同的基类或实现一个通用的接口。使用此常用类型作为哈希集中的实际泛型类型参数。



-SA


清除第二个ListBox项目,然后每次添加完整列表。

  //  在你的onClick处理程序中......  
listbox1.Items.Clear();
foreach var item in listbox2.Items)
{
listbox1.Items.Add(item);
}


建立在Sergey关于存储对象的建议的基础上,这里有一个例子我认为简化了两个ListBox的情况,并处理了你的情况想要将listBox2的全部内容复制到listBox1,或者只将listBox2中的SelectedItems复制到listBox2,并避免将重复项复制到listBox1。



我们将从一个开始用于演示目的的类的非常简单的存根:

  public   class 水手
{
public string sailorName {获得; private set ; }

public Sailor( string sName){sailorName = sName;}

public 覆盖 string ToString(){ return sailorName;}
}

同时,回到牧场,在某种形式或类别中,让我们定义我们需要的变量:

 private List <   Sailor  >  lSailor; 

private int nSailors = 10;

private bool copySelectedItemsOnly = true;

private bool clearListBox1ItemsAlways = true;

点击button1将工作人员添加到listBox2:

  private   void  button1_Click( object  sender,EventArgs e)
{
// 如有必要,创建水手列表
if (lSailor == null
{
lSailor = new List< Sailor>();

for int i = 0 ; i < nSailors; i ++)
{
Sailor newSailor = 水手( Sailor + i);
lSailor.Add(newSailor);
}

listBox2.DataSource = lSailor;
}
其他
{
// < span class =code-comment>在这种情况下不确定你真正想做什么
// 清除整个列表或选定的项目?

// 如果listBox2设置了DataSource:您可以使用此
// 来清除整个ListBox
// listBox2.DataSource = null;
// listBox2.Items.Clear();
// lSailor = null;

// 这个w仅清除SelectedItems
// listBox2.ClearSelected();
}
}

点击button2进行从listBox2到listBox1的复制:

  private   void  button2_Click( object  sender,EventArgs e)
{
if (copySelectedItemsOnly)
{
if (clearListBox1ItemsAlways)
{
// 允许重复
foreach object lbi in listBox2.SelectedItems)
{
listBox1.Items。加入(LBI);
}
}
else
{
// 无重复
foreach object lbi in listBox2.SelectedItems)
{
if ( !listBox1.Items.Contains(lbi))listBox1.Items.Add(lbi);
}
}
}
其他
{
// 将所有listBox2项复制到listBox1

// 可选清除listBox1?
// listBox1.DataSource = null;

listBox1.DataSource = listBox2.DataSource;

// 可选清除listBox2?
// listBox2.DataSource = null;
}
}


Here is what I have in terms of a list:

private void Form1_Load(object sender, EventArgs e)
        {
            List<string> _scoreboard = new List<string>();
            ProSailor sailor1 = new ProSailor(1, 24, 300, 5);
            ProSailor sailor2 = new ProSailor(2, 23, 450, 6);
            ProSailor sailor3 = new ProSailor(3, 20, 2000, 9);
            CharitySail sailor4 = new CharitySail(4, 210, "WWF");
            CharitySail sailor5 = new CharitySail(5, 2048, "Anaphylaxis Campaign");
            CharitySail sailor6 = new CharitySail(6, 325, "Childs Play");
            NoveltySail sailor7 = new NoveltySail(7, 450, "Games Aid", "Batman", 2);
            NoveltySail sailor8 = new NoveltySail(8, 1024, "Extra-Life", "Legend of Zelda", 3);
            NoveltySail sailor9 = new NoveltySail(9, 512, "Water", "Disney", 5);

            _scoreboard.Add(sailor1.ToString());
            _scoreboard.Add(sailor2.ToString());
            _scoreboard.Add(sailor3.ToString());
            _scoreboard.Add(sailor4.ToString());
            _scoreboard.Add(sailor5.ToString());
            _scoreboard.Add(sailor6.ToString());
            _scoreboard.Add(sailor7.ToString());
            _scoreboard.Add(sailor8.ToString());
            _scoreboard.Add(sailor9.ToString());

            listBox2.DataSource = _scoreboard;
        }



Now, whenever I hit the "Finished" button, I want the stuff from this list (listbox2) to get transferred to listbox1 ONE TIME ONLY. Currently if I select a listbox2 item and click finished multiple times it will duplicate the same item into listbox1 as many times as you click! How can I stop this?

I've tried deleting from the original list but the .DataSource stops that and using

if (listbox1.Items.Contains()) {}

doesn't work either. Little help please?

Cheers!

解决方案

First of all, this is a bad idea to store just strings in the list box. Store your sailor types! The item of the list box can be anything, an item of any type. But if you store real data, not the string representing data, you can later type-cast the item to your real data item and extract any information you need. As UI shows the strings returned by System.Object.ToString, you can override this function the way you want to appear in the UI.

Now, if you want to remove duplicate, you can store data in intermediate container which does not accept duplicate keys. If your key is the same as data, it can be the class System.Collections.Generic.HashSet<T>:
http://msdn.microsoft.com/en-us/library/bb359438.aspx[^].

Again, filter out just instanced of your sailor types, not strings. I hope all your sailor types have common base class or implement a common interface. Use this common type as the actual generic type parameter in the hash set.

—SA


Clear the 2nd ListBox items and then add your complete list each time.

// In your onClick handler...
listbox1.Items.Clear();
foreach( var item in listbox2.Items )
{
   listbox1.Items.Add( item );
}


Building on Sergey's suggestion to store objects, here's an example I think simplifies what happens with the two ListBoxes, and deals with the case you want to copy the entire contents of listBox2 to listBox1, or just the SelectedItems in listBox2 to listBox2, and avoids duplicates being added to listBox1.

We'll start with a really trivial stub of a class for demo purposes:

public class Sailor
{
    public string sailorName { get; private set; }

    public Sailor(string sName){sailorName = sName;}

    public override string ToString(){return sailorName;}
}

Meanwhile, back at the ranch, in some Form or Class, let's define variables we'll need:

private List<Sailor> lSailor;

private int nSailors = 10;

private bool copySelectedItemsOnly = true;

private bool clearListBox1ItemsAlways = true;

A click on button1 "adds the crew" to listBox2:

private void button1_Click(object sender, EventArgs e)
{
    // create list of Sailor, if necessary
    if (lSailor == null)
    {
        lSailor = new List<Sailor>();

        for (int i = 0; i < nSailors; i++)
        {
            Sailor newSailor = new Sailor("Sailor " + i);
            lSailor.Add(newSailor);
        }

        listBox2.DataSource = lSailor;
    }
    else
    {
        // not sure what you really want to do in this case
        // clear either the entire list, or the Selected Items ?

        // if listBox2 has a DataSource set: you can use this
        // to clear the entire ListBox
        //listBox2.DataSource = null;
        //listBox2.Items.Clear();
        //lSailor = null;

        // this will clear only SelectedItems
        // listBox2.ClearSelected();
    }
}

A click on button2 does the copying from listBox2 to listBox1:

private void button2_Click(object sender, EventArgs e)
{
    if (copySelectedItemsOnly)
    {
        if(clearListBox1ItemsAlways)
        {
            // allow duplicates
            foreach (object lbi in listBox2.SelectedItems)
            {
               listBox1.Items.Add(lbi);
            }
        }
        else
        {
            // no duplicates
            foreach (object lbi in listBox2.SelectedItems)
            {
                if (! listBox1.Items.Contains(lbi)) listBox1.Items.Add(lbi);
            }
        }
    }
    else
    {
        // copy all listBox2 items to listBox1

        // optionally clear listBox1 ?
        // listBox1.DataSource = null;

        listBox1.DataSource = listBox2.DataSource;

        // optionally clear listBox2 ?
        // listBox2.DataSource = null;
    }
}


这篇关于列表框到列表框没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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