Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#8

AW: Auf Änderungen in TEdit reagieren

  Alt 19. Nov 2014, 21:00
Hier mal ein kleines simples Beispiel, wie man auf sowas reagieren kann und das auch sehr elegant (und vor allem auch testbar) löst. (fertige EXE im Anhang)

Die Logik kommt in eine eigene überschaubare Klasse
Delphi-Quellcode:
unit Rechner;

interface

type
  TRechner = class
  private
    FWert1: string;
    FWert2: string;
    FWert3: string;
    FWert4: string;
    FWert3Berechnen: Boolean;
    function GetErgebnis1: string;
    function GetErgebnis2: string;
    procedure SetWert1( const Value: string );
    procedure SetWert2( const Value: string );
    procedure SetWert3( const Value: string );
    procedure SetWert4( const Value: string );
    function GetWert3: string;
  public
    property Wert1: string read FWert1 write SetWert1;
    property Wert2: string read FWert2 write SetWert2;
    property Ergebnis1: string read GetErgebnis1;
    // Berechnet aus Wert1 und Wert2 oder freie Eingabe
    property Wert3: string read GetWert3 write SetWert3;
    property Wert3Berechnet: Boolean read FWert3Berechnen;
    property Wert4: string read FWert4 write SetWert4;
    property Ergebnis2: string read GetErgebnis2;
  end;

implementation

uses
  System.SysUtils;

{ TRechner }

function TRechner.GetErgebnis1: string;
var
  Val1, Val2: Integer;
begin
  if TryStrToInt( FWert1, Val1 ) and TryStrToInt( FWert2, Val2 )
  then
    Result := IntToStr( Val1 + Val2 )
  else
    Result := '';
end;

function TRechner.GetErgebnis2: string;
var
  Val1, Val2: Integer;
begin
  if TryStrToInt( FWert3, Val1 ) and TryStrToInt( FWert4, Val2 )
  then
    Result := IntToStr( Val1 + Val2 )
  else
    Result := '';
end;

function TRechner.GetWert3: string;
begin
  if FWert3Berechnen
  then
    begin
      FWert3 := GetErgebnis1;
    end;
  Result := FWert3;
end;

procedure TRechner.SetWert1( const Value: string );
begin
  if FWert1 <> Value
  then
    begin
      FWert1 := Value;
      FWert3Berechnen := True;
    end;
end;

procedure TRechner.SetWert2( const Value: string );
begin
  if FWert2 <> Value
  then
    begin
      FWert2 := Value;
      FWert3Berechnen := True;
    end;
end;

procedure TRechner.SetWert3( const Value: string );
begin
  if FWert3 <> Value
  then
    begin
      FWert3 := Value;
      FWert3Berechnen := False;
    end;
end;

procedure TRechner.SetWert4( const Value: string );
begin
  if FWert4 <> Value
  then
    begin
      FWert4 := Value;
    end;
end;

end.
Was hat die Form noch zu tun? Fast nix:
Delphi-Quellcode:
unit Form.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,

  Rechner;

type
  TForm1 = class( TForm )
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormShow( Sender: TObject );
  private
    FIsLoading: Boolean;
    FRechner: TRechner;
    procedure SaveToRechner;
    procedure LoadFromRechner;
    procedure SyncWithRechner;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  published
    procedure ControlChange( Sender: TObject );
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{ TForm1 }

procedure TForm1.AfterConstruction;
begin
  inherited;
  FRechner := TRechner.Create;
end;

procedure TForm1.BeforeDestruction;
begin
  inherited;
  FRechner.Free;
end;

procedure TForm1.ControlChange( Sender: TObject );
begin
  SyncWithRechner;
end;

procedure TForm1.FormShow( Sender: TObject );
begin
  LoadFromRechner;
end;

procedure TForm1.LoadFromRechner;
begin
  if FIsLoading
  then
    Exit;

  FIsLoading := True;
  try
    Edit1.Text := FRechner.Wert1;
    Edit2.Text := FRechner.Wert2;
    Edit3.Text := FRechner.Wert3;
    Edit4.Text := FRechner.Wert4;
    Label1.Caption := FRechner.Ergebnis1;
    Label2.Caption := FRechner.Ergebnis2;

    if FRechner.Wert3Berechnet
    then
      Edit3.Font.Color := clRed
    else
      Edit3.ParentFont := True;
  finally
    FIsLoading := False;
  end;
end;

procedure TForm1.SaveToRechner;
begin
  if FIsLoading
  then
    Exit;

  FRechner.Wert1 := Edit1.Text;
  FRechner.Wert2 := Edit2.Text;
  FRechner.Wert3 := Edit3.Text;
  FRechner.Wert4 := Edit4.Text;
end;

procedure TForm1.SyncWithRechner;
begin
  SaveToRechner;
  LoadFromRechner;
end;

end.
Angehängte Dateien
Dateityp: zip dp_182807.zip (838,2 KB, 6x aufgerufen)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat