Binding picture box problem?

mythinky

Member
Joined
Jun 4, 2004
Messages
20
Programming Experience
1-3
Thanks for friends that reply my post before....
I am binding a picture box to a path... It works fine if the path is valid. Problem comes when tha path is invalid or empty...
The code that is red highlighted is where the error come out..

Private Sub BindFields()
Try
Dim b As Binding = New Binding("Image", objDView, "CoverSample")
AddHandler b.Format, AddressOf MyPictureBox_FormatImage
picBook.DataBindings.Add(b)
Catch ex As Exception
MsgBox(ex.Message, , "BindFields")
End Try
End Sub

Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
If Trim(e.Value.ToString) <> "" Then
Dim fi As New System.IO.FileInfo(e.Value.ToString)
If fi.Exists() Then
e.Value = Image.FromFile(e.Value.ToString)
isValidPath = True
End If
End If
Catch ex As Exception
MsgBox(ex.Message, , "MyPictureBox_FormatImage")
End Try
End SubpicBook.DataBindings.Add(b)picBook.DataBindings.Add(b)
 

tlkerns

Member
Joined
Jun 3, 2004
Messages
19
Programming Experience
3-5
This looks familiar.

1. Does the error occur at the line:

picBook.DataBindings.Add(b)

in the BindFields method?

2. What's the error message?

Tony
 

tlkerns

Member
Joined
Jun 3, 2004
Messages
19
Programming Experience
3-5
Nevermind, I think. It looks like you are checking for invalid or empty paths, but you aren't changing the return value when this happens. Try doing something like this:

Private Sub MyPictureBox_FormatImage(ByVal sender As Object, ByVal e As ConvertEventArgs)
If Trim(e.Value.ToString) <> "" Then
Dim fi As New System.IO.FileInfo(e.Value.ToString)
If fi.Exists() Then
e.Value = Image.FromFile(e.Value.ToString)
Else
e.Value = Nothing '<-- I'm not sure about this
End If
Else
e.Value = Nothing '<-- I'm not sure about this
End If
End Sub

Tony
 
Top Bottom