Adding SAVE and DELETE functions.

-- address3.exw

-- a simple addressbook example, part three.

-- ** the end of our previous onClick_btnAdd procedure.

      addItem(List1, entry)
      setText(Label1, getText(Sle2) & " " & getText(Sle1))
      re_index() -- routine to point to new item in sorted list
end if
end procedure

-- ** we add our new code starting here !

procedure onClick_btnSave( integer self, integer event, sequence params )
-- save database to a file... nothing new here, it's the same as lesson 10.
  listlen = getCount(List1)             -- first, how many lines are in it ?
  if listlen > 0 then                   -- no use to save an empty database.
      Handle = open("mydata3.txt", "w") -- open the file for write.
      for i = 1 to listlen do           -- go through the list,
          line = getItem(List1,i)       -- getting each line,
          line = line & '\n'            -- add a linefeed, so we can read it.
          puts(Handle, line)            -- write the line to the file.
      end for
      close(Handle)        
  end if
end procedure

The following delete routine is based on the previous example in lesson ten, with one new addition. When a database record is deleted, it will always point to the PREVIOUS record in the listbox, if there is one, and update the information in the sle's accordingly.

procedure onClick_btnDel( integer self, integer event, sequence params )
integer itemnum, junk
  if getCount(List1) > 0 then  -- skip routine if list is empty.
    itemnum = getIndex(List1)

-- get confirmation of delete request, by displaying
-- the data in a message box.
    line = getItem(List1, itemnum)
    junk = message_box("Are you sure you want to DELETE\n" & line,
                       "Confirm Delete of Item!",
                       MB_ICONQUESTION + MB_YESNO + MB_TASKMODAL)
    if junk = IDYES then
      junk = deleteItem(List1, itemnum)
      listlen = getCount(List1)
      if listlen > 0 then             -- *IF* we have any data records,
        if itemnum > 1 then           -- AND there are more than one,
          setIndex(List1, itemnum-1)  -- get the PREVIOUS record.
        end if
      else                            -- if not, then 'blank' the 
        setText(Label1, {})           -- static display line.
      end if
      if listlen > 0 then               -- *IF* we have any data records,
        if itemnum > 1 then             -- AND there are more than one,
          line=getItem(List1,itemnum-1) -- get the previous item,
        else line=getItem(List1,1)      -- or the first,
          setIndex(List1,1)             -- and point to it. 
        end if
      end if
    end if
  setFocus(List1)  -- in any case, set the focus to our listbox,
  show_parsed()    -- and show the information.
  end if
end procedure

procedure onMenu_Exit( integer self, integer event, sequence params )
    closeWindow(MyWin)
end procedure

-- ** etcetera.

..end of lesson.

CONTENTS