Inserting Pictures In A Textbox

Tyecom

Well-known member
Joined
Aug 8, 2007
Messages
78
Programming Experience
Beginner
I have a form with a TextBox1, ComboBox1, Button1, and PictureBox1. I have a folder (C:\Images) that contained images numbered from 1 to 500. I created a DataSet that populates the Combox and TextBox. My problem: When Textbox is populated with a number, that number corresponds with a image in C:\Images. That image should populate Picturbox1.

For example: The ComboBox list the names of Movies, the Textbox holds the MovieID number. When the button is clicked, the image associated to the number in TextBox1 is loaded in PictureBox1. Any help would be appreciated.
 
There are 2 ways you could change the image.
1) When clicking the button look to see what the value of the number is in TextBox1.Text and then load the image based on that.
OR
2) You could load the combobox with the Display being the Movie title and the value being the Movie number. Then handle the SelectedIndexChange event and load the appropriate image from the Pictures folder.
 
I know I can do this as you suggested, however, I would appreciate some sort of code. I've been at this for some time and I have figure it out. Please provide some type of sample of. Thanks in advance.
 
your datatable is bound through a bindingsource. Upon handling of the event fired when bindingsource.currentchanged, you should retrieve the current ID, and load the image, using Image.FromFile()

be aware that when bound to a datatable, myBindingSource.Current will return you a DataRowView wrapped in an object which you will cast to a datarowview, and then access the .Row property of (which you will cast into a type specific datarow if it was set up thus).

DirectCast(DirectCast(myBS.Current, DataRowView).Row, MyDataSet.MyWhateverDataRow).ImageID 'gets the image ID of the current row

If it was not set up thus, you'll do:

DirectCast(myBS.Current, DataRowView)("ImageID")
 
Please rename your Textbox1, Picturebox1 etc to something meaningful. Noone is going to want to read code if you have to explain what your variables are, not even you (if you return to your code 3 months later)
 
how do you populate the combobox, do you bind to it or are you looping? Some code might help us understand a little better.
 
Hi All,

I was able to get the answer I wanted. Here is the code:

Public Class Form1
Public Function AddImage(ByRef dir As String, ByRef img As String) As System.Drawing.Image
Dim temp As System.Drawing.Image = System.Drawing.Image.FromFile(dir & "\" & img & ".jpg")
Return temp
End Function

And the button's click event has:

PictureBox1.Image = AddImage("C:\Images", MovieID.Text)

I want to thank you all for your help and suggestions. I truly appreciate of the feedback. Thanks Again!!
 
Ok, I've been at this for a while. The images are populating the PictureBox perfectly; however, I can't seem to get the ComboBox to act right. Here's the problem: "Not Populating PictureBox With Correct Image When Using Dropdown Menu"

When running the program, in the ComboBox's drop down menu, I select MovieTitle "A", nothing happens.
Select another MovieTitle "B" and MovieTitle "A" loads into PictureBox.
Select another MovieTitle "C" and MovieTitle "B" loads into PictureBox.
Select another MovieTitle "D" and MovieTitle "C" loads into PictureBox, and so on.

The BindingNavigatorMoveNextItem_Click Event works fine. It loads the Movie Title, Movie ID, and loads the correct Image in PictureBox.

It loading out of sequence by 1 value. I've reading about "Positioning = -1 or + 1, etc.", but don't know if I'm on the right track.

Any suggestions would be greatly appreciated.
 
Sorry for just now responding, I've been under the weather lately. Can you explain the collection being 0? Here is the code:

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

'Open File Dialog Box.
Dim FileName As String
Dim Cancel As Integer = openFD.ShowDialog

FileName = openFD.FileName

openFD.InitialDirectory = "C:\Images"
openFD.Title = "Open Image Files"
openFD.Filter = "Image Files(*.jpg)|*.jpg"
'openFD.ShowDialog()
'FileName = openFD.FileName

If Cancel = Windows.Forms.DialogResult.Cancel Then
'MsgBox("Operation Cancelled!")
Else
FileName = openFD.FileName
MsgBox(FileName)
End If

'MsgBox(FileName)

End Sub
 
What is the code that you use to fill the ComboBox? What is the code for the ComboBox's SelectedIndexChanged event? What you are showing us is a way to manually fill the picturebox, or is this the way that you do it?
 
Back
Top