c# 關於LISTBOX的添加項的問題 以及不重複插入

來源:互聯網
上載者:User

我以前沒有用過windows form上的東西,web上很簡單,html的標籤支援元素的value屬性。我使用過delphi的combox和listbox,它的items是個stringList類型的東西,每個元素是一個字串,同時能儲存一個object;使用的時候往往定義個object的子類型,然後添加到items時new一個這種對象,最後還要管理這個對象的釋放.
      那麼開啟msdn看看windows form上的listbox吧
      經過研究猜想是這樣:
            ListBox.Items 的類型是ListBox.ObjectCollection類型

            他的add函數參數是一個object,而不是string  

           那麼按理說,一個元素類型是object,那麼系統顯示這個元素的時候自然需要ToString()
      所以也許可以這樣做:
            在視窗內寫一個小類,繼承自object

            包含兩個string的資料成員  

            一個是名字,一個是id

            然後重載基類object的函數Tostring()  

            讓這個小類的tostring 函數return name;
            定義好這個類以後,

            當添加記錄到列表框的時候,

            new 一個小類的對象

            把名字和id賦值到兩個成員  

            然後listbox1.items.add(yourObj);  

            這樣大概就行了
            然後在使用的時候

            obj=Listbox1.selected......

            obj.id .....

代碼:

 /// 
/// 列表框元素對象
///
///
public class MyItem : object
{
public string name;
public string id;
public override string ToString()
{
// TODO: 添加 MyItem.ToString 實現
return name;
}
}
//----------------------------------------------------

private void Form1_Load(object sender, System.EventArgs e)
{
MyItem item1=new MyItem();
item1.id="001";
item1.name="天下";
this.listBox1.Items.Add(item1);

MyItem item2=new MyItem();
item2.id="002";
item2.name="天上";
this.listBox1.Items.Add(item2);


MyItem item3=new MyItem();
item3.id="003";
item3.name="地下";
this.listBox1.Items.Add(item3);

MyItem item4=new MyItem();
item4.id="004";
item4.name="地上";
this.listBox1.Items.Add(item4);
}
//------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
MyItem tmp =(MyItem)this.listBox1.Items[this.listBox1.SelectedIndex];

MessageBox.Show(tmp.id);
}

 

經過測試,果然如此,哈哈
========================= 感想 ============= 從listbox這樣的小地方可以看出 .net的類庫設計的確很優雅 很能體現思想
比如這個列表框的元素,這個元素不是一個string, 因為設計者料到 , 使用者拿列表框不是僅僅作為string來用, 所以他把item抽象成object 但是還要正常顯示元素的文本, 所以他在顯示文本的時候就調用object的ToString 這樣 就優雅的多了
使用者(程式員)可以使用任何的資料類型作為元素類型,只要此元素支援Tostring就行了 而.net的所有類型都繼承自object 所以Tostring就是 Items和item的一個標準"介面"
-----------------------------------------------------------------
再看delphi的ListBox,他的items是一個stringList , stringList的每個元素必須首先"是"一個字串 然後相應的字串可以對應一個額外的Object
相比.net的設計, item不必"是"一個字串 (它可以是任何類型) 只要這個類型能向Items提供一個ToString的介面就行了
後者的設計更符合物件導向的思想

******************************************

不重複插入

View Code

for(int   i=0;i<listBox1.Items.Count;i++)   
{
if(! listBox1.SelectedItems.Contains(listBox1.Items[i]))
{
MessageBox.Show(listBox1.Items[i].ToString());
}
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.