Results 1 to 12 of 12

Thread: Selected ListBox item onto String Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Selected ListBox item onto String Array

    Hello all,

    I'm trying to write a programme that could open several files and process it oen by one. And I'm puting a feature in, to give the user to choose which files to be processed.

    What I've done is:
    I've enabled the OpenFile Dialog for multi file selection
    I've put the filenames onto a ListBox, and set the selection to multi selection

    When the programme runs, what I wish the programme to do is to copy the selected file names onto a string array. But it is giving me some errors.

    Could someone help me please. How could I copy the selected string on a ListBox onto a string array?


    Many thanks,
    Thomon

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: Selected ListBox item onto String Array

    For i = 0 To ListBox1.Items.Count - 1 Step 1
    If ListBox1.GetSelected(i) = True Then
    ListBox1.Items.Remove(i)
    ReDim Preserve file_names_copy(Y)
    filename_copy = ListBox1.SelectedItems(i)
    file_names_copy(Y) = Mid(filename_copy, 1, (Len(filename_copy) - 4)) & "_SUMMARY.csv" 'This is to change the file name from XXX to XXX_SUMMARY.CSV
    Y += 1
    End If
    Next i

    I'm not on the machine with the programme at the moment, so do forgive me for not being able to directly quoting the error code.
    But basically, when I opened 10 files, it displays the file path on the ListBox correctly, then I selected items 1,3,5,7,9 on the ListBox.
    When the programme runs, and 'i' in the For Loop goes up to 6 (for item 7), the programme halts at the line of "file_names_copy(Y) = ........". And said something about overflow.

    Please help :-)

    Cheers,
    Thomson

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: Selected ListBox item onto String Array

    I haven't declared any variables for you, apologise for that:

    Dim Preserve file_names_copy() as string
    Dim ilename_copy as string

    You might ask why I include this line:
    ListBox1.Items.Remove(i)
    The answer is...... well, I really couldn't answer. I actually had made the function worked on another application, but it just somehow doesn't feel like working for this application. And being a Newbie in programming, my fault analytical power is really limited.

    Cheers,
    T

  5. #5
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Selected ListBox item onto String Array

    First remove the line ListBox1.Items.Remove(i) and secondly loop through SelectedItems colletion instead of iterating through all the items. Also, if you loop through SelectedItems colletion then there will be need to write ListBox1.GetSelected(i) = True

  6. #6
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: Selected ListBox item onto String Array

    Another way is to use CopyTo
    Code:
    Dim strArray(ListBox1.SelectedItems.Count - 1) As String
    
            ListBox1.SelectedItems.CopyTo(strArray, 0)
    Note, this will throw an error if the items in the listbox are not strings, you could declare it as an Object array if not.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: Selected ListBox item onto String Array

    Thanks Deepak_Sakpal and user_name

    I've realised that as well
    I've actually changed it to "For Each S in ListBox1.selecteditems" to collect all the file names

    IT'S WORKING NOW!!!

    Thanks for all the help, :-)

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: Selected ListBox item onto String Array

    But I do have a follow up question, if I may:

    At the moment, I am displaying the full file path on the ListBox. Is there any easy way to trim it down so it only display the file name, rather than the full file path?

    Cheers,

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selected ListBox item onto String Array

    Search for the last backslash in the path.
    Then substring from that to end should give you the pure file name


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Selected ListBox item onto String Array

    Look into the classes in the IO namespace. I believe there is a Path object, and a File object. At least one of those will take a full path and return pretty nearly any piece of it that you could ask for.

    It's the Path class, I just looked. Pass it in the full path and you can get whatever you want out of it.
    Last edited by Shaggy Hiker; Dec 5th, 2008 at 11:09 AM.
    My usual boring signature: Nothing

  11. #11
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Selected ListBox item onto String Array

    Quote Originally Posted by dotdotdot
    But I do have a follow up question, if I may:

    At the moment, I am displaying the full file path on the ListBox. Is there any easy way to trim it down so it only display the file name, rather than the full file path?

    Cheers,
    vb.net Code:
    1. ' This will return only TST TS1.8_STEP12.txt
    2. MessageBox.Show(IO.Path.GetFileName("C:\Documents and Settings\dsakpal\Desktop\TST TS1.8_STEP12.txt"))

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    14

    Re: Selected ListBox item onto String Array

    Cool,

    Thanks for all the help

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