The Liberty Basic Newsletter - Issue #18 - Oct 98
  "The only stupid question is the one you don't ask"!

In this issue:
            1) LB Newsletter Discussion Email list
            2) Understanding Arrays with Listboxes

            ***************************************

1) LB Newsletter Discussion Email list

My request for feedback on this issue has resulted in an over-
whelming "Yes - Option 1 sounds great".  I thank all the people

who took the time to respond.  And although I called this
"Brosco's LB Newsletter" - it belongs to all LBers - it is YOUR
newsletter - so if the Email list is to be of any use - YOU need
to use it.  By the way - you may have noticed a name change at
the top of this posting.  The new name is:

    "The Liberty Basic Newsletter"

This means that YOU should try to contribute to it.  It is no
longer Brosco's - it belongs to everybody! You can contribute
by:

a)  Writing an article (Alyce and I will 'clean it up' if necessary)
b)  Commenting and discussing Newsletter topics. (Yep - this will be
    just as important as the newsletter content itself)

When you read a newsletter and don't fully understand something -
or you think something is wrong (Yes - I make mistakes; No - I
don't know it all) hit the 'reply' button and comment on it.  If you
see a mistake - 'REPLY' immediately - you will be doing everyone
a favour.  If you have additional information that would supplement

the article - SHARE it!  Don't be a 'cyber couch potato' and leave
it for someone else.

And if you only installed your copy of LB 'yesterday' - your
comments/questions are just as valuable as those from the old-hands.
Although some the topics in the newsletter get a little heavy - the
majority of the content is aimed at helping the new programmers - so
if, as a new LBer you don't follow something - please say so.

We are very lucky in the LB community.  It is the friendliest group

that I have ever found on the net.  They don't laugh at so-called
'stupid questions'.  In fact - I believe it was Garrett (co-host of
the LB Network site) that coined the phrase:

  "The only stupid question is the one you don't ask"!

(This is now the slogan for the newsletter - thanks Garrett)

Some guidelines on using the Email list.

1)  There are other lists and messages boards for posting general
questions - PLEASE restrict any discussion on this list to

Newsletter content.  Of course, this is not restricted to the
latest issue - discussion on previous issues is welcomed.

2)  On most Email readers - when you hit 'reply' it copies the
entire copy of the original posting into your reply.  PLEASE
don't post your reply with all this.  Use the 'Edit' features
to remove it.  Obviously - if your posting is regarding part
of the content - leave that part in - but remove the rest.  Lets
face it - everyone on the list has a copy of the newsletter -

they don't need to see yet another copy in its entirety in your
reply.

3)  Types of postings that will be welcomed (in no particular order):

    a)  Requests for further explanation
    b)  Highlighting errors in newsletters
    c)  Requests for articles on particular topics
    d)  Articles that you have written (Please send these to Alyce or me)
    e)  Supplementary information on Newsletter articles
    f)  Descriptions of how you have used the content
    etc.

4)  Finally, something from my personal wish-list.  I believe
that the Newsletter will have met its objectives when I see a
posting something like this:


"I have read the newsletter article on .......... but I used
the techniques in a slightly different way.  What I did was ....
.........  I hope this is helpful to others."


This will be an indication that the newsletter is a REAL success!

******************************************************

2) Understanding Arrays with Listboxes

Liberty BASIC takes care of a lot of the details of managing
Listboxes (and Comboboxes) for us - but it can be very helpful
to understand how they interact with the Array containing the
items in the list.

The array containing the items in the list must be a SINGLE
Dimension array of STRINGS.  It is best to DIMension the array
early in your program:

    dim Array$(100)     ' Allow a few extra items than necessary

When you DIM or REDIM an array - the entire contents are set to

null - that is - every element has a value of "".  This is very
important to remember - you'll see why shortly.

The array above actually has 101 elements NOT 100, as the valid
array elements are: Array$(0), Array$(1), Array$(2) .... Array$(100).

When you initialise the array - do NOT use Array$(0) if you will be
using it in conjuction with a LISTBOX or COMBOBOX.  This will
also be explained later.

First we may give the Array some initial values - either by hard

coding them in the program - or loading them from a text file:

    Array$(1) = "Apple"
    Array$(2) = "Orange"
    Array$(3) = "Pineapple"
    Array$(4) = "Watermelon"
    Array$(5) = "Mandarine"
    Array$(6) = "Pawpaw"
    Array$(7) = "Kiwi Fruit"

It will also be handy later on to know how many items are in the array:

    numA = 7

Now we define the list box:

    listbox #w.lb, Array$(, [lb.select], 10, 10, 120, 120

and then open the window:

    Open "Listbox Demo" for Graphics_nsb as #w

When this window is open it will show the contents of the array in
the ListBox.

Important points:

a)  All array elements with a null value ("") are ignored.  Since we
didn't put a value in Array$(0), the first item in the list will be
Array$(1) - "Apple".

b)  All item from Array$(8) to Array$(100) are ignored - as far as
the list box is concerned - they don't exist!

c) If one of the items - say Array$(5) was empty - it would also be
ignored - AND the 5th item (as far as the Listbox is concerned)

