c
带图标和自定义颜色的listbox(C designs ListBox with icons and custom colors)
c#设计带图标和自定义颜色的listbox(C# designs ListBox with
icons and custom colors)
C# designs ListBox with icons and custom colors
The C# design, with icons and custom colors, is the key word for ListBox, C#, ListBox, and color,
In a peer-to-peer file transfer project, I need to display the real-time information of file transfer: transfer file list and the current file transfer, then I think of ListBox, but I used ListBox, I found it could not change the text color to control, so I want to expand out. ListBox control ------ListBoxEx.
My goal is to add icons to space and to change the text color of the control at any time. The class is derived from the ListBox
Public class ListBoxEx: ListBox {... }
For ease of operation, I designed a special class ListBoxEx for each item of ListBoxExItem
Public, class, ListBoxExItem {... }
To keep my control in line with the standard manipulation of WinForm's standard controls, I redesigned two collection classes:
Public class ListBoxExItemCollection: IList, ICollection, IEnumerator {}
/ / this class compared to the standard ListBox
ObjectCollection in this class as a Items attribute in ListBoxEx type
Public, class, SelectedListBoxExItemCollection:: IList, ICollection, IEnumerator{}
/ / this class compared to the standard ListBox
SelectedObjectCollection in this class as a SelectedItems attribute in ListBoxEx type
See below the implementation of the two collection class:
The implementation of ListBoxExItemCollection: in order to achieve the set (Items) operation can be reflected to the ListBoxEx control so that such is only for ListBox Items (type ObjectCollection) made a layer of packaging, is that all the Items property in the ListBox as long as the parameters of type object are converted to ListBoxExItem, for example:
Public void Remove (ListBoxExItem item)
{
This._Items.Remove (item); //_Items is the ObjectCollection type
}
Public, void, Insert (int, index, ListBoxExItem, item)
{
This._Items.Insert (index, item);
}
Public int Add (ListBoxExItem item)
{
Return this._Items.Add (item);
}
As you can see, there is a constructor in ListBoxExItemCollection to pass the Items object in ListBox
Private ObjectCollection _Items;
Public ListBoxExItemCollection (ObjectCollection baseItems)
{
This._Items = baseItems;
}
The implementation of the SelectedListBoxExItemCollection class is the same way, just wrapping the SelectedObjectCollection.
After the collection is implemented, let's look at the
implementation of ListBoxExItem:
To enable it to support icons and multiple colors, add the
following members
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 ();
}
}
Of course:
Private string _Text;
Public string Text
{
Get {return this._Text}
Set {this._Text = value}
}
In order for the control to display the text of the item
correctly, you must override the ToString () method
公共tostring()重写字符串
{
这_text返回;
}
再看listboxex的实现:
为了使控件能够自我绘制,所以:DrawMode = drawmode.ownerdrawfixed;
为了覆盖基类的项目等相关属性添加
私人listboxexitemcollection _items;/ /在构造函数中创建
同时还需要重写属性项目:
新公共listboxexitemcollection项目
{
得到
{
这_items返回;
}
}
新市民ListBoxExItem SelectedItem /强制转换为listboxexitem
{
得到的回报是listboxexitem base.selecteditem {;}
集合{ base.selecteditem =值;}
}
新市民SelectedListBoxExItemCollection SelectedItems /重新包
装selecteditems
{
得到
{
返回新的selectedlistboxexitemcollection(基地。selecteditems);
}
}
为了支持图标,添加一个图像列
ImageList
私人的ImageList的ImageList;
市民ImageList ImageList
{
得到的回报this.imagelist {;}
设置
{
this.imagelist =价值;
这invalidate();/ /图像列表改变后马上更新控件。
}
}
而此控件的核心却在一个方法OnDrawItem,这个方法每当控件的项需要重绘时就被调用
OnDrawItem(system.windows.forms.drawitemeventargs PE保护覆盖无效)
{
drawbackground();/ /画背景PE。
drawfocusrectangle();/ /画边框PE。
矩形边界;
/检查索引是否有效。
如果(pe.index > = 0和PE。指数,基地。项目。计数)
{
listboxexitem项目=本吧。
这篇C #设计带图标和自定义颜色的ListBox的关键词是C #,列表框,颜色,EMS [体育] / /取得需要绘制项的引用。指数;
int IOFFSET = 0;
/如果图像列表存在并设置了图像索引,则绘制图像。
如果(this.imagelist~= null)
{
如果(item.imageindex > - 1和item.imageindex <这个ImageList。图像。计数)
{
这个ImageList。画(pe.graphics,界限。左边界。顶部边界。高度、界限。高度、项目。ImageIndex);/ /绘制图标
}
IOFFSET + =界限。高度;/ / this.imagelist.imagesize.width;
}
//绘制项目文本
体育。图形。束带(item.text,PE。字体,新的SolidBrush(项目。
前景色),bounds.left + IOFFSET界限。顶部);/ /根据项的颜色绘制文本
}
OnDrawItem(PE)的基础;
}
}
到此为止,listboxex以完整的实现,并且支持可视化设计。