943,754 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3250
  • VB.NET RSS
Sep 20th, 2008
0

Open Images using combo box

Expand Post »
I am having a combo box in my form and i want to open a image using that now whenever i select any option from the combo box different different images should be displayed what cud be the necessary code and what event should be used to write the necessary code for it plz guide with all necessary deatils along with a example code..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asmit1987 is offline Offline
22 posts
since Sep 2008
Sep 20th, 2008
0

Re: Open Images using combo box

1/to use image in the form you need PictureBox Control
2/in order to change the picture whenever the user select item from the comboBox you use the SelectedIndexChanged event, you can get the item that user selected by
VB.NET Syntax (Toggle Plain Text)
  1. dim selectedItem as string
  2. selectedItem = ComboBox.Text
Last edited by manal; Sep 20th, 2008 at 9:41 pm.
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Sep 20th, 2008
0

Re: Open Images using combo box

Quote ...
dim selectedItem as string
selectedItem = ComboBox.Text
why didn't use selectedItem = ComboBox.SelectedItem ?
and there are path on combobox item to loaded?
Last edited by Jx_Man; Sep 20th, 2008 at 11:50 pm.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Sep 21st, 2008
0

Re: Open Images using combo box

i am opening the image frm the combo box the image is placed in a picture box now if my first item in the list be x then what code should be written to open the image in the picture box
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asmit1987 is offline Offline
22 posts
since Sep 2008
Sep 21st, 2008
0

Re: Open Images using combo box

Do you store the file path in your combo box? Like this:

VB.NET Syntax (Toggle Plain Text)
  1. ComboBox1.Items.Clear()
  2. ComboBox1.Items.Add("D:\Download\Image1.jpg")
  3. ComboBox1.Items.Add("D:\Download\Image2.jpg")

then you get the image with:

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2. '
  3. ' Load picture from the file
  4.  
  5. Try
  6. PictureBox1.Image = Image.FromFile(ComboBox1.SelectedItem.ToString)
  7. Catch ex As Exception
  8. ' Handle exception
  9. End Try
  10.  
  11. End Sub

ComboBox1.SelectedItem is of the type object so you have to cast it to the string.
However, if you store "friendly names" in your combo box, you need some mapping to file names. Here's a one way to do that:

VB.NET Syntax (Toggle Plain Text)
  1. Private ImagePaths() As String
  2.  
  3. ComboBox1.Items.Clear()
  4. ComboBox1.Items.Add("My dog")
  5. ComboBox1.Items.Add("My wife")
  6. ReDim ImagePaths(1)
  7. ImagePaths(0) = "D:\Download\Image1.jpg"
  8. ImagePaths(1) = "D:\Download\Image2.jpg"

and then you do the mapping to get the image:

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2. '
  3. ' Load picture from the file
  4.  
  5. Try
  6. PictureBox1.Image = Image.FromFile(ImagePaths(ComboBox1.SelectedIndex))
  7. Catch ex As Exception
  8. ' Handle exception
  9. End Try
  10.  
  11. End Sub
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Procdedure for sending sms using vb.net 2005 help
Next Thread in VB.NET Forum Timeline: Using Graphics





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC