而SelectedListBoxExItemCollection類的實現也用同樣的方法,只不過是對SelectedObjectCollection包裝罷了。
集合實現後,再來看ListBoxExItem的實現:
為了使它支持圖標和多種顏色添加如下成員
private int _ImageIndex;
public int ImageIndex
{
get { return this._ImageIndex; }
set { this._ImageIndex = value;}
}
private Color _ForeColor;
public Color ForeColor
{
get{ return this._ForeColor;}
set
{
this._ForeColor = value;
this.Parent.Invalidate();
}
}
當然還有:
private string _Text;
public string Text
{
get { return this._Text; }
set { this._Text = value; }
}
為了控件能正確顯示此項的文本,還必須重寫ToString()方法
public override string ToString()
{
return this._Text;
}
再看ListBoxEx的實現:
為了使控件能夠自我繪制,所以:DrawMode = DrawMode.OwnerDrawFixed;
為了覆蓋基類的Items等相關屬性添加
private ListBoxExItemCollection _Items; //在構造函數中創建
同時還需要重寫屬性Items:
new public ListBoxExItemCollection Items
{
get
{
return this._Items;
}
}
new public ListBoxExItem SelectedItem //強制轉換為ListBoxExItem
{
get{ return base.SelectedItem as ListBoxExItem;}
set{ base.SelectedItem = value;}
}
new public SelectedListBoxExItemCollection SelectedItems //重新包裝SelectedItems
{
get
{
return new SelectedListBoxExItemCollection(base.SelectedItems);
}
}