在一個點對點文件傳輸?shù)捻椖恐?,我需要顯示文件傳輸?shù)膶崟r信息:傳輸?shù)奈募斜砗彤斍皞鬏數(shù)奈募敃r我想到了用ListBox,但是但我用了ListBox后,我發(fā)現(xiàn)它不能改變控件中文本想的顏色,于是我就想擴展一下ListBox控件------ListBoxEx。
我的目標是給空間加上圖標,還要能時時改變控件文本顏色。于是從ListBox派生類
public class ListBoxEx : ListBox {…}
為了操作方便我為ListBoxEx的每一項設(shè)計專門的類ListBoxExItem
public class ListBoxExItem {…}
為了保持我這個控件與WinForm的標準控件的操作借口一致,我又重新設(shè)計了兩個集合類:
public class ListBoxExItemCollection : IList, ICollection, IEnumerator {}
//這個類相對于標準ListBox中的ObjectCollection,這個類作為ListBoxEx中的Items屬性的類型
public class SelectedListBoxExItemCollection : : IList, ICollection, IEnumerator{}
//這個類相對于標準ListBox中的SelectedObjectCollection,這個類作為ListBoxEx中的SelectedItems屬性的類型
下面看兩個集合類的實現(xiàn):
ListBoxExItemCollection的實現(xiàn):為了做到對集合(Items)的操作能夠及時反映到ListBoxEx的控件中所以,此類只是對ListBox中Items(ObjectCollection類型)作了一層包裝,就是把ListBox中Items屬性的所有方法的只要是object類型的參數(shù)都轉(zhuǎn)換成ListBoxExItem,比如:
public void Remove(ListBoxExItem item)
{
this._Items.Remove(item); //_Items為ObjectCollection類型
}
public void Insert(int index, ListBoxExItem item)
{
this._Items.Insert(index, item);
}
public int Add(ListBoxExItem item)
{
return this._Items.Add(item);
}
由上可知,ListBoxExItemCollection中有一個構(gòu)造函數(shù)來傳遞ListBox中的Items對象
private ObjectCollection _Items;
public ListBoxExItemCollection(ObjectCollection baseItems)
{
this._Items = baseItems;
}
而SelectedListBoxExItemCollection類的實現(xiàn)也用同樣的方法,只不過是對SelectedObjectCollection包裝罷了。
集合實現(xiàn)后,再來看ListBoxExItem的實現(xiàn):
我的目標是給空間加上圖標,還要能時時改變控件文本顏色。于是從ListBox派生類
public class ListBoxEx : ListBox {…}
為了操作方便我為ListBoxEx的每一項設(shè)計專門的類ListBoxExItem
public class ListBoxExItem {…}
為了保持我這個控件與WinForm的標準控件的操作借口一致,我又重新設(shè)計了兩個集合類:
public class ListBoxExItemCollection : IList, ICollection, IEnumerator {}
//這個類相對于標準ListBox中的ObjectCollection,這個類作為ListBoxEx中的Items屬性的類型
public class SelectedListBoxExItemCollection : : IList, ICollection, IEnumerator{}
//這個類相對于標準ListBox中的SelectedObjectCollection,這個類作為ListBoxEx中的SelectedItems屬性的類型
下面看兩個集合類的實現(xiàn):
ListBoxExItemCollection的實現(xiàn):為了做到對集合(Items)的操作能夠及時反映到ListBoxEx的控件中所以,此類只是對ListBox中Items(ObjectCollection類型)作了一層包裝,就是把ListBox中Items屬性的所有方法的只要是object類型的參數(shù)都轉(zhuǎn)換成ListBoxExItem,比如:
public void Remove(ListBoxExItem item)
{
this._Items.Remove(item); //_Items為ObjectCollection類型
}
public void Insert(int index, ListBoxExItem item)
{
this._Items.Insert(index, item);
}
public int Add(ListBoxExItem item)
{
return this._Items.Add(item);
}
由上可知,ListBoxExItemCollection中有一個構(gòu)造函數(shù)來傳遞ListBox中的Items對象
private ObjectCollection _Items;
public ListBoxExItemCollection(ObjectCollection baseItems)
{
this._Items = baseItems;
}
而SelectedListBoxExItemCollection類的實現(xiàn)也用同樣的方法,只不過是對SelectedObjectCollection包裝罷了。
集合實現(xiàn)后,再來看ListBoxExItem的實現(xiàn):

