none
OpenFileDialog load multiple images

    Question

  • Hi, using the following code mashup i am not quite getting the desired results. If anyone has the time and/or solution it would really help.
    I have a panel with a button and multiple PictureBox'  i can select all the images but only get one image to load and or replace the existing image on PictureBox1.
    I am not sure where i am going wrong, if the code itself is the best way to go or if there is an even easier way to do this.

        Private Sub ButtonLoadImages_Click(sender As Object, e As EventArgs) Handles ButtonLoadImages.Click
            OpenFileDialog2.Multiselect = True
            OpenFileDialog2.Title = "Select Image to Import"
            OpenFileDialog2.Filter = "Image Files (JPG,GIF,BMP,)|*.jpg;*.gif;*.bmp"
            If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim PicBox() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
                Dim nextPicBox = PicBox.Where(Function(x) IsNothing(x.Image)).FirstOrDefault
                If Not IsNothing(nextPicBox) Then
                    nextPicBox.ImageLocation = OpenFileDialog2.FileName
                End If
            End If
        End Sub


    Thanks in advance.


    regards Flatland France


    Monday, October 10, 2016 10:04 AM

Answers

  • Air code (untested)

        Private Sub ButtonLoadImages_Click(sender As Object, e As EventArgs) Handles ButtonLoadImages.Click
            OpenFileDialog2.Multiselect = True
            OpenFileDialog2.Title = "Select Image to Import"
            OpenFileDialog2.Filter = "Image Files (JPG,GIF,BMP,)|*.jpg;*.gif;*.bmp"
            If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim PicBox() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
                For Each s As String In Me.OpenFileDialog2.FileNames
                    Dim nextPicBox = PicBox.Where(Function(x) IsNothing(x.Image)).FirstOrDefault
                    If Not IsNothing(nextPicBox) Then
                        nextPicBox.ImageLocation = s
      nextPicBox.Refresh() End If Next End If End Sub

    Your example really doesn't help. My example has OpenFileDialog2 whereas you have 1 (minor), your example use messagebox.Show ??? I don't need a message box. Your example use Me. (isn't that for the form itself)? I am using a panel with Pictureboxs. Loop over it's entries? Got me there.

    I am not sure where you pasted the code from, but personally i would prefer that my code is corrected so i can see what i have done wrong.

    btw, excuse me for trying to help :-(

    Monday, October 10, 2016 10:51 AM

All replies

  • Hi,

    instead of using the FileName Property, use the FileNames property and loop over its entries:

            Me.OpenFileDialog1.Multiselect = True
            If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                For Each s As String In Me.OpenFileDialog1.FileNames
                    MessageBox.Show(s)
                Next
            End If

    Regards,

      Thorsten

    Monday, October 10, 2016 10:14 AM
  • Hi,

    instead of using the FileName Property, use the FileNames property and loop over its entries:

            Me.OpenFileDialog1.Multiselect = True
            If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                For Each s As String In Me.OpenFileDialog1.FileNames
                    MessageBox.Show(s)
                Next
            End If

    Regards,

      Thorsten

    I don't want to sound dumb, even if i will, but i don't understand what you mean.
    With my limited knowledge, it took me 2 days just to get my code above to work with undesirable results.

    Your example really doesn't help. My example has OpenFileDialog2 whereas you have 1 (minor),
    your example use messagebox.Show ??? I don't need a message box.
    Your example use Me. (isn't that for the form itself)? I am using a panel with Pictureboxs.
    Loop over it's entries? Got me there.

    I am not sure where you pasted the code from, but personally i would prefer that my code is corrected so i can see what i have done wrong.

    lol i am even more confused than before.

    Never mind thanks anyway, i'll keep playing around and googling until something falls into place.


    regards Flatland France

    Monday, October 10, 2016 10:43 AM
  • Air code (untested)

        Private Sub ButtonLoadImages_Click(sender As Object, e As EventArgs) Handles ButtonLoadImages.Click
            OpenFileDialog2.Multiselect = True
            OpenFileDialog2.Title = "Select Image to Import"
            OpenFileDialog2.Filter = "Image Files (JPG,GIF,BMP,)|*.jpg;*.gif;*.bmp"
            If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim PicBox() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
                For Each s As String In Me.OpenFileDialog2.FileNames
                    Dim nextPicBox = PicBox.Where(Function(x) IsNothing(x.Image)).FirstOrDefault
                    If Not IsNothing(nextPicBox) Then
                        nextPicBox.ImageLocation = s
      nextPicBox.Refresh() End If Next End If End Sub

    Your example really doesn't help. My example has OpenFileDialog2 whereas you have 1 (minor), your example use messagebox.Show ??? I don't need a message box. Your example use Me. (isn't that for the form itself)? I am using a panel with Pictureboxs. Loop over it's entries? Got me there.

    I am not sure where you pasted the code from, but personally i would prefer that my code is corrected so i can see what i have done wrong.

    btw, excuse me for trying to help :-(

    Monday, October 10, 2016 10:51 AM
  • ... oh, you need to refresh each picturebox after assigning the ImageLocation. I updated my code from the last reply.

    Regards,

      Thorsten


    Monday, October 10, 2016 10:56 AM
  • Air code (untested)

        Private Sub ButtonLoadImages_Click(sender As Object, e As EventArgs) Handles ButtonLoadImages.Click
            OpenFileDialog2.Multiselect = True
            OpenFileDialog2.Title = "Select Image to Import"
            OpenFileDialog2.Filter = "Image Files (JPG,GIF,BMP,)|*.jpg;*.gif;*.bmp"
            If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim PicBox() As PictureBox = {PictureBox1, PictureBox2, PictureBox3, PictureBox4}
                For Each s As String In Me.OpenFileDialog2.FileNames
                    Dim nextPicBox = PicBox.Where(Function(x) IsNothing(x.Image)).FirstOrDefault
                    If Not IsNothing(nextPicBox) Then
                        nextPicBox.ImageLocation = s
      nextPicBox.Refresh() End If Next End If End Sub

    Your example really doesn't help. My example has OpenFileDialog2 whereas you have 1 (minor), your example use messagebox.Show ??? I don't need a message box. Your example use Me. (isn't that for the form itself)? I am using a panel with Pictureboxs. Loop over it's entries? Got me there.

    I am not sure where you pasted the code from, but personally i would prefer that my code is corrected so i can see what i have done wrong.

    btw, excuse me for trying to help :-(

    @Thorsten, Thank you once again. This is not the first time you have helped me out of a tight spot and i very much appreciate it. I am just sorry that i can't return the favor, you are already light years ahead of me ;-)

    Kind regards.

    BTW, you are excused :-D sorry if i sounded off hand it wasn't intended.


    regards Flatland France


    Monday, October 10, 2016 5:05 PM

  • @Thorsten, Thank you once again. This is not the first time you have helped me out of a tight spot and i very much appreciate it.

    [...]
    Kind regards.


    regards Flatland France


    Thanks! :-)

    Regards,

      Thorsten

    Monday, October 10, 2016 5:28 PM