unit Befund7;

// ---------------------------------------------------------------------------
//  MainMenu1 und PopupMenu1 sind im Objektinspektor unter den Eigenschaften
//  MainMenu und PopupMenu von Form1 angemeldet und so mit dem Formular
//  verknüpft. Das Popupmenü nutzt dieselben Methoden wie das Mainmenü.
// ---------------------------------------------------------------------------

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Menus, ComCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Panel1: TPanel;
    ScrollBar1: TScrollBar;
    RichEdit1: TRichEdit;
    MainMenu1: TMainMenu;
    Datei1: TMenuItem;
    Oeffnen1: TMenuItem;
    Speichern1: TMenuItem;
    Drucken1: TMenuItem;
    Ende1: TMenuItem;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    PrintDialog1: TPrintDialog;
    PopupMenu1: TPopupMenu;
    Oeffnen2: TMenuItem;
    Speichern2: TMenuItem;
    Ende2: TMenuItem;
    Drucken2: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ScrollBar1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Oeffnen1Click(Sender: TObject);
    procedure Speichern1Click(Sender: TObject);
    procedure Drucken1Click(Sender: TObject);
    procedure Ende1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

const
  DateiPfad = 'c:\delphi\buch\';

var
  DateiName : String = DateiPfad + 'Diagnose.txt';
  SammelName: String = DateiPfad + 'PsychoX.txt';

var
  Form1: TForm1;
  Diagnose, Psycho: TStringList;
  Nr: Integer;
  Sicher: Boolean;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  Diagnose := TStringList.Create;
  Psycho   := TstringList.Create;
  Diagnose.Add ('Keine Sprechstunde');
  ScrollBar1.Min := 0;
  ScrollBar1.Max := Diagnose.Count - 1;
  Sicher := false;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Panel1.Caption := '';
  Edit1.Text := '';
  Edit1.SetFocus;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Nr := random (Diagnose.Count);
  Panel1.Caption := Diagnose.Strings[Nr];
  Psycho.Add (Edit1.Text);
  Psycho.Add (Panel1.Caption);
  Sicher := false;
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  Panel1.Caption := Diagnose.Strings[ScrollBar1.Position];
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var Knopf: Integer;
begin
  if not Sicher then
  begin
    Knopf := Application.MessageBox
      ('Alle Eingaben (nochmal) speichern?', '', 4+32);
    if Knopf = IDYES then Speichern1Click (Sender);
  end;
end;

procedure TForm1.Oeffnen1Click(Sender: TObject);
begin
  try
    if OpenDialog1.Execute then
      Diagnose.LoadFromFile (OpenDialog1.FileName);
    ScrollBar1.Max := Diagnose.Count - 1;
  except
    ShowMessage ('Fehler beim Öffnen der Datei!');
  end;
end;

procedure TForm1.Speichern1Click(Sender: TObject);
begin
  try
    if SaveDialog1.Execute then
    begin
      Psycho.SaveToFile (SaveDialog1.FileName);
      Sicher := true;
    end;
  except
    ShowMessage ('Fehler beim Speichern der Datei!');
    Sicher := false;
  end;
end;

procedure TForm1.Drucken1Click(Sender: TObject);
begin
  try
    if PrintDialog1.Execute then
    begin
      RichEdit1.Text := Diagnose.Text;
      RichEdit1.Print ('Diagnose');
    end;
  except
    ShowMessage ('Fehler beim Drucken der Datei!');
  end;
end;

procedure TForm1.Ende1Click(Sender: TObject);
var Knopf: Integer;
begin
  if not Sicher then
  begin
    Knopf := Application.MessageBox
      ('Alle Eingaben (nochmal) speichern?', '', 3+32);
    if Knopf = IDYES then Speichern1Click (Sender);
  end;
  if Knopf <> IDCANCEL then Close;
end;

end.
