Results 1 to 4 of 4

Thread: OpenFileDialog & Array help

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    2

    OpenFileDialog & Array help

    I want to use an open file dialog, and be able to select multiple images and have their full path names loaded in an array of an unspecified size... so far I've got to here, but am stuck at how to proceed.

    Can someone help?

    VB Code:
    1. Private Sub Menu_Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Menu_Open.Click
    2.  
    3.         'declares undefined array
    4.         Dim Picturelist() As String
    5.  
    6.         Dim fileName As String
    7.         Dim i As Integer
    8.  
    9.         ofdPicture.InitialDirectory = "C:\"
    10.         ofdPicture.Multiselect = True
    11.         ofdPicture.Filter = "Image Files (*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF"
    12.         ofdPicture.ShowDialog()

    I know I'm meant to use ReDim but how would I "count" the number of individual files that have been selected by the user?

    Would I have to use a loop to put the path name into the array? Something like

    VB Code:
    1. For i = 1 To Picturelist.GetUpperBound(Picturelist)
    2.             ReDim Preserve Picturelist(Picturelist.GetUpperBound + 1)
    3.             Picturelist(i) = ofdPicture.FileName
    4.         Next i

  2. #2
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear friend, you don't have to redim, but simply to assign Filenames (plural, not Filename) to your array, without specifying an index:

    Picturelist = ofdPicture.FileNames

    That's all!
    If,for other situation you need something easy to redim, have a look to arraylist.
    Good job
    Live long and prosper (Mr. Spock)

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    2
    Thank you for your help! It set me on the right track..... greatly appreciated!

    Now a slight deviation - if I was to want to select more images again using OFD, wouldn't the original list of filenames in the array be overridden?

    How would I go about changing this? I would have to use ReDim now, no? To increase the size of the array...

    I did this:

    VB Code:
    1. If ofdPicture.ShowDialog = DialogResult.OK Then
    2.             ReDim Preserve Pictures(i)
    3.             For i = 0 To ofdPicture.FileNames.GetUpperBound(0)
    4.                 Pictures(i) = ofdPicture.FileNames(i)
    5.             Next i
    6.         End If

    Not entirely sure if it was correct though, because I didn't specify the limit to the array?

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I can't test my code, now, so be careful, but i think it should be useful, also if wrong. I think you can understand, anyway, which is the problem in the code you posted.
    Excuse my english!

    VB Code:
    1. Dim OldFinalPosition as integer=pictures.getlenght(0)
    2. If ofdPicture.ShowDialog = DialogResult.OK Then
    3.             ReDim Preserve Pictures(pictures.ofdPicture.FileNames.GetLenght(0)+OldFinalPosition- 1)
    4.             For i = 0 To ofdPicture.FileNames.GetUpperBound(0)
    5.                 Pictures(i+oldfinalposition) = ofdPicture.FileNames(i)
    6.             Next i
    7.         End If

    Let me know if it' s sufficient to help you.
    Live long and prosper (Mr. Spock)

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