Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a PictureBox control that has a bindable FileName property in VB .NET
DescriptionThis example shows how to make a PictureBox control that has a bindable FileName property in VB .NET
Keywordsbind, data, bindable, PictureBox, NamedPicture
CategoriesVB.NET, Controls, Database
 
Unfortunately the PictureBox control doesn't have a bindable property so it's not trivial to connect one to a data navigation control. However, it's relatively easy to make a picture control that has a bindable property.

The following control inherits from PictureBox and adds a FileName property. When the property is set, the control tries to use the indicated file for its image. Note that the property has the Browsable and Bindable attributes set.

 
Imports System.ComponentModel

Public Class NamedPicture
    Inherits PictureBox

    Private m_FileName As String
    <Browsable(True), Bindable(True)> _
    Public Property FileName() As String
        Get
            Return m_FileName
        End Get
        Set(ByVal Value As String)
            If m_FileName <> Value Then
                m_FileName = Value
                Try
                    Image = Image.FromFile(Value)
                    Me.Refresh()
                Catch
                    Image = Nothing
                End Try
            End If
        End Set
    End Property
End Class
 
The example program available for download builds a DataTable containing picture names and file names. It sets a DataGrid control's DataSource property to the DataTable so the grid displays its values. It then makes a NamedPicture control, and binds its FileName property to the DataTable's PicturePath column. When you click on records in the DataGrid, the NamedPicture control automatically loads the selected file.
 
Public Class Form1
...
    Private m_DataSet As DataSet

    Private m_NamedPicture As NamedPicture

    Private m_PictureNames As Collection

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        ' Make a DataTable.
        Dim picture_table As New DataTable("PictureTable")
        picture_table.Columns.Add(New DataColumn( _
            "PictureName", GetType(String)))
        picture_table.Columns.Add(New DataColumn( _
            "PicturePath", GetType(String)))

        ' Put some data in the DataTable.
        Dim picture_path As String = Application.StartupPath
        picture_path = picture_path.Substring(0, _
            picture_path.LastIndexOf("\"))
        picture_table.Rows.Add(New Object() {"Snortimer", _
            picture_path & "\snortimer.bmp"})
        picture_table.Rows.Add(New Object() {"Bone", _
            picture_path & "\bone.bmp"})
        picture_table.Rows.Add(New Object() {"Dog", _
            picture_path & "\dog.jpg"})
        picture_table.Rows.Add(New Object() {"VB Helper", _
            picture_path & "\vbhelper.bmp"})

        ' Bind the DataTable to the DataGrid.
        DataGrid1.DataSource = picture_table

        ' Make the NamedPicture control.
        m_NamedPicture = New NamedPicture
        With m_NamedPicture
            .BackColor = Color.PapayaWhip
            .Left = 0
            .Top = DataGrid1.Top + DataGrid1.Height
            .Width = Me.ClientRectangle.Width
            .Height = Me.ClientRectangle.Height - .Top
            .Anchor = AnchorStyles.Bottom Or _
                AnchorStyles.Left Or AnchorStyles.Right Or _
                AnchorStyles.Top
            .Visible = True
            .SizeMode = PictureBoxSizeMode.Normal
        End With
        Me.Controls.Add(m_NamedPicture)

        ' Bind the NamedPicture control's FileName
        ' property to the DataTable's PicturePath member.
        m_NamedPicture.DataBindings.Add("FileName", _
            picture_table, "PicturePath")
    End Sub
End Class
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated