..and lazier still ! ..without being too lackadaisical.
This time around, we are going to add yet another button, and some file handling procedures, so we can actually save all the data we so diligently place into the listbox. So lets get right to it .....
--lists3.exw
-- ...then added a button and an SLE to "fill" the listbox.
-- Now MOVE and RE_SIZE the silly button to add another,
-- just to add a new feature.
include win32lib.ew
constant
Win = create( Window, "List +++", 0, Default, Default, 160, 190, 0 ),
List1 = create( List, "", Win, 10, 10, 120, 58, LBS_NOINTEGRALHEIGHT ),
Label1 = create(RText, "", Win, 10, 75, 120, 20, 0),
Sle1 = create( EditText, "", Win, 10, 96, 110, 20, 0 ),
Button1 = create( PushButton, "ADD", Win, 10, 126, 60, 20, 0 ),
Button2 = create( PushButton, "SAVE", Win, 75, 126, 60, 20, 0 )
-- ^^ here's our new SAVE button.
atom Handle -- declare 2 variables we use for file handling.
object line
-- the following 2 lines check to see if we have a file,
-- and creates that file if it does not exist,
-- or our program will crash when it tries to "read" from nothing !
-- there ARE better ways to handle this, illustrated later.
Handle = open( "mydata.txt", "a" ) -- mydata.txt is our "human" readable file.
close( Handle )
procedure onLoad_Win( integer self, integer event, sequence params )
-- add items to the listbox, from a file, this time !
addItem(List1, " " ) -- better do this, or setIndex() crashes
-- with an empty list.
-- this is again, a temporary solution.
Handle = open( "mydata.txt", "r" ) -- open the file,
while 1 do -- and get each line,
line=gets(Handle)
if atom(line) then exit end if -- until end of file.
line = line[1..length(line)-1] -- remove the line-feed we add below,
addItem( List1, line ) -- and "park" it in the listbox.
end while
close( Handle )
setIndex(List1, getCount(List1)) --point to the last item.
setFocus( Sle1 )
end procedure
procedure show_it(integer here)
sequence this
this = getItem( List1, here )
setText( Label1, this )
end procedure
procedure onChange_List1( integer self, integer event, sequence params )
atom where
where=getIndex(List1)
show_it(where)
end procedure
procedure onClick_Button1( integer self, integer event, sequence params )
sequence list_entry list_entry = getText( Sle1 )
if length(list_entry) > 0 then addItem( List1, list_entry ) end if
-- at Ad's insistence, I have shown the new line referred to earlier..
-- to prevent the addition of empty list items.
SetText( Sle1, {} ) -- clear the text entry window
setFocus( Sle1 ) -- move our 'focus' to the entry window
end procedure
procedure onClick_Button2( integer self, integer event, sequence params )
-- here's our new feature to save our simple database to a file.
integer listlen
listlen = getCount( List1 ) -- first, how many lines are in it ?
Handle = open( "mydata.txt","w")
for i = 2 to listlen do -- note we don't save
-- the first 'dummy' line.
line = getItem( List1, i ) & {10} -- add a linefeed, so we can read it.
puts( Handle,line ) -- write the line to the file.
end for
close( Handle ) -- it's very important to close anything we've opened.
end procedure
setHandler( Win, w32HOpen, routine_id( "onLoad_Win" ) )
setHandler( List1, w32HChange, routine_id("onChange_List") )
setHandler( Button1, w32HClick, routine_id("onClick_Button1") )
setHandler( Button2, w32HClick, routine_id("onClick_Button2") )
-- ^^ our latest event handler.
WinMain( Win,Normal )
-- end --
I hope the last three examples answer one
of the questions addressed on the Euphoria mailing list recently.
(..how do you go about
developing a large program ? )
One of the simple answers given was, step by step, just as this
program was created in the last three steps.
One caution to keep
in mind, however, is that some planning before you start will save
you a lot of work as you go along. Notice that I had to essentially
design my window three times, just to "fit" in new items as
I went along. How much easier it would have been, if I'd laid out the
whole window before I started. All that pixel counting isn't much fun
at all ;-)
..end of lesson.