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
  • Is it your intention to dynamically add this listbox to the current Window or are you trying to populate a listbox that was created in the Forms designer?

    If you are dynamically adding this listbox control to the current form then you need to define its size and add it to the controls collection of the form.


               set my-listbox to new System.Windows.Forms.ListBox
               set my-listbox::FormattingEnabled to True
               set my-listbox::Location to new System.Drawing.Point( 124 37)
               set my-listbox::Name to "listBox2"
               set my-listbox::Size to new System.Drawing.Size( 120 95)
               set my-listbox::TabIndex to 1
               invoke self::Controls::Add(my-listbox)

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


    If you are creating the listbox using the Forms Designer then all of this code will automatically be generated for you in the Formn.Designer.cbl file.

    Then all you have to do is add the items to the listbox and display the form.

    You can also bind a listbox to an array or other list object so that you do not have to add the values specifically to the listbox control:


           local-storage section.
           01 companyNames type String occurs any.
           procedure division using by value sender as object e as type System.EventArgs.

               set content of companynames to ("Fred", "Jim", "Chris")
               
               set listBox1::DataSource to companyNames
           

Reply
  • Is it your intention to dynamically add this listbox to the current Window or are you trying to populate a listbox that was created in the Forms designer?

    If you are dynamically adding this listbox control to the current form then you need to define its size and add it to the controls collection of the form.


               set my-listbox to new System.Windows.Forms.ListBox
               set my-listbox::FormattingEnabled to True
               set my-listbox::Location to new System.Drawing.Point( 124 37)
               set my-listbox::Name to "listBox2"
               set my-listbox::Size to new System.Drawing.Size( 120 95)
               set my-listbox::TabIndex to 1
               invoke self::Controls::Add(my-listbox)

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


    If you are creating the listbox using the Forms Designer then all of this code will automatically be generated for you in the Formn.Designer.cbl file.

    Then all you have to do is add the items to the listbox and display the form.

    You can also bind a listbox to an array or other list object so that you do not have to add the values specifically to the listbox control:


           local-storage section.
           01 companyNames type String occurs any.
           procedure division using by value sender as object e as type System.EventArgs.

               set content of companynames to ("Fred", "Jim", "Chris")
               
               set listBox1::DataSource to companyNames
           

Children
No Data