Results 1 to 6 of 6

Thread: [RESOLVED] Yet Another ListView Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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...)".

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: Yet Another ListView Question

    vb Code:
    1. For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
    2.             test &= CheckedListBox1.CheckedItems(i).ToString
    3.         Next

    replace the test &= with whatever variable you're using to store the string.

    (*edit* or you can go the route above )
    Last edited by BackWoodsCoder; Aug 17th, 2009 at 03:26 PM. Reason: too slow!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    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 *^*&(

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    New Romney, Kent, UK
    Posts
    232

    Cool Re: Yet Another ListView Question

    Oh Crap I didn't have the 'Output' window open. I thought the output would appear in the 'messages/errors' window.

    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.

    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

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