Liberty Basic is develeopped by Carl Gundel Original Newsletter compiled by Alyce Watson and Brosco Translation to HTML: Raymond Roumeas
In this issue:
1) LB Newsletter Discussion Email list
2) Understanding Arrays with Listboxes
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 - Idon'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.
"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!
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 tonull - that is - every element has a value of "". This is veryimportant 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:
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.
Comments, requests or corrections: Hit 'REPLY' now!
(but don't forget to CUT the newsletter from your reply!)
' 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" 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