Highlighted
Absent Member.
Absent Member.
3196 views

Filling list boxes in .NET code

Jump to solution

[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.
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Filling list boxes in .NET code

Jump to solution
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.

View solution in original post

0 Likes
8 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Filling list boxes in .NET code

Jump to solution
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
       

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Filling list boxes in .NET code

Jump to solution
Have defined the listbox as part of a form. Listbox is called 'CompanyList'.

As part of my code 'behind the form' code reads as:

method-id CompanyList_SelectedIndexChanged final private.
local-storage section.
01 companyName type String.
procedure division using by value sender as object e as type System.EventArgs.

invoke self::Controls::Add(CompanyList)
invoke CompanyList::BeginUpdate

move "Fred" to companyName
invoke CompanyList::Items::Add(companyName)
invoke CompanyList::EndUpdate

end method.


When running the form, from the beginning, this code does not appear to be executed and my 'list box' remains empty.

What commands trigger this code to run so that on loading the screen the listbox is filled in?

Thanks.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Filling list boxes in .NET code

Jump to solution
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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Filling list boxes in .NET code

Jump to solution
Cheers. Thats exactly what I was looking for.

Can you confirm that the following TWO lines of code do what I THINK they should do...

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.

& 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

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Filling list boxes in .NET code

Jump to solution
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.
       
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Filling list boxes in .NET code

Jump to solution
Looking good.

A further question.....Having filled my listbox with items I'vse selected one for 'amending' which I've then taken the listbox line content and placed on my form in 'amend boxes'. Having amended the entry (address lets say) how do I now 'refresh' the listbox so that it takes account of my newly amended address?

Cheers.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Filling list boxes in .NET code

Jump to solution
There are many ways of doing this including using binding as I mentioned earlier but the most simple is probably something like:


    set BankList::Items::Item(indexOfSelection) to newitem


where indexofSelection contains the index of the item to change and newitem is the modified item.

You can also use add, remove. etc methods to change items in listbox item collection.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Filling list boxes in .NET code

Jump to solution
Many thanks for the help on this topic.
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.