ListBox YZX110 with icons and custom colors [Original] Keyword Listbox Source-defined Control
-------- Listbox with icons and custom colors
In a project transferred in a point-to-point file, I need to display real-time information for file transfer: the list of files transmitted and the currently transferred file, I thought of using ListBox, but I used ListBox, I found it can't change the Chinese I want to have the color, so I want to expand the ListBox control ------ ListBoxEx.
My goal is to add icons to space, but also change the color of the control text. So from Listbox derived class
Public Class ListBoxEx: listbox {...}
For the convenience of me, I have a special class ListBoxExItem for each of ListBoxex.
Public class listboxIXItem {...}
In order to keep my control with the operating excuse of WinForm's standard control, I also redesigned two set classes.
Public Class ListBoxExItemCollection: ilist, iCollection, IEnumerator {} // This class is the type of Items property in ListBoxEx relative to ObjectCollection in standard ListBox.
Public Class SelectedListBoxExItemCollection:: ilist, iCollection, IENUMERATOR {} // This class relative to SelectedObjectCollection in standard ListBox, this class is the type of SelectedItems property in ListBoxex.
Look at the implementation of two collection classes:
Implementation of ListBoxExItemCollection: To do a set of operations to the collection (Items), this class is only a layer of packaging in ListBox's items (ObjectCollection type), which is all methods of Items properties in listbox. As long as the object of the object type is converted into ListBoxExItem, such as:
Public Void Remove (ListBoxExItem Item)
{
THIS._ITEMS.Remove (item); // i _ items for ObjectCollection type
}
Public void insert (int index, listboxexItem item)
{
THIS._ITEMS.INSERT (ITEX, ITEM);
}
Public int Add (ListBoxExItem Item)
{
Return this._Items.add (item);
}
It is known that there is a constructor in the ListBoxExItemCollection to deliver items objects in Listbox.
Private ObjectCollection_Items;
Public ListBoxExItemCollection (ObjectCollection BaseItems)
{
THIS._ITEMS = BASEITEMS;
}
The implementation of the SelectedListBoxExItemCollection class also uses the same method, but it is just a selectedObjectCollection package.
After the collection is implemented, look at the implementation of ListBoxExItem:
In order to support it support icons and multiple colors to add as follows
Private int _ImageIndex;
Public Int ImageIndex
{
Get {return this._imageindex;
Set {this._imageindex = value;
}
Private Color_forcolor; 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;
}
For the control, the text can be displayed correctly, but also to rewrite the toString () method
Public override string toString ()
{
Return THIS._TEXT;
}
Look at the implementation of ListBoxex:
In order to make the control self-drawn, DrawMode = DrawMode.OWNERDRAWFIXED;
To cover the relevant attributes of the base class Items
Private ListBoxExItemCollection_Items; // Created in the constructor
Also need to override the property items
New Public ListBoxExItemcollection Items
{
get
{
Return this._items;
}
}
New public listboxexItem selecteditem // Forced to convert to ListBoxExItem
{
Get {return base.selecteditem as listboxIXITEM;
Set {base.selecteditedItem = value;
}
New public selectedListBoxExItemCollection SelectedItems // Repacking SelectEMS
{
get
{
Return New SelectedListBoxExItemCollection (BASE.SELECTEDITEMS);
}
}
In order to support icons, add an image list imagelist
Private imagelist imagelist;
Public imagelist imagelist
{
Get {return this.imagelist;
set
{
THIS.IMAGELIST = VALUE;
this.invalidate (); // The image list is immediately updated and the control is immediately
}
}
The core of this control is in a method ondrawitem, which is called whenever the item is renovated.
Protected Override Void OnDrawItem (System.Windows.Forms.DrawiteMeventargs PE)
{
Pe.drawBackground (); //
PE.DRAWFOCUSRECTANGLE (); // Section Box
Rectangle bounds = pe.bounds;
// Check WHether the index is valid
IF (PE.index> = 0 && pe.index { ListBoxExItem item = this.Items [pe.index]; // get a reference to the need to draw items IOFFSET = 0; // if the image list is present and the image index is set, draw the Image IF (this.imagelist! = NULL) { IF (item.imageIndex> -1 && item.imageindex <.imagelist.images.count) { this.imagelist.draw (pe.graphics, bounds.heft, bounds.top, bounds.height, bounds.height, item.imageIndex); // Draw icon } Ioffset = Bounds.height; // this.imagelist.imageSize.width; } // Draw Item Text PE.Graphics.drawstring (item.text, pe.font, new solidbrush (item.forecolor), Bounds.Left Ioffset, Bounds.top; // Draw text according to the color of the item } Base.ondrawItem (PE); } } To this end, ListBoxEx is implemented in a complete implementation and supports visual design. If you need full code, please contact YZX110@bit.edu.cn