How to convert a String to Tnotifyevent in Delphi?

Asked

Viewed 766 times

3

I want to change the Ontimer event from a Ttimer in my project at runtime, I tried as follows: Timer1.OnTimer:= ('close'); But Delphi reports this: (E2010 Incompatible types: 'TNotifyEvent' and 'string'), how can I convert String 'Close' for TNotifyEvent?

3 answers

3

You should send a Procedure to Ontimer, not a String. If your intention with String 'Close' is to close the Form is simple, create a Procedure with type (Sender: Tobject) with the Close command:

procedure FecharFormulario(Sender: TObject);
begin
  Close;
end;

For the Ontimer Event you do:

Timer1.OnTimer := FecharFormulario;
  • And if I want to give the event function through parameters set by an INI file?

  • 1

    Then we’re back to square one, where you shouldn’t send String to the Event!

3


To use a file. INI; First create a file of type . ini with the example content:

[P_LOG]

command = test

Function to read file . ini:

function TForm1.LeIni(sIndice, sCampo: string): string;
var
  ArqIni: TIniFile;
begin
  try
    Result := '';
    ArqIni := TIniFile.Create(ExtractFilePath(Application.ExeName) +'Teste.ini');
    try
    Result := Trim(ArqIni.ReadString(sIndice, sCampo, ''));
    finally
    ArqIni.Free;
    end;
  except
    ShowMessage
    ('Não foi possível encontrar o arquivo de Parâmetros .ini');
  end;

end;

Using the function

procedure TForm1.Button3Click(Sender: TObject);
 var
  ConteudoArquivoINI :String;
begin
 ConteudoArquivoINI := LeIni('P_LOG', 'comando')) the
end;

0

You can do something like create a record to store the name of each procedure:

type mrec = record nome: string; proc: procedure(Sender: TObject) of Object; end; ... var s : string; procs: array of mrec; begin Setlength(procs, 2); procs[0].nome := 'Button8Click'; procs[0].proc := Button8Click; procs[1].nome := 'Button6Click'; procs[1].proc := Button6Click; end; ... procedure ExecutaPeloNome(nome: string); var i: integer; begin for i := 0 to length(procs) - 1 do if procs[i].nome = nome then begin procs[i].proc(nil); break; end; end; ... ExecutaPeloNome('Button8Click');

Honestly, I don’t think it’s worth it!

  • that line: proc: Procedure(Sender: Tobject) of Object; can be: proc: Tnotifyevent;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.