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
  • Verified Answer

    If you are creating the listboix in the designer then do not add the control to the Controls collection as this will automatically be done in the designer.cbl file.

    The SelectedIndex event doesn't fire until the listbox has focus and you change the selection in the listbox by highlighting it.

    If you just want to populate the listbox with data when the form is created you should do this in the New event after the InitializeComponent method id invoked.

    Try something like the following:


           working-storage section.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               invoke self::populateListbox           
               goback.
           end method.

           method-id populateListbox public.
           local-storage section.
           01 companyName string.
           procedure division.
               invoke CompanyList::BeginUpdate
               move "Fred" to companyName
               invoke CompanyList::Items::Add(companyName)
               invoke CompanyList::EndUpdate
               goback.
           end method.
Reply
  • Verified Answer

    If you are creating the listboix in the designer then do not add the control to the Controls collection as this will automatically be done in the designer.cbl file.

    The SelectedIndex event doesn't fire until the listbox has focus and you change the selection in the listbox by highlighting it.

    If you just want to populate the listbox with data when the form is created you should do this in the New event after the InitializeComponent method id invoked.

    Try something like the following:


           working-storage section.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               invoke self::populateListbox           
               goback.
           end method.

           method-id populateListbox public.
           local-storage section.
           01 companyName string.
           procedure division.
               invoke CompanyList::BeginUpdate
               move "Fred" to companyName
               invoke CompanyList::Items::Add(companyName)
               invoke CompanyList::EndUpdate
               goback.
           end method.
Children
No Data