|
-
Dec 5th, 2008, 05:35 AM
#1
Thread Starter
New Member
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
-
Dec 5th, 2008, 06:01 AM
#2
Re: Selected ListBox item onto String Array
What is the error? Can you post your code?
-
Dec 5th, 2008, 06:56 AM
#3
Thread Starter
New Member
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
-
Dec 5th, 2008, 07:01 AM
#4
Thread Starter
New Member
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
-
Dec 5th, 2008, 07:33 AM
#5
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
-
Dec 5th, 2008, 07:46 AM
#6
Hyperactive Member
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.
-
Dec 5th, 2008, 08:30 AM
#7
Thread Starter
New Member
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, :-)
-
Dec 5th, 2008, 08:33 AM
#8
Thread Starter
New Member
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,
-
Dec 5th, 2008, 09:53 AM
#9
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
-
Dec 5th, 2008, 11:05 AM
#10
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
 
-
Dec 7th, 2008, 11:17 PM
#11
Re: Selected ListBox item onto String Array
 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:
' This will return only TST TS1.8_STEP12.txt
MessageBox.Show(IO.Path.GetFileName("C:\Documents and Settings\dsakpal\Desktop\TST TS1.8_STEP12.txt"))
-
Dec 9th, 2008, 11:20 AM
#12
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|