Results 1 to 5 of 5

Thread: [RESOLVED] Opening images with ListBox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    34

    Resolved [RESOLVED] Opening images with ListBox

    Hello, I have a ListBox.

    When I load up an image, a entry in added to the ListBox with the location of the image file. I also have a PictureBox... when you click a item in the listbox, I need the PictureBox to load the image associated with the ListBox entry.

    I tried this, but of I course it didn't work:

    Private Sub import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles import.Click
    Dim dlg As DialogResult = openfile.ShowDialog()
    Dim filename As String = openfile.FileName
    Images.Items.Add(filename)
    Dim selectpic = New Bitmap(Images.SelectedItem.Text)
    PictureBox1.Image = selectpic
    End Sub


    Now I know this is incorrect:

    Dim selectpic = New Bitmap(Images.SelectedItem.Text)

    But I am stumped on what to do, can I set a variable for each new entry which contains the image?

  2. #2

    Re: Opening images with ListBox

    So the ListBox contains the path to the image right?
    vb.net Code:
    1. PictureBox1.Image = Image.FromFile(Images.SelectedItem.ToString)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Opening images with ListBox

    Quote Originally Posted by formlesstree4 View Post
    So the ListBox contains the path to the image right?
    vb.net Code:
    1. PictureBox1.Image = Image.FromFile(Images.SelectedItem.ToString)
    Assuming that the ListBox contains Strings in the first place:
    vb.net Code:
    1. PictureBox1.Image = Image.FromFile(Images.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    34

    Resolved Re: Opening images with ListBox

    Thanks, works great. Only thing is, you always have to have a image selected or you'll get a ArgumentException error.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Opening images with ListBox

    Quote Originally Posted by iMiles View Post
    Thanks, works great. Only thing is, you always have to have a image selected or you'll get a ArgumentException error.
    That would be what validation is for. Test that an item is selected before using the selected item.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width