Highlighted
jplonguev_sbcglobal_ Absent Member.
Absent Member.
15658 views

ListView set and get item information

Jump to solution

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

0 Likes
4 Solutions

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

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.

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

It doesn't show in Intellisense, which is odd, but if you just type it in it will work.

I will look into this.

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

There is an issue with a ListView in that when a selection changes the event is actually fired twice, once for the new selection and once for the deselection of the old selection. You must avoid reading the value when the event is fired for deselection as the index is invalid at that point.

I used the ItemSelectionChanged event instead of SelectedIndexChanged and check to see if the count of the SelectedItems collection is > 0 before reading the selection.

This works for a single selection listview but gets more complicated with a multiselection listview.

      method-id listView1_ItemSelectionChanged 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.Windows.Forms.ListViewItemSelectionChangedEventArgs.

           if listView1::SelectedItems::Count > 0

              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

              invoke listView1::Focus  

          end-if

       end method.

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

You can use an iterator to automatically process a list or collection like the following:

Each time thru it automatically getsthe next occurrence without the need for an index.

The following will process all current items in the listview:

01 row type ListViewItem.

01 column1 string.

01 column2 string.

01 column3 string.

            perform varying row thru listView1::Items

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

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

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

            end-perform

0 Likes
16 Replies
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

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.

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

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

 

 

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

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

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

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.

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

Many thanks Chris.

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

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.

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

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

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

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

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

It doesn't show in Intellisense, which is odd, but if you just type it in it will work.

I will look into this.

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

One more list view question.  I have a button that successfully retrieves the selection in the list view when clicked (thanks).  I would like to execute the same code when the user simply clicks on a different selection in the listview (SelectedIndexChanged).  I added the event and thought I could just copy the code to this method and it would function the same way.  I get an index error in the event handler that I don't get in the button method.  Is the event handler a totally different animal?

 

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

There is an issue with a ListView in that when a selection changes the event is actually fired twice, once for the new selection and once for the deselection of the old selection. You must avoid reading the value when the event is fired for deselection as the index is invalid at that point.

I used the ItemSelectionChanged event instead of SelectedIndexChanged and check to see if the count of the SelectedItems collection is > 0 before reading the selection.

This works for a single selection listview but gets more complicated with a multiselection listview.

      method-id listView1_ItemSelectionChanged 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.Windows.Forms.ListViewItemSelectionChangedEventArgs.

           if listView1::SelectedItems::Count > 0

              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

              invoke listView1::Focus  

          end-if

       end method.

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

To print the contents of the listview I need to increment the index of the row, I have it defined as below and have tried using [LVIdx] and [LVIdx + 1] to increment the rows and print the data.  Right now, I can only print the the first record.  Is is the way I have LVIdx defined in WS?

0 Likes
Micro Focus Expert
Micro Focus Expert

RE: ListView set and get item information

Jump to solution

You can use an iterator to automatically process a list or collection like the following:

Each time thru it automatically getsthe next occurrence without the need for an index.

The following will process all current items in the listview:

01 row type ListViewItem.

01 column1 string.

01 column2 string.

01 column3 string.

            perform varying row thru listView1::Items

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

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

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

            end-perform

0 Likes
jplonguev_sbcglobal_ Absent Member.
Absent Member.

RE: ListView set and get item information

Jump to solution

You are a genius and this post should answer about everything anybody needs to know about list view code in Visual COBOL.  Thanks so much Chris, you are the man.  Unbelievably helpful.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.