Help! Update picture not working right  
Author Message
Dangerous Dan





PostPosted: Visual Basic General, Help! Update picture not working right Top

Hi all.

I will attempt to explain as best I can.

I have created a sql database which holds a few fields. One of the fields is the path to a photo stored on my hard drive which is related to the record.

When I move through the records I want the photo to load and display on my form, in the picture box.

I have created my form with the fields all on it, and the picture box. When I dragged the fields on from the Data Sources box a toolbar was automatically created that allowed me to navigate the records. I.e a previous button, a record count X of Y, a next button, an add button, a save button.

The fields all store the information ok, and the navigation buttons work fine. The problem I have is that I cannot get the photo to update after the record has been loaded.

This is all the code in my form:

Public Class Form1

Private Sub PlantsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlantsBindingNavigatorSaveItem.Click

Me.Validate()

Me.PlantsBindingSource.EndEdit()

Me.PlantsTableAdapter.Update(Me.PlantsDataSet.Plants)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'TODO: This line of code loads data into the 'PlantsDataSet.Positions' table. You can move, or remove it, as needed.

Me.PositionsTableAdapter.Fill(Me.PlantsDataSet.Positions)

'TODO: This line of code loads data into the 'PlantsDataSet.PlantTypes' table. You can move, or remove it, as needed.

Me.PlantTypesTableAdapter.Fill(Me.PlantsDataSet.PlantTypes)

'TODO: This line of code loads data into the 'PlantsDataSet.PlantTypes' table. You can move, or remove it, as needed.

Me.PlantTypesTableAdapter.Fill(Me.PlantsDataSet.PlantTypes)

'TODO: This line of code loads data into the 'PlantsDataSet.Plants' table. You can move, or remove it, as needed.

Me.PlantsTableAdapter.Fill(Me.PlantsDataSet.Plants)

If PhotoPathTextBox.Text = "" Then

'do nothing

Else

UpdatePhoto()

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

OpenFileDialog1.ShowDialog()

PhotoPathTextBox.Text = OpenFileDialog1.FileName

UpdatePhoto()

End Sub

Private Sub UpdatePhoto()

PictureBox1.Image = System.Drawing.Image.FromFile(PhotoPathTextBox.Text)

End Sub

Private Sub BindingNavigatorMovePreviousItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMovePreviousItem.Click

UpdatePhoto()

End Sub

Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click

UpdatePhoto()

End Sub

End Class

As you can see i have created a sub called updatephoto() when, when called, reads in the photo. The problem is that when I navigate the records it seems to read the photo in first, then update the record, so the photo is always of the record before!

I hope im making sense!

Thanks

Dan



Visual Basic1  
 
 
nobugz





PostPosted: Visual Basic General, Help! Update picture not working right Top

Try using the PositionChanged event on the binding source. You'll also need BindingComplete to display the photo when the form displays the 1st record.