would be Array$(6)!!!!!!

d)  The Listbox 'numbers' the items starting at 1.  So by not using
Array$(0) and by having no empty elements within our list - the
Listbox number for an item is identical to the number we use as a
subscript for the array.  READ this point again if you didn't
understand it - its the single most crucial point in maintaining the
array and keeping it in sync with the listbox.


Suppose we also had included a Button on our window to allow us

to delete items in the list:

    Button #w.del, "Delete Selected", [lb.del], UL, 140, 90, 140, 25

Here's the code you would need to delete an item:

[lb.del]
    print #w.lb, "selectionindex?"
    input #w.lb, index
    if index = 0 then
        Notice "Nothing has been selected!"
        goto [loop]
        end if
    for i = index to numA
        Array$(i) = Array$(i+1)
        next i
    print #w.lb, "reload"
    numA = numA - 1
    goto [loop]

First we get the index of the selected item:

    print #w.lb, "selectionindex?"
    input #w.lb, index

If no item has been selected the above code will return 0.

    if index = 0 then
        Notice "Nothing has been selected!"
        goto [loop]
        end if

We can now safely assume that a valid item has been selected -
so we must delete it from our array:

    for i = index to numA
        Array$(i) = Array$(i+1)
        next i

Suppose the item selected was #5 and that at this stage we still

only had 7 elements in the array - the above code would set:

    Array$(5) = Array$(6)   ' Pawpaw
    Array$(6) = Array$(7)   ' Kiwi Fruit
    Array$(7) = Array$(8)   ' ""

That last item is important - otherwise the Listbox would show items
6 and 7 as both being 'Kiwi Fruit'.  Thats why I suggest that the
array should always be slightly bigger than required - it ensures
that there is a null item at the top of the list to use when deleting
an entry.

We know must RELOAD the listbox:

    print #w.lb, "reload"

and we should also correct our variable that holds the count of
the number of items in tha array:

    numA = numA - 1


And similarly we may want to include code to ADD items to our
listbox.  First we define a textbox for enterring the new item
and a Button to activate the Addition:

    textbox #w.tb, 140, 15, 140, 25
    button #w.add, "Add Item", [lb.add], UL, 140, 45, 140, 25

And the code that gets activated when the user clicks the

"Add Item" button is:

[lb.add]
    print #w.tb, "!contents?"
    input #w.tb, new$
    if new$ = "" then
        notice "You must enter a new item first!"
        goto [loop]
        end if
    numA = numA + 1
    Array$(numA) = new$
    print #w.lb, "reload"
    print #w.tb, ""
    goto [loop]


If the list is to be maintained in a sorted order, we should also
sort the array before issuing the Reload command:

    sort Array$(, 1, numA

That's all there is to it.  Just remember two simple rules:

1)  Listbox items are numberred starting at 1.

2)  Null entries in the array are ignored - thus causing Listbox
indexes to mismatch the Array subscript numbers.

The full code for the demo program follows below.

--------------------------------------------------------------
 Newsletter written by: Brosco.
 Comments, requests or corrections mailto:brosco@orac.net.au

 Translated from Australian to English by an American:
 Alyce Watson -  Chief Editor.  Thanks Alyce.

--------------------------------------------------------------


' Demo of Arrays and Listboxes
' The LB Newsletter - Oct 98
' written by: Brosco
'
    nomainwin
    WindowWidth = 300
    WindowHeight = 180

    dim Array$(100)

    Array$(1) = "Apple"
    Array$(2) = "Orange"
    Array$(3) = "Pineapple"
    Array$(4) = "Watermelon"
    Array$(5) = "Mandarine"
    Array$(6) = "Pawpaw"
    Array$(7) = "Kiwi Fruit"
    numA = 7

    listbox #w.lb, Array$(, [lb.select], 10, 10, 120, 120

    textbox #w.tb, 140, 15, 140, 25
    button #w.add, "Add Item", [lb.add], UL, 140, 45, 140, 25
    Button #w.del, "Delete Selected", [lb.del], UL, 140, 90, 140, 25

    Open "Listbox Demo" for Graphics_nsb as #w
    print #w, "trapclose [close.w]"

    print #w, "down; color black; backcolor lightgray"
    print #w, "place 135 10; boxfilled 285 75"

[loop]
    input var$

[lb.select]
    print #w.lb, "selectionindex?"
    input #w.lb, index
    notice "You selected item# ";index

    goto [loop]

[lb.add]
    print #w.tb, "!contents?"
    input #w.tb, new$
    if new$ = "" then
        notice "You must enter a new item first!"
        goto [loop]
        end if
    numA = numA + 1
    Array$(numA) = new$
    print #w.lb, "reload"
    print #w.tb, ""
    goto [loop]

[lb.del]
    print #w.lb, "selectionindex?"
    input #w.lb, index
    if index = 0 then
        Notice "Nothing has been selected!"
        goto [loop]
        end if

    for i = index to numA
        Array$(i) = Array$(i+1)
        next i
    print #w.lb, "reload"
    numA = numA - 1
    goto [loop]

[close.w]
    close #w
    end