Example
The following is a typical example of a magic pushbutton in Borland Delphi:
procedure TForm1.Button1Click(Sender: TObject); var reg: TRegistry; begin reg := TRegistry.Create; try reg.RootKey := HKey_Current_User; if reg.OpenKey('\Software\MyCompany', true) then begin reg.WriteString('Filename', Edit1.Text); end; finally reg.Free; end; end;A better way to do this is to refactor the business logic (in this example storing the filename to the registry) into a separate class.
type TPreferences = class private FFilename: String; procedure SetFilename(const Value: String); public property Filename: String read FFilename write SetFilename; procedure Load; procedure Save; end;and call this class Save method from the Click handler:
procedure TForm1.Button1Click(Sender: TObject); begin Preferences.Save; end; procedure TForm1.Edit1Change(Sender: TObject); begin Preferences.Filename := Edit1.Text; end;Read more about this topic: Magic Pushbutton
Famous quotes containing the word example:
“Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.”
—Marcel Proust (18711922)