C # Design a listbox with icons and custom colors

Source: Internet
Author: User
Tags count insert relative return tostring
Design

In a point-to-point file transfer project, I need to display real-time information on file transfers: A list of transferred files and currently transmitted files, I thought of using a ListBox, but after I used the listbox, I found that it couldn't change the color of the controls in Chinese. So I just want to expand the ListBox control------LISTBOXEX.

My goal is to add an icon to the space and to change the color of the control text from time to time. So derive the class from the listbox

public class Listboxex:listbox {...}

In order to facilitate the operation of my listboxex for each of the special design of the class Listboxexitem

public class Listboxexitem {...}

To keep my control in line with the WinForm standard controls, I redesigned two collection classes:

public class Listboxexitemcollection:ilist, ICollection, IEnumerator {}
This class is relative to the objectcollection in the standard ListBox, which is the type of the Items property in Listboxex

public class Selectedlistboxexitemcollection:: IList, ICollection, ienumerator{}
This class is relative to the selectedobjectcollection in the standard ListBox, which is the type of the SelectedItems property in the Listboxex

Here's a look at the implementation of the two collection classes:

Listboxexitemcollection implementation: In order to do the operation of the collection (Items) can be reflected in a timely manner to the Listboxex control so, this class is only on the listbox Items (objectcollection type) A wrapper is to convert all methods of the Items property in the ListBox to Listboxexitem, as long as the parameters of object type are:

public void Remove (Listboxexitem item)
{
This._items.remove (item); _items as ObjectCollection type
}

public void Insert (int index, LISTBOXEXITEM item)
{
This._items.insert (index, item);
}

public int Add (Listboxexitem item)
{
return This._items.add (item);
}

From the above, there is a constructor in Listboxexitemcollection to pass the items object in the ListBox

Private ObjectCollection _items;

Public listboxexitemcollection (ObjectCollection baseitems)
{
This._items = Baseitems;
}

And the implementation of the Selectedlistboxexitemcollection class also use the same method, but only to selectedobjectcollection packaging.

After the collection is implemented, 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 there are:

private string _text;

public string Text
{
get {return this._text;}
set {This._text = value;}
}

For the control to display the text of this item correctly, you must also override the ToString () method

public override string ToString ()
{
return this._text;
}

Then look at the implementation of Listboxex:

To enable the control to draw itself, so: DrawMode = drawmode.ownerdrawfixed;

To override related properties such as the base class's items, add

 

Private Listboxexitemcollection _items; creating in constructors

You also need to override properties items:

New public Listboxexitemcollection Items
{
Get
{
return this._items;
}
}

New public Listboxexitem SelectedItem//cast to Listboxexitem
{
get{return base. SelectedItem as Listboxexitem;}
set{base. SelectedItem = value;}
}

New public selectedlistboxexitemcollection SelectedItems//repackaging SelectedItems
{
Get
{
Return to new Selectedlistboxexitemcollection (base. SelectedItems);
}
}

To support the icon, add an image list ImageList

Private ImageList ImageList;

Public ImageList ImageList
{
get {return this.imagelist;}
Set
{
This.imagelist = value;
This. Invalidate ()//update the control immediately after the image list changes
}
}

The core of this control is in a method OnDrawItem, which is invoked whenever the control's items need to be redrawn

 

protected override void OnDrawItem (System.Windows.Forms.DrawItemEventArgs pe)
{
Pe. Drawbackground (); Draw background
Pe. Drawfocusrectangle (); Draw a border
Rectangle bounds = pe. Bounds;

Check whether the index is valid

if (PE. Index >= 0 && pe. Index < base. Items.Count)
{
Listboxexitem item = this. Items[pe. Index]; Get a reference to the item you want to draw
int 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 < This.imageList.Images.Count)
{
This.imageList.Draw (PE. Graphics, bounds. Left, bounds. Top, bounds. Height, bounds. Height, item. ImageIndex); Drawing icons
}
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 based on the color of an item

}
Base. OnDrawItem (PE);
}
}

So far, Listboxex is fully implemented and supports visual design.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.