ListView set and get item information

My list view is populated initially thanks to the forum.  I need to be able to select the the first line in the list view now and return the data so I know the SiteListNum.  The examples for the listbox on the forum don't seem to work correctly for me with list view.  Can anyone provide an example?  My code is below:

*>PopulateListbox method      

method-id PopulateListbox final private.

local-storage section.

01 SiteList.

   05 SiteListDesc  pic x(40)   value spaces.

   05 SiteListNum   pic x(4)    value spaces.

01 indexOfSelection binary-long value 0.

01 row type ListViewItem.

procedure division.

    if ds-return = spaces

       invoke listView1::BeginUpdate

       perform varying idx from 1 by 1 until idx > 99

           if lb-siteentry(idx) > spaces

               set row to new ListViewItem(LB-SITEENTRY-DESC(idx))

               invoke row::SubItems::Add(LB-SITEENTRY-NUM(idx))

               invoke listView1::Items::Add(row)

           else

               exit perform   

           end-if

       end-perform

      

       *> set the selected listbox item to first line

        move 0 to indexOfSelection

 

       *> get the selected listbox item

       

       invoke listView1::EndUpdate

    end-if

end method.

*>end PopulateListbox method

  • The following is an example which populates a 3 column listview with data and then sets the 2nd row to be the selected row and then when a button is clicked it will extract the columns from the first selected row into appropriate data items which in this simple case are strings.

          class-id testlistview.Form1 is partial
                    inherits type System.Windows.Forms.Form.
          working-storage section.
          method-id NEW.
          01 row type ListViewItem.
          procedure division.
              invoke self::InitializeComponent
              set row to new ListViewItem("R1Column1 Data")
              invoke row::SubItems::Add("R1Column2 Data")
              invoke row::SubItems::Add("R1Column3 Data")
              invoke listView1::Items::Add(row)
              set row to new ListViewItem("R2Column1 Data")
              invoke row::SubItems::Add("R2Column2 Data")
              invoke row::SubItems::Add("R2Column3 Data")
              invoke listView1::Items::Add(row)
              set row to new ListViewItem("R3Column1 Data")
              invoke row::SubItems::Add("R3Column2 Data")
              invoke row::SubItems::Add("R3Column3 Data")
              invoke listView1::Items::Add(row)
            *> sets second row in listview to be selected row as indexes are 0 based.
              set listView1::Items[1]::Selected to true
              goback.
          end method.

          method-id button1_Click final private.
          01 row type ListViewItem.
          01 column1 string.
          01 column2 string.
          01 column3 string.
          procedure division using by value sender as object e as type System.EventArgs.

         *>  You can set the columns directly using the SubItems property of the SelectedItems property.
         *>  There can be multiple selected items so [0] gets the first one.

              set column1 to listView1::SelectedItems[0]::SubItems[0]
              set column2 to listView1::SelectedItems[0]::SubItems[1]
              set column3 to listView1::SelectedItems[0]::SubItems[2]

         *>  Or you can do the following which gets the selected Row from the SelectedItems property and then gets the column from it.

              set row to listView1::SelectedItems[0]
              set column2 to row::SubItems[1]
              set column3 to row::SubItems[2]
              goback.

          end method.
          end class.

  • set listView1::Items[0]::Selected to true... executes

    set row to listView1::SelectedItems[0]... throws an exception (InvalidArgument=Value of '0' is not valid for 'index'.Parameter name: index.

    set column1 to listView1::SelectedItems[0]::SubItems[0]          

    set column2 to listView1::SelectedItems[0]::SubItems[1]          

    set column3 to listView1::SelectedItems[0]::SubItems[2]... these three throw the same exception as above.  What ami I missing?

    Code:

    *>PopulateListbox method      

    method-id PopulateListbox final private.

    local-storage section.

    01 column1 string.

    01 column2 string.  

    01 column3 string.

     

    01 indexOfSelection binary-long value 0.

    01 row type ListViewItem.

      

    procedure division.

        if ds-return = spaces

           invoke listView1::BeginUpdate

           perform varying idx from 1 by 1 until idx > 99

               if lb-siteentry(idx) > spaces

                   set row to new ListViewItem(LB-SITEENTRY-DESC(idx))

                   invoke row::SubItems::Add(LB-SITEENTRY-NUM(idx))

                   invoke listView1::Items::Add(row)

               else

                   exit perform   

               end-if

           end-perform

          

           *> set the selected listbox item to first line, zero based

            set listView1::Items[0]::Selected to true

     

           *>test

            set column1 to listView1::SelectedItems[0]::SubItems[0]

            set column2 to listView1::SelectedItems[0]::SubItems[1]

            set column3 to listView1::SelectedItems[0]::SubItems[2]

          

           *> get the selected listbox item, zero based

            set row to listView1::SelectedItems[0]      

           

           *> get the selected listbox item, data is columns, zero based

            set row to listView1::SelectedItems[0]

            *>set DISP-SITE-DESC to row::SubItems[0]

            *>set DISP-SITE-ID to row::SubItems[1]

            set column2 to row::SubItems[0]

            set column3 to row::SubItems[1]

     

           invoke listView1::EndUpdate

        end-if

    end method.

    *>end PopulateListbox method

     

     

  • although set listView1::Items[0]::Selected to true... executes, when the form loads, the line is not highlighted??

  • Verified Answer

    You need to display the listview on the form after setting the selected property before you can read the selecteditems property because at the point you are trying to get the selecteditems the listview control has not yet been created. This is why you are getting the index errors.

    Also, the selected row will only be highlighted if the focus is set to the listview control when it is drawn.

    invoke listView1::Focus

    I am attaching my example that shows this working correctly.

    testlistview.zip
  • Chris,

    Thank you again.  Follow up question, the value returned below in { } is what I need to move into another data field, for example, site numer of 0001.  Is there a shortcut to just get the value enclosed in the braces or do I need to read the string in my cobol pgm for what is in the braces?  I don't see anything in the doc or the forum about this.

  • Sorry, I should have done this in the example:

    Using the Text property will retrieve just the value of the column:

       if listView1::Created

           set row to listView1::SelectedItems[0]

           set column1 to row::SubItems[0]::Text

           set column2 to row::SubItems[1]::Text

           set column3 to row::SubItems[2]::Text

       end-if

  • I saw an example of that in the VB forums online, tried to give it shot and I don't have the option: