Well, you need to inherit the new control from the ListBox. To do this, create a new project in your project type "Windows Management Library" and use the following code in the file file source file management file:
public partial class ListBoxWithBg : ListBox
{
Image image;
Brush brush, selectedBrush;
public ListBoxWithBg()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DrawItem += new DrawItemEventHandler(ListBoxWithBg_DrawItem);
this.image = Image.FromFile("C:\\some-image.bmp");
this.brush = new SolidBrush(Color.Black);
this.selectedBrush = new SolidBrush(Color.White);
}
void ListBoxWithBg_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index == this.Items.Count - 1)
{
e.Graphics.DrawImage(this.image, new Point(0, 0));
}
else
{
e.Graphics.DrawImage(this.image, e.Bounds, e.Bounds, GraphicsUnit.Pixel);
}
Brush drawBrush =
((e.State & DrawItemState.Selected) == DrawItemState.Selected)
? this.selectedBrush : this.brush;
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, drawBrush, e.Bounds);
}
}
I skipped all the designer code and thus, for brevity, but you will need to remember the Disposeimages and brushes in the Disposecontrol method .
source
share