# Adding the Addressbook functions.

This addressbook example uses a variable-length database in which each field in each database e_entry is separated by a comma/space combination.
As a result, this combination of characters is the only one which you must NOT type into the entry sle's.
Since each line in the listbox is a database entry, we have to add these separaters to each line before adding the line to the listbox. This is done in the onClick_btnAdd() procedure.
These separators are then removed in the show_parsed() procedure to fill the sle's which show the individual fields.
Since our listbox is now a sorted list, we have also written the re_index() procedure, which will point to the latest entry we added, or the first.
All the new code is shown in red, as usual.

-- address2.exw

    -- a simple addressbook example, part two.

    -- set the background color
    setWindowBackColor( MyWin, rgb(0,255,127) )

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

    atom Handle               -- variable we use for file handling
    integer listlen           -- length of list
    integer index             -- index for parsing line
    object line, line2, e_entry

    procedure clear_all()
    -- clear all the sle's in preparation for new data.
      setText(Sle1,{})  setText(Sle2,{})
      setText(Sle3,{})  setText(Sle4,{})
      setText(Sle5,{})  setText(Sle6,{})
      setText(Sle7,{})  setText(Sle8,{})
    end procedure

    procedure onClick_btnNew( integer self, integer event, sequence params )
      setFocus(Sle1)            -- point to first e_entry field
      clear_all()               -- and clear them all.
    end procedure

    function lTrim(sequence string)
      -- returns line segment up to comma/space combo.
      for i=1 to length(string) do
        if string[i] = ',' then
          if string[i+1] = ' ' then return string[1..i-1] end if
        end if
      end for
      return string
    end function

    procedure get_chunk()
      index = length( line2 )+3       -- skip the comma/space separator.
      if index < length( line) then   -- are we all done ?
        Line = line[index..length( line )]
        line2 = lTrim( line )
      else
        line2 = " "
      end if
    end procedure

    procedure show_parsed()
    -- removing the comma and space in the listbox line
    -- for each item and inserting the data in the
    -- appropriate single-line-e_entry fields.
      clear_all()
      index=1
      line2=lTrim(line)
      setText(Sle1,line2)
      get_chunk()
      setText(Sle2,line2)
      get_chunk()
      setText(Sle3,line2)
      get_chunk()
      setText(Sle4,line2)
      get_chunk()
      setText(Sle5,line2)
      get_chunk()
      setText(Sle6,line2)
      get_chunk()
      setText(Sle7,line2)
      get_chunk()
      setText(Sle8,line2)
    -- names will be shown in the static line in reverse order.
      setText(Label1, getText(Sle2) & " " & getText(Sle1))
    end procedure

    procedure onLoad_MyWin( integer self, integer event, sequence params )
    -- read from a file, IF it exists.
      Handle = open("mydata3.txt", "r")     -- open the file,
      if Handle != -1 then                  -- if file successfully opened,
        while 1 do                          -- get each line,
            line = gets(Handle)
            if atom(line) then              -- until end of file.
                exit
            end if
            line = line[1..length(line)-1]  -- remove line-feed we add below,
            addItem(List1, line)            -- and "park" it in the listbox.
        end while
        close(Handle)
      end if
      -- check to make sure the list is not empty
      listlen = getCount(List1)             -- get the lists length
      if listlen > 0 then
          setIndex(List1, 1)                -- point to the first item
          line = getItem(List1,1)           -- get the first record
          show_parsed()                     -- show it in the sle's
      end if
      setFocus(List1)                       -- and 'hi-lite' the first record
    end procedure

    procedure onChange_List1( integer self, integer event, sequence params )
    -- update the sle's as we scroll through the sorted list
    integer what
        what = getIndex(List1)
        if what >= 1 then
          line=getItem(List1, what)
          show_parsed()
        end if
    end procedure

    procedure re_index()
    -- to point to the latest entry in the listbox,
    -- do a sequential comparison of each 'line' in the listbox with the
    -- latest 'entry' line until we get a match.
      listlen = getCount(List1)         -- how many lines are in it ?
      if listlen > 0 then
          for i = 1 to listlen do
              line = getItem( List1, i )
                if compare( line, e_entry ) = 0 then
                  setIndex( List1, i )   -- the lines match!
                end if
          end for
      end if
    setFocus(List1)
    end procedure

    procedure onClick_btnAdd( integer self, integer event, sequence params )
    -- add up all the sle fields to build a line for the listbox,
    -- separating each with a comma/space combo.
    integer junk
      e_entry = getText(Sle1) & ", " & getText(Sle2) & ", " & getText(Sle3)
      e_entry=e_entry & ", " & getText(Sle4) & ", " & getText(Sle5) & ", "
      e_entry=e_entry & getText(Sle6) & ", " & getText(Sle7) & ", "
      e_entry=e_entry & getText(Sle8) & ", "
      if length(e_entry) = 16 then  -- is the line really 'empty' ?
          junk = message_box("Type some text in first!",
                             "Empty entry!", MB_TASKMODAL + MB_ICONWARNING)
      else
          setFocus(List1)
          addItem(List1, e_entry)
          setText(Label1, getText(Sle2) & " " & getText(Sle1))
          re_index() -- routine to point to new item in sorted list
      end if
    end procedure

    -- ** procedures to save and delete will be added here
    -- ** in the next lesson.

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

    -- ** add the following three lines to the event handlers.
    setHandler( MyWin, w32HOpen, routine_id("onLoad_MyWin") )
    setHandler( List1, w32HChange, routine_id("onChange_List1") )
    setHandler( btnNew, w32HClick, routine_id("onClick_btnNew") )

    -- ** etcetera.

This program, up to now, will only allow you to clear the sle's and add records to the sorted listbox.

..end of lesson.

CONTENTS