Arrays and file dialogs

Perhaps these may help a bit, Kurt? Don't know what kind of things you've experimented with so far... Just copy the code sections into LB and run them.

Arrays

A 1D array is basically just a list- of either numbers or of strings. It takes only ONE number to specify which term you are interested in. Basically like a row of pigeonholes for keeping things in.

'apples, pears, bananas' is a list of string entries- we'd say fruit( 1) ="apples", fruit( 2) ="pears", etc, and then you can-
search all the fruit( ) array for a particular entry ( ?Is "pineapple" in any of the fruit( ) values the program knows?), or
look up one ( ?What is the third fruit, ie fruit( 3)- answer, "bananas").

'23,39,666' is a 1D numeric array, and used similarly.

One useful application is making databases, and the ability to sort entries numerically or alphanumerically.

See also my relevant webpage about 2D & 3D data.

2D arrays

These need two numbers to specify which you are referring- like several rows of pigeonholes, one above the other. This is exactly what a spreadsheet uses. You can put a value into, or read a value back from, a place that is three rows up and 5 places along. You'd call this 'pigeonHole( 3, 5)'.

In this first example I fill a 2d array with random numbers, and then display them on a 2D grid. Each number stored is used to represent how much red, green and blue to start with. ( 255 =max of a color, 0 min. If RG and B are all 255 -->white. If all 0, -->black.

I then look over and over at a random one on the grid, and increase its Red and Blue parts, and re-display it. I need to keep the values in a 2D array so I can look up the present value and replace it with a new, calculated value.

_________________________________________________________

    nomainwin

    dim location( 100, 100)

    WindowWidth  =520
    WindowHeight =560

    graphicbox #w.gb, 5, 5, 500, 500

    for y =0 to 99
        for x =0 to 100
            location( x, y) =256 *rnd( 1)
        next x
    next y

    open "Kurt2DarrayD" for window as #w

    #w "trapclose [quit]"
    #w.gb "down ; size 5"

    for y =0 to 99
        for x =0 to 100
            g =location( x, y)
            #w.gb "color "; g; " "; g; " "; g
            #w.gb "set "; 5 +x *5; " "; 5 +y *5
        next x
    next y

    #w.gb "flush"
    '#w.gb "getbmp scr 0 0 500 500"
    'bmpsave "scr", "initialRnd.bmp"

    for i =1 to 10000
        x =int( 100 *rnd( 1))
        y =int( 100 *rnd( 1))
        g =location( x, y)  '   ie present value of this term
        g =min( g +10, 255) '   ie increase by 10, but don't allow to exceed 255
        location( x, y) =g  '   put back new value. Will eventually be 255, and colour magenta.
        #w.gb "color "; g; " "; 255 -g; " "; g
        #w.gb "set "; 5 +x *5; " "; 5 +y *5
    next i

    #w.gb "flush"
    '#w.gb "getbmp scr 0 0 500 500"
    'bmpsave "scr", "finalRnd.bmp"

    wait

[quit]
    close #w
    end

-


The second example is laid out to show how it is very like a spreadsheet being used as a database.

' __________________________________________________________ ' ***** Kurt2DarrayB2.bas ***** dim customerInfo$( 10, 5) ' This creates a grid of empty cells, like a spreadsheet, 5 wide and 10 vertically ' which are now filled in. for index = 0 to 9 ' computer starts at position zero by convention read a$: customerInfo$( index, 0) =a$ read a$: customerInfo$( index, 1) =a$ read a$: customerInfo$( index, 2) =a$ read a$: customerInfo$( index, 3) =a$ read a$: customerInfo$( index, 4) =a$ next index input "Give me the customer's index number ( 0 to 10) >"; num print "This is a customer called "; customerInfo$( num, 0) input "Give me a postcode >"; postcode$ found =0 for index =0 to 9 if customerInfo$( index, 4) =postcode$ then print customerInfo$( index, 0); " lives at "; postcode$ found =1 end if next index if found =0 then print "Sorry- no customer for that postcode" end data "John", "Chaces", "Kingston St Mary", "Somerset", "TA2 8HW" data "Jenn", "Chaces", "Kingston St Mary", "Somerset", "TA2 8HW" data "Jean", "66 North Street", "Taunton", "Somerset", "TA3 9HW" data "Fred", "137, The Parade", "Wellington", "Somerset", "TA3 8HH" data "Kurt", "The Ritz", "London", "UK", "SH2 3ZZ" data "The Queen", "Buck Palace", "Pall Mall", "London", "xxx 666" data "Carl", "Somewhere", "Connecticut", "Somerset", "whoKnows" data "Alyce", "Mon Repos", "Maryland", "Somerset", "ZipZip" data "Stephan", "Berchtesgarten", "Bavaria", "Austria", "Bav456" data "Stephanie", "Blue Lagoon", "The Bronx", "NEW York", "pq234"

Gives typical output..
   Give me the customer's index number ( 0 to 10) >6
   This is a customer called Carl
   Give me a postcode >ZipZip
   Alyce lives at ZipZip


Font dialog and specification

It is common to want to specify at run time what font you want- its name, size, style, color. You can do this with the supplied 'fontdialog, widget, or provide your own way of entering it at run time. You can use a dropdown menu; pop up a filedialog; or get values from textboxes.

Here's an example using the inbuilt 'filedialog'.

'	*****  kurtFD1.bas  *****


    nomainwin

    WindowWidth  =504
    WindowHeight =540

    graphicbox #w.gb,  10, 40, 480, 400
    button     #w.bu, "Click Me!", [textSpecification], LR, 100, 20

    open "Fontdialog demo" for window as #w

    #w "trapclose [quit]"

    #w.gb "down ; fill 90 90 90 ; flush ; backcolor 90 90 90"
    '   Fill background with gray and select same gray for text's background color

    wait

[quit]
    close #w
    end

[textSpecification]'    get a font spec. Print text in random color and place...
    fontdialog "Arial bold 18", fontSpec$
    #w.gb "font "; fontSpec$
    #w.gb "color "; 256 *rnd(1); " "; 256 *rnd(1); " "; 256 *rnd(1)
    #w.gb "up ; goto "; 300 *rnd( 1); " "; 360 *rnd( 1)
    #w.gb "down"
    #w.gb "\Kurt E"
    wait
And here's an example using a user-defined way to select fonts at run-time, getting values from textboxes.

    nomainwin

    WindowWidth  =504
    WindowHeight =600

    graphicbox #w.gb,  10, 140, 480, 380

    statictext #w.st1, "", 10,  10, 110, 26
    statictext #w.st2, "", 10,  40, 110, 26
    statictext #w.st3, "", 10,  70, 110, 26
    statictext #w.st4, "", 10, 100, 110, 26

    textbox    #w.tb1, 120,   2, 280,  28
    textbox    #w.tb2, 120,  32, 280,  28
    textbox    #w.tb3, 120,  62, 280,  28
    textbox    #w.tb4, 120,  92, 280,  28

    button     #w.bu1, "Draw me!", [textSpecification], LR, 50, 20

    open "Fontdialog demo" for window as #w

    #w "trapclose [quit]"

    #w.gb "down ; fill 90 90 90 ; flush ; backcolor 90 90 90"
    '   Fill background with gray and select same gray for text's background color

    #w.st1 "Text     required"
    #w.st2 "Font     required"
    #w.st3 "Position required"
    #w.st4 "Color    required"

    #w.tb1 "Kurt Eifert"
    #w.tb2, "Arial bold italic 28"
    #w.tb3, "20 240"
    #w.tb4 "cyan" ' or eg "240 130 60"

    wait

[quit]
    close #w
    end

[textSpecification] '   expects "text to print", "color specification", "position", "font"
    '   eg Type in the box "Kurt Eifert","180 80 80", "20 140", "Arial 32 bold"
    #w.tb1  "!contents? text$"
    #w.tb2  "!contents? font$"
    #w.tb3  "!contents? posn$"
    #w.tb4  "!contents? color$"

    #w.gb   "font ";      font$
    #w.gb   "color ";     color$
    #w.gb   "\";          text$
    #w.gb   "up ; goto "; posn$; " ; down"
    #w.gb   "flush"
    wait


' ________________________________________________________________