VB.NET • Bind a Picture Box to a Data Source

Listing 2. The BoundPictureBox control inherits from PictureBox and exposes one additional member, the ImageFile property. When this property is assigned, a new image is loaded in the picture box. You can therefore bind this property to a database field or another data source to display images automatically.

Imports System.windows.forms
Imports System.ComponentModel

Public Class BoundPictureBox
   Inherits PictureBox

   Private m_ImageFile As String

   <Browsable(False), Bindable(True)> _
      Public Property ImageFile() As String
      Get
         Return m_ImageFile
      End Get
      Set(ByVal Value As String)
         If m_ImageFile <> Value Then
            m_ImageFile = Value
            Me.Image = _
               Image.FromFile(m_ImageFile)
         End If
      End Set
   End Property
End Class