'===========================================================================
' Subject: List box demo                      Date: 03-16-02 (  :  )       
'  Author: Brad Moore                         Code: LB                     
'  Origin: brad.moore@weyerhaeuser.com      Packet: LIBERTY.ABC
'===========================================================================
   'Thingy II  (I didn't know what else to call it)
   'by Brad Moore - copyright 2001
   'This code is copyrighted so that I may retain my rights to my original works.
   'As the copyright owner I authorize ANY person who wants to use this code
   'to create new works or to adapt to a project they are working on - to use it
   'freely without need to contact the author or to acknowledge the author in anyway.
   '
   'Please report any errors or bugs to bjaz.moore@verizon.net
   '
   'This project will produce two listboxes.  The interface allows the user to
   'move items from the left (master list) to the right and then re-order the items
   'on the right (as well as remove them) at will.
   '
   'I created this to determine how to accomplish this task for another program I am
   'working on, as well as to demonstrate to the Liberty Basic community how this works.
   '
   'To run this you must have Liberty Basic version 2.02.  It is avaialble from
   'the web site www.libertybasic.com
   '
   '-------------------------------------------------------------------------------------
   ' Code Maintenance
   '
   ' Name       Date      Description
   ' ---------  --------  --------------------------------
   ' BJMoore    07/19/01  Created the program.
   '
   '-------------------------------------------------------------------------------------

   'Note: This code uses Data/Read statements and requires LB2.02

    Nomainwin

    WindowWidth = 552
    WindowHeight = 480

    'Set up the variables we will be using
    dim Master$(30)
    dim NewList$(200)
    dim Working$(201)
    NLIndex = 0

    'Read in the values for the Master array to be displayed in the left Listbox
    for x = 1 to 16
       read A$
       Master$(x) = A$
    next x

    'Set up the main window and open it
    listbox #Main.MasterLb, Master$(, [lMasterLBDoubleClick], 22, 71, 184, 360
    button #Main.btnAdd, "ADD >", [AddClick], UL, 214, 76, 112, 30
    button #Main.btnRemove, "< Remove", [RemoveClick], UL, 214, 111, 112, 30
    listbox #Main.NewListLB, NewList$(, [NewListLBDoubleClick], 334, 71, 184, 365
    statictext #Main.statictext5, "Master List", 22, 46, 88, 20
    statictext #Main.statictext6, "New List", 334, 46, 64, 20
    statictext #Main.statictext7, "Move items from the Master List to the New List because it is really fun...", 22, 11, 600, 20
    button #Main.btnQuit, "Quit", [QuitClick], UL, 214, 401, 112, 30
    button #Main.btnRemoveAll, "<< Remove All", [RemoveAllClick], UL, 214, 146, 112, 30
    button #Main.btnProperties, "Properties", [PropertiesClick], UL, 214, 330, 112, 30
    groupbox #Main.groupbox9, "Re-Order Items", 214, 186, 112, 135
    statictext #Main.statictext10, "MoveUp", 246, 221, 52, 20
    statictext #Main.statictext11, "MoveDown", 238, 266, 69, 20
    bmpbutton #Main.btnMoveUp, "BMP\SCRLUP.BMP", [MoveUpClick], UL, 262, 241
    bmpbutton #Main.btnMoveDown, "BMP\SCRLDOWN.BMP", [MoveDownClick], UL, 262, 286
    open "Thingy II Application" for window_nf as #Main
    print #Main, "font ms_sans_serif 0 16"
    print #Main, "trapclose [QuitClick]"



[Main.inputLoop]   'wait here for input event
    wait
    goto [Main.inputLoop]



[lMasterLBDoubleClick]   'Perform action for the listbox named 'MasterLb'

    goto [AddClick]
    'goto [Main.inputLoop]


[AddClick]   'Perform action for the button named 'btnAdd'

    'Get the user selection by index value
    print #Main.MasterLb, "selectionindex? i"
    if i < 1 then
       beep
       goto [Main.inputLoop]
    end if

    'Increment the counter that tracks total items in newlist array
    'and add the item to the end of the new list array
    NLIndex = NLIndex + 1
    NewList$(NLIndex) = Master$(i)

    'Reload the list box and select the new item we just moved into the listbox
    print #Main.NewListLB, "reload"
    print #Main.NewListLB, "selectindex ";NLIndex

    goto [Main.inputLoop]


[RemoveClick]   'Perform action for the button named 'btnRemove'

    'Get the user selection by index value
    print #Main.NewListLB, "selectionindex? Foundi"
    if Foundi < 1 then
       beep
       goto [Main.inputLoop]
    end if

    'Build a working array
    for x = 1 to NLIndex
       Working$(x) = NewList$(x)
    next x

    'If the item deleted was not the last item, move the subsequent
    'items up one position in the array
    if Foundi < NLIndex then
       for x = Foundi+1 to NLIndex
          Working$(x-1) = NewList$(x)
       next x
    end if

    'decrament our counter that tracks total items in newlist
    NLIndex = NLIndex - 1

    'Populate Newlist array from the working array
    for x = 1 to NLIndex
       NewList$(x) = Working$(x)
    next x

    'Don't forget to set the last element in newlist to blank or
    'there will be multiple copies of it in the list box
    NewList$(NLIndex+1) = ""

    'Reload the list box and select the first item
    print #Main.NewListLB, "reload"
    print #Main.NewListLB, "selectindex 1"
    goto [Main.inputLoop]


[NewListLBDoubleClick]   'Perform action for the listbox named 'NewListLB'

    'Redirect to PropertiesClick code

    goto [PropertiesClick]


[QuitClick]   'Perform action for the button named 'btnQuit'

    Close #Main
    end

    goto [Main.inputLoop]

[MoveUpClick]   'Perform action for the bmpbutton named 'btnMoveUp'

    'Get the user selection by index value
    print #Main.NewListLB, "selectionindex? Foundi"
    if Foundi <= 1 then
       beep
       goto [Main.inputLoop]
    end if

    if NLIndex <= 1 then
       beep
       goto [Main.inputLoop]
    end if

    'Build a working array
    for x = 1 to NLIndex
       Working$(x) = NewList$(x)
    next x

    'Now simply switch the locations of the select item and the item
    'before it
    Working$(Foundi - 1) = NewList$(Foundi)
    Working$(Foundi) = NewList$(Foundi - 1)

    'Populate Newlist array from the working array
    for x = 1 to NLIndex
       NewList$(x) = Working$(x)
    next x

    'Reload the list box and re-select the item we just moved
    print #Main.NewListLB, "reload"
    print #Main.NewListLB, "selectindex ";Foundi - 1

    goto [Main.inputLoop]


[MoveDownClick]   'Perform action for the bmpbutton named 'btnMoveDown'

    'Get the user selection by index value
    print #Main.NewListLB, "selectionindex? Foundi"
    if Foundi < 1 then
       beep
       goto [Main.inputLoop]
    end if

    'If the last item was selected, then you can not move it down anymore
    if Foundi = NLIndex then
       beep
       goto [Main.inputLoop]
    end if

    'No sense moving stuff around when there is only one item
    if NLIndex <= 1 then
       beep
       goto [Main.inputLoop]
    end if

    'Build a working array
    for x = 1 to NLIndex
       Working$(x) = NewList$(x)
    next x

    'Now simply switch the locations of the select item and the item
    'before it
    Working$(Foundi + 1) = NewList$(Foundi)
    Working$(Foundi) = NewList$(Foundi + 1)

    'Populate Newlist array from the working array
    for x = 1 to NLIndex
       NewList$(x) = Working$(x)
    next x

    'Reload the list box and re-select the item we just moved
    print #Main.NewListLB, "reload"
    print #Main.NewListLB, "selectindex ";Foundi + 1

    goto [Main.inputLoop]


[RemoveAllClick]   'Perform action for the button named 'btnRemoveAll'

    'No sense deleting stuff that is not there
    if NLIndex < 1 then
       beep
       goto [Main.inputLoop]
    end if

    'Verify whether the user really wants to do this...
    confirm "Are you sure you want to empty all the items from the NewList?"; answer$
    if answer$ = "no" then [Main.inputLoop]

    'Set all the elements to blank
    For x = 1 to NLIndex
       NewList$(x) = ""
    next x

    'set the index that tracks total items in newlist back to zero
    NLIndex = 0

   'Fianlly Reload the list box
    print #Main.NewListLB, "reload"

    goto [Main.inputLoop]


[PropertiesClick]   'Perform action for the button named 'btnProperties'

   'Get the user selection by index value
    print #Main.NewListLB, "selectionindex? Foundi"
    if Foundi < 1 then
       beep
       goto [Main.inputLoop]
    end if

    KeyValue$ = NewList$(Foundi)
    KVLen = Len(KeyValue$)
    KVLen$ = str$(KVLen)
    Type$ = "Color"

    if KeyValue$ = "Up" or _
       KeyValue$ = "Down" or _
       KeyValue$ = "Left" or _
       KeyValue$ = "Right" then Type$ = "Direction"

    if KeyValue$ = "New" or _
       KeyValue$ = "Start" or _
       KeyValue$ = "Old" or _
       KeyValue$ = "Spin" or _
       KeyValue$ = "Turn" then Type$ = "Command"

    if KeyValue$ = "Circle" or _
       KeyValue$ = "Square" then Type$ = "Shape"

    Index$ = str$(Foundi)

    WindowWidth = 232
    WindowHeight = 190

    button #Prop.btnCancel, "Cancel", [CancelClick], UL, 14, 121, 88, 30
    button #Prop.btnAccept, "Accept", [AcceptClick], UL, 110, 121, 96, 30
    statictext #Prop.statictext3, "Properties of item:", 14, 6, 152, 20
    statictext #Prop.statictext4, "Name:", 22, 31, 40, 20
    statictext #Prop.txtName, KeyValue$, 86, 31, 120, 20
    statictext #Prop.statictext6, "Type:", 22, 51, 40, 20
    statictext #Prop.txtType, Type$, 86, 51, 120, 20
    statictext #Prop.statictext8, "Length:", 22, 71, 56, 20
    statictext #Prop.txtLength, KVLen$, 86, 71, 128, 20
    statictext #Prop.statictext10, "Index:", 22, 91, 48, 20
    statictext #Prop.txtIndex, Index$, 86, 91, 128, 20
    open "Properties" for dialog_nf as #Prop
    print #Prop, "font ms_sans_serif 0 16"


[Prop.inputLoop]   'wait here for input event
    wait
    goto [Prop.inputLoop]



[CancelClick]   'Perform action for the button named 'btnCancel'

    close #Prop
    goto [Main.inputLoop]


[AcceptClick]   'Perform action for the button named 'btnAccept'

    close #Prop
    goto [Main.inputLoop]










'Data Area -------------------------------------------------------------

Data New, Start, Old, Turn, Right, Left, Down, Up, Spin, Black, Red, Blue, Green, Yellow, Circle, Square

