[RESOLVED] Yet Another ListView Question
Hiya People,
Newbie here, I've been trawling these forums, and the net all day. Modified lots of code examples to try and fit my needs. Nothing as yet, have I managed to get to work :(
I have finally decided to post my own question, so here goes.
I have a CheckedListView (suggested in a post I read, and it seems to fit my needs) which I populate with the filtered names of the files from within a selected folder.
Then the user selects the ones to process, by using the checkboxes. So basically it is a single column text box with checkboxes down the left hand side.
What I've been trying to do is get the selected text (i.e. the name of the file) and put it in a string array for later use.
Please make any answers verbose, as I've not been using .net for very long, and need a bit of spoon feeding.
I've used various forms of: For each item as .....ListViewItem......Next and also various forms of for x = 1 to Listitem.count......Next
e.g.
Code:
Dim DWGListArray() As String
Dim X As Integer = LSCheckedListBox1.Items.Count
Dim Ctr As Integer
For Ctr = 0 To X
If LSCheckedListBox1.Items(Ctr).Checked Then
DWGListArray(Ctr) = LSCheckedListBox1.Items(Ctr).Text
Debug.Print(DWGListArray(Ctr))
End If
Which would you suggest, ensuring that the filename text goes into the str array. Any help would be very much appreciated.
Regards
GB :)
Re: Yet Another ListView Question
Well, why don't you tell us what isn't working?
Two things though:
- Instead of looping from 0 to X you should loop from 0 to X-1, since the items are zero-based. If you have 3 items (X = 3) then items(2) is the last item, and items(3) does not exist.
- Instead of using an array, I advice you to use a List(Of String) instead. Replace the first line with "Dim DWGList As New List(Of String)" and replace the 'add to array' code with "DWGList.Add(name here...)".
Re: Yet Another ListView Question
The checkedlistbox class has the CheckedIndices and CheckedItems properties. If you need to get the text of the checked items then you just read the checkeditems property, which returns a checkeditemcollection that you can loop thru and get the individual checked item
Code:
For Each checkedItem As Object In LSCheckedListBox1.CheckedItems
Debug.WriteLine(checkedItem.ToString)
Next
There is no need to create a separate array to hold these checked items because you can access them any time via the checkedlistbox.checkeditems property.
Re: Yet Another ListView Question
vb Code:
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
test &= CheckedListBox1.CheckedItems(i).ToString
Next
replace the test &= with whatever variable you're using to store the string.
(*edit* or you can go the route above ;) )
Re: Yet Another ListView Question
Gee, Thank a bunch people, That's really great, that'll give me something to get on with tomorrow. I was getting really disheartened with my lack of progress today, and hopefully I'll get this sorted tomorrow.
@ NickThissen - I couldn't post too much as I mucked about with so much code today, its difficult to know which bit was closest to being right, and which was a total pile of *^*&( :D
Re: Yet Another ListView Question
Oh Crap :cry: I didn't have the 'Output' window open. I thought the output would appear in the 'messages/errors' window. :eek:
I though something was strange when there was no output from your samples, so I added a textbox and it worked fine. Explorered the views menu and saw 'output' clicked on it and saw all my previous attempts in the window which appeared. How gutted am I. That means I may have cracked it yesterday without knowing. :blush:
Nevermind, lesson learnt I hope this helps some other 'newb'.
Many thanks for your help guys, I feel much better today, so now to poke these filenames into the application I want them to run in, ;) No doubt you'll be hearing from me again, so untill then, adios amigos :wave: