VB.NET  •  Draw the Control the Editor Shows

Listing 1. GDI+ makes quick work of displaying all the images in an ImageList. You also add text for the image index and draw a basic selection rectangle.

Protected Overrides Sub OnPaint(ByVal e As _
   System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim r As Rectangle
Dim i, Hgt, Wid As Integer
Dim fNormal As New _
   Font(FontFamily.GenericSansSerif, 8)
Dim fBold As New Font(fNormal, FontStyle.Bold)
If Not IsNothing(_ImageList) AndAlso Not _
   _ImageList.Images.Count = 0 Then

   'Draw images and text
   Hgt = _ImageList.ImageSize.Height
   Wid = _ImageList.ImageSize.Width
   For i = 0 To Me._ImageList.Images.Count - 1
      Dim f As Font = fNormal
      If i = _ImageIndex Then f = fBold
      g.DrawString(i.ToString, f, _
         Brushes.Black, 2, (i * Hgt) + 6)
      g.DrawImage(_ImageList.Images(i), _
         TextWid + 1, 1 + (i * Hgt), _
         Wid, Hgt)
   Next
   'Draw selection border
   r = New Rectangle(2, 2 + ( _
      _ImageIndex * Hgt), TextWid + Wid - 2, _
      Hgt - 2)
   g.DrawRectangle(Pens.Blue, r)
End If

r = New Rectangle(1, 1, Me.Size.Width - 2, _
   Me.Size.Height - 2)
g.DrawRectangle(Pens.Black, r)
fNormal.Dispose()
fBold.Dispose()
MyBase.OnPaint(e)
End Sub