Page 1 of 1

Listbox items

Posted: Mon Jan 16, 2017 8:16 pm
by bluebird
The listbox control below has an items array literally declared within. A function to return the value was also written as shown.

1.)
Can that items entry be an array variable, example
aTypes :={"Financial","Commercial","Technical","Entertainment"}
.
.
.

ITEMS aTypes

If so, can the same listbox be invoked and re-used with different arrays, each containing other items lists?

2) If you want to select an "item", do you have to write a function like I did to fetch the selection so that it can be
displayed elsewhere?

DEFINE LISTBOX L_Type
ROW 73
COL 70
PARENT Form_2
WIDTH 200
HEIGHT 35
VALUE 0
VISIBLE .F.
ON LOSTFOCUS GetcType()
ITEMS {"Financial","Commercial","Technical","Entertainment"}
END LISTBOX

*-----------------------------------------------------
Function GetcType()
*-----------------------------------------------------
Local nTypeIndex:=0, cTypex

form_2.L_Type.visible:=.F. //hide the listbox after using it
form_2.T_Type.visible:=.T. //show the textbox with the value over top of hidden listbox

nTypeIndex:=Form_2.L_type.value // get litbox index to "items"
cTypex:=aTypes[nTypeIndex] // find out its value from a LOCAL array that has the same values as the ListBox
form_2.T_Type.Value:=cTypex // display the selected "Item"

return

Listbox items

Posted: Mon Jan 16, 2017 9:33 pm
by Pablo César
Sorry, I did not understand it...

Would you make a practical example to make easier the test ?

Re: Listbox items

Posted: Tue Jan 17, 2017 3:21 am
by Rathinagiri
bluebird wrote: Mon Jan 16, 2017 8:16 pm 1.)Can that items entry be an array variable, example
aTypes :={"Financial","Commercial","Technical","Entertainment"}
.
.
.

ITEMS aTypes

If so, can the same listbox be invoked and re-used with different arrays, each containing other items lists?
Yes! You can use any array of items to an existing listbox.

For that you need to write coding for adding the array items. It can be done in a small function also. For example,

Code: Select all

aMyArray := { 'Item1', 'Item2', 'Item3' }
myform.mylistbox.deleteallitems
for i := 1 to len( aMyArray )
   myform.mylistbox.additem( aMyArray[ i ] )
next i
Or

Code: Select all

aMyArray := { 'Item1', 'Item2', 'Item3' }
Set_Array_to_ListBox( 'myform', 'mylistbox', aMyArray )

function Set_Array_to_ListBox( cFormName, cListBox, aArray )
   local i
   doMethod( cFormName, cListBox, 'DELETEALLITEMS' )
   for i := 1 to len( aArray )
      doMethod( cFormName, cListBox, 'ADDITEM', aArray[ i ] )
   next i
return nil
bluebird wrote: Mon Jan 16, 2017 8:16 pm 2) If you want to select an "item", do you have to write a function like I did to fetch the selection so that it can be
displayed elsewhere?

DEFINE LISTBOX L_Type
ROW 73
COL 70
PARENT Form_2
WIDTH 200
HEIGHT 35
VALUE 0
VISIBLE .F.
ON LOSTFOCUS GetcType()
ITEMS {"Financial","Commercial","Technical","Entertainment"}
END LISTBOX

*-----------------------------------------------------
Function GetcType()
*-----------------------------------------------------
Local nTypeIndex:=0, cTypex

form_2.L_Type.visible:=.F. //hide the listbox after using it
form_2.T_Type.visible:=.T. //show the textbox with the value over top of hidden listbox

nTypeIndex:=Form_2.L_type.value // get litbox index to "items"
cTypex:=aTypes[nTypeIndex] // find out its value from a LOCAL array that has the same values as the ListBox
form_2.T_Type.Value:=cTypex // display the selected "Item"

return
No. Need not be!

Code: Select all

Function GetcType()
form_2.L_Type.visible:=.F.   //hide the listbox after using it
form_2.T_Type.visible:=.T.  //show the textbox with the value over top of hidden listbox
form_2.T_Type.Value:=form_2.l_type.item( form2.l_type.value )        // display the selected "Item"
return
The above code is enough. All the items from a listbox can at any time be retrieved using formname.listbox.item( nItemIndex )