Filling list boxes in .NET code

[Migrated content. Thread originally posted on 11 May 2012]

The following code doesn't seem to display my listbox contents after performing it. What am I missing?

method-id LBox_Button_Click final private.
local-storage section.
01 my-listbox type System.Windows.Forms.ListBox.
01 companyName type String.
procedure division using by value sender as object e as type System.EventArgs.

set my-listbox to new System.Windows.Forms.ListBox
invoke my-listbox::BeginUpdate

move "Fred" to companyName
invoke my-listbox::Items::Add(companyName)

invoke my-listbox::EndUpdate

end method.
Parents
  • 1) To set the selection bar on a listbox value I use...

    invoke BankList::SetSelected(indexOfSelection, True)

    ..where BankList is my Listbox name and 'indexOfSelection' has been defined in local storage as binary long value 0.

    Yes, this will work correctly but is mostly used for multi-selection listboxes where you are selecting more than one item.

    For single selection listboxes you could also use:
    set BankList::SelectedIndex to indexOfSelection
    and the opposite to get the current setting use:
    set indexOfSelection to BankList::SelectedIndex


    & 2) to retrieve the data within my listbox where the user has selected a value from the list is...

    set BankDetails to BankList::GetSelected(indexOfSelection)

    ..where BankDetails is an 01 level containing subitems which appear as part of my listbox 'line' values

    No, this does not work like this as GetSelected returns a boolean value of true or false depending on if the index passed to it is selected or not.

    If you wish to return a list of selected items then you should look at the SelectedItems property or one of the others documented at: ListBox properties.

    Here is a sample of how to get some of these values:

           method-id button1_Click final private.
           local-storage section.
           01 indexofselection binary-long.
           01 bool  condition-value.
           01 selectedindex binary-long.
           01 selecteditem string.
           01 selecteditems type ListBox SelectedObjectCollection.
           procedure division using by value sender as object e as type System.EventArgs.
               set bool to BankList::GetSelected(indexOfSelection)
               display bool
               set selectedindex to BankList::SelectedIndex *> gets index of selection this is 0 based.
               display selectedindex
               set selecteditem to BankList::SelectedItem   *> for single selection listbox
               set selecteditems to BankList::SelectedItems *> for multiple selection listbox
                         
               perform varying selecteditem thru selecteditems *>display collection returned for multiple selection boxes
                  display selecteditem
               end-perform
                         
           end method.
           
Reply
  • 1) To set the selection bar on a listbox value I use...

    invoke BankList::SetSelected(indexOfSelection, True)

    ..where BankList is my Listbox name and 'indexOfSelection' has been defined in local storage as binary long value 0.

    Yes, this will work correctly but is mostly used for multi-selection listboxes where you are selecting more than one item.

    For single selection listboxes you could also use:
    set BankList::SelectedIndex to indexOfSelection
    and the opposite to get the current setting use:
    set indexOfSelection to BankList::SelectedIndex


    & 2) to retrieve the data within my listbox where the user has selected a value from the list is...

    set BankDetails to BankList::GetSelected(indexOfSelection)

    ..where BankDetails is an 01 level containing subitems which appear as part of my listbox 'line' values

    No, this does not work like this as GetSelected returns a boolean value of true or false depending on if the index passed to it is selected or not.

    If you wish to return a list of selected items then you should look at the SelectedItems property or one of the others documented at: ListBox properties.

    Here is a sample of how to get some of these values:

           method-id button1_Click final private.
           local-storage section.
           01 indexofselection binary-long.
           01 bool  condition-value.
           01 selectedindex binary-long.
           01 selecteditem string.
           01 selecteditems type ListBox SelectedObjectCollection.
           procedure division using by value sender as object e as type System.EventArgs.
               set bool to BankList::GetSelected(indexOfSelection)
               display bool
               set selectedindex to BankList::SelectedIndex *> gets index of selection this is 0 based.
               display selectedindex
               set selecteditem to BankList::SelectedItem   *> for single selection listbox
               set selecteditems to BankList::SelectedItems *> for multiple selection listbox
                         
               perform varying selecteditem thru selecteditems *>display collection returned for multiple selection boxes
                  display selecteditem
               end-perform
                         
           end method.
           
Children
No Data