The followingCodeThe listboox function with icons can be directly copied for running. The running result is as follows:
Using system; <br/> using system. drawing; <br/> using system. collections; <br/> using system. componentmodel; <br/> using system. windows. forms; <br/> using system. data; <br/> namespace listboxwithicon <br/>{< br/> /// <br/> <summary> /// Summary of form1. <Br/> // </Summary> <p> public class form1: system. windows. forms. form <br/>{< br/> private glistbox Lb; <br/> /// <br/> <summary> /// required designer variables. <Br/> // </Summary> <p> private system. componentmodel. container components = NULL; <br/> Public form1 () <br/> {<br/> // required for Windows Form Designer <br/> // <br/> initializecomponent (); <br/> // todo: after initializecomponent calls, add Any constructor Code <br/> // <br/>}< br/> /// <br/> <summary> // clear all resources used. <Br/> // </Summary> <p> protected override void dispose (bool disposing) <br/>{< br/> If (disposing) <br/>{< br/> If (components! = NULL) <br/>{< br/> components. dispose (); <br/>}< br/> base. dispose (disposing ); <br/>}< br/> # code generated by region windows Form Designer <br/> /// <br/> <summary> // required by the designer to support method-do not use the code editor to modify <br/> // the content of this method. <Br/> // </Summary> <p> private void initializecomponent () <br/>{< br/> imagelist = new imagelist (); <br/> imagelist. images. add (image. fromfile (@ "D: \ My file \ myrice \ favicon. ICO "); <br/> imagelist. images. add (image. fromfile (@ "D: \ My files \ myrice \ favicon4.ico"); <br/> imagelist. images. add (image. fromfile (@ "D: \ My file \ myrice \ favicon5.ico"); <br/> This. lb = new glistbox (); <br/> This. suspendlayout (); <br/> // <B R/> // LB <br/> // <br/> This. lb. itemheight = 16; <br/> This. lb. location = new system. drawing. point (10, 10); <br/> This. lb. name = "listbox1"; <br/> This. lb. size = new system. drawing. size (200, 60); <br/> This. lb. tabindex = 0; <br/> lb. imagelist = imagelist; <br/> lb. items. add (New glistboxitem ("http://xml.sz.luohuedu.net/", 0); <br/> lb. items. add (New glistboxitem ("http://lucky.myrice.com/", 1); <br/> lb. ITE Ms. add (New glistboxitem ("wonderful world of the Meng xianhui Conference", 2 )); <br/> // form1 <br/> // <br/> This. autoscalebasesize = new system. drawing. size (6, 14); <br/> This. clientsize = new system. drawing. size (292,160); <br/> This. controls. add (this. LB); <br/> This. TEXT = "ListBox test with icons"; <br/> This. load + = new system. eventhandler (this. form1_load); <br/> This. resumelayout (false); <br/>}< br/> # endregion <br/> // <br/> <summary> /// Use the main entry point of the <SPAN class = 'wp _ keywordlink '> Program </span>. <Br/> // </Summary> <p> [stathread] <br/> static void main () <br/>{< br/> application. run (New form1 (); <br/>}< br/> private void form1_load (Object sender, system. eventargs E) <br/>{< br/>}< br/> // glistboxitem class <br/> public class glistboxitem <br/>{< br/> private string _ mytext; <br/> private int _ myimageindex; <br/> // attribute <br/> Public String text <br/>{< br/> get {return _ mytext ;} <br/> set {_ mytext = valu E ;}< br/>}< br/> Public int imageindex <br/>{< br/> get {return _ myimageindex ;} <br/> set {_ myimageindex = value ;}< br/>}< br/> // constructor <br/> Public glistboxitem (string text, int index) <br/>{< br/> _ mytext = text; <br/> _ myimageindex = index; <br/>}< br/> Public glistboxitem (string text ): this (text,-1) {}< br/> Public glistboxitem (): This ("") {}< br/> Public override string tostring () <br/>{< br/> return _ myte XT; <br/>}< br/> // glistbox class <br/> public class glistbox: listBox <br/>{< br/> private imagelist _ myimagelist; <br/> Public imagelist <br/>{< br/> get {return _ myimagelist ;} <br/> set {_ myimagelist = value ;}< br/>}< br/> Public glistbox () <br/>{< br/> This. drawmode = drawmode. ownerdrawfixed; <br/>}< br/> protected override void ondrawitem (system. windows. forms. drawitemeventargs e) <br/> {<Br/> E. drawbackground (); <br/> E. drawfocusrectangle (); <br/> glistboxitem item; <br/> rectangle bounds = E. bounds; <br/> size imagesize = _ myimagelist. imagesize; <br/> try <br/> {<br/> item = (glistboxitem) items [E. index]; <br/> If (item. imageindex! =-1) <br/>{< br/> imagelist. draw (E. graphics, bounds. left, bounds. top, item. imageindex); <br/> E. graphics. drawstring (item. text, E. font, new solidbrush (E. forecolor), <br/> bounds. left + imagesize. width, bounds. top); <br/>}< br/> else <br/>{< br/> E. graphics. drawstring (item. text, E. font, new solidbrush (E. forecolor), <br/> bounds. left, bounds. top); <br/>}< br/> catch <br/>{< br/> If (E. index! =-1) <br/>{< br/> E. graphics. drawstring (items [E. index]. tostring (), E. font, <br/> New solidbrush (E. forecolor), bounds. left, bounds. top); <br/>}< br/> else <br/>{< br/> E. graphics. drawstring (text, E. font, new solidbrush (E. forecolor), <br/> bounds. left, bounds. top); <br/>}< br/> base. ondrawitem (E); <br/>}< br/>