首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

設計支援圖示與自訂顏色的 ListBox (2):項目類別與集合包裝

     編輯:關於C語言

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; // 建構函式中建立

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); }
}

透過 new 關鍵字隱藏基類成員,提供強型別存取介面。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved