Mega Code Archive

 
Categories / Delphi / System
 

Easy (none temp var required) way of discovering changes

Title: easy (none temp var required) way of discovering changes Question: Is there an easy way to know if a form's textboxes has been modified ? Answer: When writing normal UI-applications you often want to know weather the text-controls on the form has been changed or not since the form showed up in front of the user... a very simple way is to use two recursive functions (like the onec below) for this purpose. They will loop thru the entire form and check if any TEdit-kind of VCL has been modified! What you need to do i to call Modify(myForm, false); from the from's OnShow-event. This will set all TEdits as NOT modified but as fast as the user changes one it will automatically set modified to true. In your forms OnClose-event you attach the "if IsModified(myForm) then ..." code (normally you then wish to save the data, or perhaps do the ms-thingy and ask the user a couple of times wheater he really want to loose the changes or not =).... This solution wont handle any other standard VCL's then the once _below_ TEdit (TMemo, TRichEdit..) so you must still figure out a way keep track on your CheckBox'es etc but atleast this will make it a little bit easier =) -------------------------------------------------------------------- function IsModified(Parent: TWinControl) : Boolean; var i : integer; begin for i := 0 to Parent.ControlCount-1 do if (Parent.Controls[i] is TEdit) then begin if TEdit(Parent.Controls[i]).Modified then begin result := true; exit; end; end else if Parent.Controls[i] is TWinControl then begin result := IsModified(TWinControl(Parent.Controls[i])); if result then exit; end; result := false; end; procedure Modify(Parent: TWinControl; Value: boolean); var i : integer; begin for i := 0 to Parent.ControlCount-1 do if Parent.Controls[i] is TEdit then TEdit(Parent.Controls[i]).Modified := Value else if Parent.Controls[i] is TWinControl then Modify(TWinControl(Parent.Controls[i]), value); end;