using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Reflection;
下面的代码实现了带图标的ListBoox的功能,可以直接拷贝即可运行。运行结果如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
this.lb = new GListBox();;
this.SuspendLayout();
//
// lb
//
this.lb.ItemHeight = 16;
this.lb.Location = new System.Drawing.Point(10, 10);
this.lb.Name = "listBox1";
this.lb.Size = new System.Drawing.Size(200, 60);
this.lb.TabIndex = 0;
lb.ImageList = imageList;
lb.Items.Add( new GListBoxItem("http://xml.sz.luohuedu.net/",0));
lb.Items.Add( new GListBoxItem("http://lucky.myrice.com/",1));
lb.Items.Add( new GListBoxItem("【孟宪会之精彩世界】",2));
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 160);
this.Controls.Add(this.lb);
this.Text = "带图标的ListBox测试";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
// GListBoxItem 类
public class GListBoxItem
{
private string _myText;
private int _myImageIndex;
// 属性
public string Text
{
get {return _myText;}
set {_myText = value;}
}
public int ImageIndex
{
get {return _myImageIndex;}
set {_myImageIndex = value;}
}
//构造函数
public GListBoxItem(string text, int index)
{
_myText = text;
_myImageIndex = index;
}
public GListBoxItem(string text): this(text,-1){}
public GListBoxItem(): this(""){}
public override string ToString()
{
return _myText;
}
}