1. sethandler() help please
I'm fairly new to Euphoria and am trying to get this piece of code
working. The Save button is supposed to switch between Save and Close
depending on whether anything has been changed. It will switch one way,
but not back. Prolly a dumb mistake on my part, but I'm just not seeing
it! Code follows:
include Win32lib.ew
without warning
-- Window Product_Edit
global constant Product_Edit = createEx( Window, "Edit Product List", 0,
Default, Default, 403, 412, 0, 0 )
global constant Product_Text = createEx( CText, "Product", Product_Edit,
5, 0, 110, 20, 0, 0 )
global constant QTY_Text = createEx( CText, "QTY", Product_Edit, 120, 0,
40, 20, 0, 0 )
global constant Price_Text = createEx( CText, "Price", Product_Edit,
165, 0, 70, 20, 0, 0 )
global constant Product_List = createEx( List, "", Product_Edit, 5, 20,
110, 355, or_all({WS_VSCROLL}), 0 )
global constant QTY_List = createEx( List, "", Product_Edit, 120, 20,
40, 355, 0, 0 )
global constant Price_List = createEx( List, "", Product_Edit, 165, 20,
70, 355, 0, 0 )
global constant Add_Button = createEx( PushButton, "Add", Product_Edit,
295, 40, 90, 30, 0, 0 )
global constant Edit_Button = createEx( PushButton, "Edit",
Product_Edit, 295, 90, 90, 30, 0, 0 )
global constant Delete_Button = createEx( PushButton, "Delete",
Product_Edit, 295, 140, 90, 30, 0, 0 )
global constant Cancel_Button = createEx( PushButton, "Cancel",
Product_Edit, 295, 275, 90, 30, 0, 0 )
global constant Save_Button = createEx( PushButton, "Close",
Product_Edit, 295, 325, 90, 30, 0, 0 )
-- Variables --
global integer isnew, haschanged, NeedToSave, YES, NO
global sequence hl
isnew=0
haschanged=0
YES=1
NO=0
NeedToSave=NO
procedure save_off()
setText(Save_Button, "Close")
setHandler(Save_Button, w32HClick, {-1,
routine_id("Close_Button_onClick")})
hl = getHandler(Save_Button, w32HClick)
addItem( Product_List, hl )
end procedure
procedure save_on()
setText(Save_Button, "Save")
setHandler(Save_Button, w32HClick, {-1,
routine_id("Save_Button_OnClick")})
addItem( Product_List, "Save On" )
end procedure
-- Button Procedures --
procedure Close_Button_onClick(integer self, integer event, sequence
params)
NeedToSave=NO
closeWindow(Product_Edit)
end procedure
procedure Save_Button_onClick(integer self, integer event, sequence
params)
NeedToSave=NO
save_off()
end procedure
procedure Add_Button_onClick(integer self, integer event, sequence
params)
NeedToSave=YES
save_on()
end procedure
procedure Delete_Button_onClick(integer self, integer event, sequence
params)
NeedToSave=YES
save_on()
end procedure
procedure Edit_Button_onClick(integer self, integer event, sequence
params)
NeedToSave=YES
save_on()
end procedure
procedure Cancel_Button_onClick(integer self, integer event, sequence
params)
closeWindow(Product_Edit)
end procedure
-- Program Procedures --
procedure Product_Edit_onDestroy(integer self, integer event, sequence
params)
integer junk
if haschanged=1 then
junk = message_box("You have made changes.\n" &
"Would you like to save them?",
"Product List Changed!",
MB_ICONWARNING + MB_YESNO + MB_TASKMODAL)
if junk = IDYES then
--Save_Button_onClick()
end if
end if
end procedure
-- Handlers --
--onOpen[Product_Edit] = routine_id("onLoad_Product_Edit")
--onChange[List1] = routine_id("onChange_List1")
setHandler(Add_Button, w32HClick, routine_id("Add_Button_onClick"))
setHandler(Edit_Button, w32HClick, routine_id("Edit_Button_onClick"))
setHandler(Delete_Button, w32HClick,
routine_id("Delete_Button_onClick"))
setHandler(Cancel_Button, w32HClick,
routine_id("Cancel_Button_onClick"))
setHandler(Save_Button, w32HClick, routine_id("Close_Button_onClick"))
setHandler(Product_Edit, w32HDestroy,
routine_id("Product_Edit_onDestroy"))
WinMain( Product_Edit,Normal )
2. Re: sethandler() help please
Hi,
the reason you are having trouble is that a call to routine_id must appear
(not execute) after the routine it is gettting the ID for.
By the way, this is an interesting way to do this effect. I normally do
this differently but your way is quite a novel approach that should work
fine.
Any how you need to make these few changes...
Add this to near the top of the file...
integer r_Close, r_Save
Then change the save_off/save_on routine thus...
procedure save_off()
setText(Save_Button, "Close")
setHandler(Save_Button, w32HClick, {-1,r_Close}) --<< NB
hl = getHandler(Save_Button, w32HClick)
addItem( Product_List, hl )
end procedure
procedure save_on()
setText(Save_Button, "Save")
setHandler(Save_Button, w32HClick, {-1,r_Save}) --<< NB
addItem( Product_List, "Save On" )
end procedure
and finally, just prior to the WinMain() call add these lines ...
r_Close = routine_id("Close_Button_onClick")
r_Save = routine_id("Save_Button_onClick")
This seems to work fine.
--
Derek
--
cheers,
Derek Parnell
3. Re: sethandler() help please
On Thu, 4 Sep 2003 03:28:04 +0000, David Davis <gdavis at hypercon.net>
wrote:
Derek's answer is correct. I also noticed the following line (which
Derek replaced, so it makes no difference now):
>routine_id("Save_Button_OnClick")})
..
>procedure Save_Button_onClick(integer self, integer event, sequence=20
Of course routine_id is case sensitive. I always wrap routine_id, eg:
function routineid(sequence name)
integer r
r=3Droutine_id(name)
if r=3D-1 then ?9/0 end if
return r
end function
so it fails a bit more obviously. However, you do need to make sure
this is right at the end of the code, after all the routines it will
reference and before the bank of setHandler etc calls, or, sometimes
more bizarrely, call it via r_routineid=3Droutine_id("routineid")
I hope that makes sense. Although it can be quite confusing at first,
it has made my code much more maintainable.
Regards,
Pete
PS No, I still can't think of a case where you'd pass routine_id an
unrecognised string and want it to return -1 quietly instead of
stopping with an error. Rob claims it would break existing code.