Skip to main content

How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

Something like the following should work:

   if checkedListBox1::CheckedItems::Count > 0
        declare s as string
        declare allitems as string = ""
        perform varying s thru checkedListBox1::CheckedItems
             set allitems to allitems & s & " "
        end-perform
        invoke type MessageBox::Show (allitems)
   end-if


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

Hi Chris

    This worked great. I have one additional problem. I should have stated this before. I also need to remove the checked items from the list. Here is the method I have so far:

      method-id btnDeleteNames_Click final private.

      Working-Storage Section.

      01 NamesArray.

          05 NamesTable    Occurs 1 to 99 Times Depending on ListCount Indexed by Inx.

              10 ANum       Pic 9(5).

              10            Pic X.

              10 AName      Pic X(50).

      01 ListCount          Pic 99.

      procedure division using by value sender as object e as type System.EventArgs.

          Set ListCount to clbNamess::CheckedItems::Count.

          If clbNamess::CheckedItems::Count > 0

              Declare S as string

              Declare Itm as Object

              Set Inx to 1

              Perform varying S thru clbNames::CheckedItems

                  Set NamesTable(Inx) to S

                  Set Inx up by 1

              End-Perform

              Set I to 1

              Perform Until I > ListCount

                  Set Itm to NamesTable(I)

                  Invoke clbNames::Items::Remove(Itm)

                  Set I up by 1

              End-Perform

          Invoke type MessageBox::Show(NamesArray)

      end-if

      end method.

When I execute this code the Table is built, but nothing is Removed from the list.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

The items are not being removed because your PIC X(50) definition would retain trailing spaces and then the item would not be found in the items collection.

Try using the S string data item instead of defining an object as follows:

             Perform varying i from 1 by 1 until i > ListCount
                 Set S to NamesTable(I)
                 Invoke clbNamess::Items::Remove(S::TrimEnd)
             End-Perform


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!


First, you're trying to remove items by index. That requires the ::RemoveAt method, not the ::Remove method.

Second, collection::RemoveAt(index) will change the indices of all the remaining items. So if you want to remove items at indices 3 and 5 from a 10-item collection, for example, after removing the item at 3, the one formerly at 5 will now be at 4. So indices are not the way to go, unless you want to perform an additional calculation in your loop.

Third, using a COBOL table makes this code more complex and verbose than it needs to be. Use the Framework.

declare item as object
perform varying item through clbNames::CheckedItems
   invoke clbNames::Items::Remove(item)
end-perform

I haven't tested that, but it ought to work. CheckedItems and Items are separate collections, so you can iterate through the former while modifying the latter.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

Hi Chris

    Never Mind. I figured it out. I replaced the second loop with the following:

              Set I to 1

              Perform Until I > ListCount

                  Move NameTable(I) to SearchString

                  Invoke clbNames::Items::RemoveAt(clbNames::FindString(SearchString))

                  Set I up by 1

              End-Perform

It didn't work if I used NameTable(I) in "FindString". Too bad, I like brevity.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

I should have looked at your latest answer. I *** it better than the one I used.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

I should have looked at your latest answer. I *** it better than the one I used.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

Gah. Ignore my comments above about removing by index - I misread your code.

That said, I'd expect the code I posted above to work. If it doesn't, let me know and I'll throw together an actual sample and get it working properly.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

Chris, The latest suggestion didn't work. I used this instead:

              Perform varying I from 1 by 1 until I > ListCount

                  Move NameTable(I) to SearchString

                  Invoke clbNames::Items::RemoveAt(clbNames::FindString(SearchString))

              End-Perform

SearchString is only 5 characters witch will hold the Customer number.


How do I get a list of checked Items from a checked List Box? I have tried several methods but it won't accept anything. I need help!

The same code that I have to you worked on my side but I may be initializing the Items differently than you are.

If I take Michael's suggestion and replace the COBOL table with .NET Framework classes I can get it to work with only the following code where the list of items selected witll be in stringArray:

 

          if clbNamess::CheckedItems::Count > 0
              declare s1 s2 as string = ""
              declare stringArray as string occurs 99
              invoke clbNamess::CheckedItems::CopyTo(stringArray, 0)
              perform varying s1 thru stringArray
                 invoke clbnamess::Items::Remove(s1)
                 set s2 to s2 & s1 & " "     
              end-perform
              invoke type MessageBox::Show(s2)
          end-if