Einzelnen Beitrag anzeigen

KetchUp

Registriert seit: 16. Nov 2018
31 Beiträge
 
#47

AW: Elemente aus TStringList in Laufzeit entfernen

  Alt 18. Nov 2018, 13:06
Eventuell fehlt ihm etwas Mayo <> Ketchup
Der Gedanke hinter dem Nickname war eigentlich "catch up"

OK, dann poste mal den ganzen Quellcode. Der enthält jetzt aber auch ein Haufen Grafikzeug, da ich versuche das visuell ansprechend zu gestalten ohne wirklich Ahnung von Delphi zu haben.

Also das ganz ist in 3 Forms aufgeteilt. Ein Hauptmenü, einmal der Vokabeltrainer und eine Zusammenfassung am Schluss.

Im Hauptmenü kann man auswählen ob man Deutsch>Englisch oder Englisch>Deutsch lernen will und wie oft eine Vokabel wiederholt werden soll. Dann startet man mit einem Button den Vokabeltrainer (zweites Fenster öffnet sich, Erstes wird geschlossen).

Hier werden die Vokabeln abgefragt. Wenn man alle entsprechend der Wiederholungsanzahl richtig eingegeben hat öffnet sich das dritte Fenster (zweites schließt sich).

Hier wird eine Zusammenfassung angezeigt. Wie lange man gebraucht hat, wie viele versuche man gebraucht hat...
Dann soll man mit einem Button das Hauptmenü wieder öffen können um von vorn anzufangen.

Hier der Quellcode für das Hauptmenü
Delphi-Quellcode:
unit U_MainMenu;

interface

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

type
  TForm1 = class(TForm)
    Bt_Start: TButton;
    Bt_SwitchLanguage: TButton;
    Img_Background_01: TImage;
    Img_Background_02: TImage;
    L_HeadingMainMenu: TLabel;
    E_Repititions: TEdit;
    Img_BackgroundSwitchLanguage: TImage;
    L_SwitchLanguage: TLabel;
    E_Amount: TEdit;
    Img_HeadingAmountBackground: TImage;
    L_HeadingAmount: TLabel;
    Img_HeadingRepititionsBackground: TImage;
    L_HeadingRepititions: TLabel;
    procedure Bt_StartClick(Sender: TObject);
    procedure Bt_SwitchLanguageClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    //procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  var GermanEnglish: Boolean;
  var Repititions: Byte;

implementation
uses
  U_VocabularyTraining;

{$R *.dfm}

procedure TForm1.Bt_StartClick(Sender: TObject);
var
  Form2: TForm2;

begin
  Repititions:= StrToInt(E_Repititions.Text);
  Form2 := TForm2.Create(Self);
  //Application.ShowMainForm := False;
  //Form1.Visible := False;
  try
    Form2.ShowModal;
  finally
    Form2.Free;
  end;
end;


procedure TForm1.Bt_SwitchLanguageClick(Sender: TObject);

Begin

  If GermanEnglish = True
  Then
    Begin
      L_SwitchLanguage.Caption:='Englisch -> Deutsch';
      GermanEnglish:= False;
    End

  Else
    Begin
      L_SwitchLanguage.Caption:='Deutsch -> Englisch';
      GermanEnglish:= True;

    End;

End;


procedure TForm1.FormCreate(Sender: TObject);
var
  rectangle1: TRect;
  color: Tcolor;
  CenterTextHeadingMainMenu_1, CenterTextHeadingMainMenu_2,
  CenterTextSwitchLanguage_1, CenterTextSwitchLanguage_2,
  CenterTextHeadingAmount_1, CenterTextHeadingAmount_2,
  CenterTextHeadingRepititions_1, CenterTextHeadingRepititions_2: Integer;

begin
  GermanEnglish:= True;
  Img_Background_01.Canvas.Brush.Color:= RGB(170,220,70);
  Img_Background_01.Canvas.Rectangle(-1,-1,Img_Background_01.Width+1,Img_Background_01.Height+1);

  Img_Background_02.Canvas.Brush.Color:= RGB(170,220,70);
  Img_Background_02.Canvas.Rectangle(-1,-1,Img_Background_02.Width+1,Img_Background_02.Height+1);

  Img_BackgroundSwitchLanguage.Canvas.Brush.Color:= RGB(170,220,70);
  Img_BackgroundSwitchLanguage.Canvas.Rectangle(-1,-1,Img_Background_02.Width+1,Img_Background_02.Height+1);

  Img_HeadingAmountBackground.Canvas.Brush.Color:= RGB(170,220,70);
  Img_HeadingAmountBackground.Canvas.Rectangle(-1,-1,Img_Background_02.Width+1,Img_Background_02.Height+1);

  Img_HeadingRepititionsBackground.Canvas.Brush.Color:= RGB(170,220,70);
  Img_HeadingRepititionsBackground.Canvas.Rectangle(-1,-1,Img_Background_02.Width+1,Img_Background_02.Height+1);

  L_HeadingRepititions.Font.Name:='Helvetica LT Std';
  L_HeadingRepititions.Font.Size:= 16;
  L_HeadingRepititions.Font.Color:= clWhite;
  CenterTextHeadingRepititions_1:= Form1.Width div 3;
  CenterTextHeadingRepititions_2:= L_HeadingRepititions.Width div 1;
  L_HeadingRepititions.Left:= CenterTextHeadingRepititions_1 + CenterTextHeadingRepititions_2;

  L_HeadingAmount.Font.Name:='Helvetica LT Std';
  L_HeadingAmount.Font.Size:= 16;
  L_HeadingAmount.Font.Color:= clWhite;
  CenterTextHeadingAmount_1:= Form1.Width div 4;
  CenterTextHeadingAmount_2:= L_HeadingAmount.Width div 2;
  L_HeadingAmount.Left:= CenterTextHeadingAmount_1 - CenterTextHeadingAmount_2;

  E_Amount.Font.Name:='Helvetica LT Std';
  E_Amount.Font.Size:= 16;
  E_Amount.Font.Color:= RGB(170,220,70);
  CenterTextHeadingAmount_1:= Form1.Width div 4;
  CenterTextHeadingAmount_2:= E_Amount.Width div 2;
  E_Amount.Left:= CenterTextHeadingAmount_1 - CenterTextHeadingAmount_2;

  L_HeadingMainMenu.Font.Name:='Helvetica LT Std';
  L_HeadingMainMenu.Font.Size:= 24;
  L_HeadingMainMenu.Font.Color:= clWhite;
  CenterTextHeadingMainMenu_1:= Form1.Width div 2;
  CenterTextHeadingMainMenu_2:= L_HeadingMainMenu.Width div 2;
  L_HeadingMainMenu.Left:= CenterTextHeadingMainMenu_1 - CenterTextHeadingMainMenu_2;

  L_SwitchLanguage.Font.Name:='Helvetica LT Std';
  L_SwitchLanguage.Font.Size:= 16;
  L_SwitchLanguage.Font.Color:= clWhite;
  CenterTextSwitchLanguage_1:= Form1.Width div 2;
  CenterTextSwitchLanguage_2:= L_SwitchLanguage.Width div 2;
  L_SwitchLanguage.Left:= CenterTextSwitchLanguage_1 - CenterTextSwitchLanguage_2;

  E_Repititions.Text:= '3';

end;

end.
Ja, das kann man sicherlich optimaler gestalten aber das ist in der Aufgabe nicht so wichtig (ist ja auch nur ein Schulfach, kein Informatik Studium). Ich muss aber den ganzen Quellcode in einem Vortrag erklären können.

Und hier der Quellcode zum Vokabeltrainer:

Delphi-Quellcode:
unit U_VocabularyTraining;

interface

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

type
  TForm2 = class(TForm)
    Bt_Compare: TButton;
    E_UserGuess: TEdit;
    Img_FeedBack: TImage;
    Img_Background_04: TImage;
    Img_Background_03: TImage;
    L_HeadingTrainer: TLabel;
    L_RemainingWords: TLabel;
    Img_BackgroundUnknownWord: TImage;
    L_UnknownWord: TLabel;
    L_CorrectAnswer: TLabel;
    Img_BackgroundCorrectAnswer: TImage;
    Img_FlagQuestion: TImage;
    Img_FlagAnswer: TImage;
    L_HeadingTrainerDescription: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Bt_CompareClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  var
    Vocabulary: TStringList;
    RandomNumber, Abfragen: Integer;
    jpgFlag, jpgCorrect, jpgWrong: TJpegImage;
    StartTime: TDateTime;
    Duration: TDateTime;
  end;

var
  Form2: TForm2;

implementation
uses
  U_MainMenu, U_Conclusion;

{$R *.dfm}

procedure TForm2.Bt_CompareClick(Sender: TObject);
var
  Input: String;
  Answer, RepetitionCheck: Integer;
  Form3: TForm3;

Begin
  If Vocabulary.Count > 0 Then

    Begin
      If GermanEnglish = True

      Then
        Begin
          Input:= E_UserGuess.Text;
          E_UserGuess.Text:= '';

          Answer:=AnsiCompareStr(Vocabulary.Names[RandomNumber], Input);

          If Answer=0
          Then
            Begin
              L_CorrectAnswer.Caption:='';
              Img_FeedBack.Picture.Bitmap.Assign(jpgCorrect);
              Vocabulary.Delete(RandomNumber);
            End

          Else
            Begin
              L_CorrectAnswer.Caption:='Deine Antwort: '+(Input)+' '+'Richtige Antwort: '+(Vocabulary.Names[RandomNumber]);
              Img_FeedBack.Picture.Bitmap.Assign(jpgWrong);

            End;

            RepetitionCheck:=RandomNumber;
            While RepetitionCheck = RandomNumber Do RandomNumber:=random(Vocabulary.Count);

            try
              L_UnknownWord.Caption:= Vocabulary.ValueFromIndex[RandomNumber];
              L_RemainingWords.Caption:= 'Verbleibende Vokabeln: '+IntToStr(Vocabulary.Count);
              L_RemainingWords.Font.Name:='Helvetica LT Std';
              L_RemainingWords.Font.Size:= 16;
              L_RemainingWords.Font.Color:= RGB(170,220,70);
              finally
            end;
        end

      Else
        Begin
          Input:= E_UserGuess.Text;
          E_UserGuess.Text:= '';

          Answer:=AnsiCompareStr(Vocabulary.ValueFromIndex[RandomNumber], Input);

          If Answer=0
          Then
            Begin
              L_CorrectAnswer.Caption:='';
              Img_FeedBack.Picture.Bitmap.Assign(jpgCorrect);
              jpgCorrect.free;
              Vocabulary.Delete(RandomNumber);
            End

          Else
            Begin
              L_CorrectAnswer.Caption:='Deine Antwort: '+(Input)+' '+'Richtige Antwort: '+(Vocabulary.ValueFromIndex[RandomNumber]);
              Img_FeedBack.Picture.Bitmap.Assign(jpgWrong);
              jpgWrong.free;
            End;

            RepetitionCheck:=RandomNumber;
            While RepetitionCheck = RandomNumber Do RandomNumber:=random(Vocabulary.Count);

            try
              L_UnknownWord.Caption:= Vocabulary.Names[RandomNumber];
              L_RemainingWords.Caption:= 'Verbleibende Vokabeln: '+IntToStr(Vocabulary.Count);
              L_RemainingWords.Font.Name:='Helvetica LT Std';
              L_RemainingWords.Font.Size:= 16;
              L_RemainingWords.Font.Color:= RGB(170,220,70);
              finally
            end;
        End;
    End

  Else
    Begin
      FormDestroy(Vocabulary);
    End;
End;


procedure TForm2.FormCreate(Sender: TObject);
var
  CenterTextHeadingTrainer_1, CenterTextHeadingTrainer_2,
  CenterTextHeadingTrainerDescription_1, CenterTextHeadingTrainerDescription_2,
  CenterTextRemainingWords_1, CenterTextRemainingWords_2: Integer;

begin
   //Color := RGB(170,220,70); //Hintergrund grün
   //P_Question.Color := RGB(255,255,255); //Fragefeld Hintergrund weiß
   //P_Answer.Color := RGB(255,255,255); //Antwortfeld Hintergrund weiß

  Img_Background_03.Canvas.Brush.Color:= RGB(170,220,70);
  Img_Background_03.Canvas.Rectangle(-1,-1,Img_Background_03.Width+1,Img_Background_03.Height+1);

  Img_Background_04.Canvas.Brush.Color:= RGB(170,220,70);
  Img_Background_04.Canvas.Rectangle(-1,-1,Img_Background_04.Width+1,Img_Background_04.Height+1);

  Img_BackgroundUnknownWord.Canvas.Brush.Color:= RGB(170,220,70);
  Img_BackgroundUnknownWord.Canvas.Rectangle(-1,-1,Img_BackgroundUnknownWord.Width+1,Img_BackgroundUnknownWord.Height+1);

  Img_BackgroundCorrectAnswer.Canvas.Brush.Color:= RGB(220,220,220);
  Img_BackgroundCorrectAnswer.Canvas.Rectangle(-1,-1,Img_BackgroundCorrectAnswer.Width+1,Img_BackgroundCorrectAnswer.Height+1);

  L_HeadingTrainer.Font.Name:='Helvetica LT Std';
  L_HeadingTrainer.Font.Size:= 24;
  L_HeadingTrainer.Font.Color:= clWhite;
  CenterTextHeadingTrainer_1:= Form1.Width div 2;
  CenterTextHeadingTrainer_2:= L_HeadingTrainer.Width div 2;
  L_HeadingTrainer.Left:= CenterTextHeadingTrainer_1 - CenterTextHeadingTrainer_2;

  L_HeadingTrainerDescription.Font.Name:='Helvetica LT Std';
  L_HeadingTrainerDescription.Font.Size:= 16;
  L_HeadingTrainerDescription.Font.Color:= clWhite;
  CenterTextHeadingTrainerDescription_1:= Form1.Width div 5;
  CenterTextHeadingTrainerDescription_2:= L_HeadingTrainerDescription.Width div 2;
  L_HeadingTrainerDescription.Left:= CenterTextHeadingTrainerDescription_1 - CenterTextHeadingTrainerDescription_2;

  L_RemainingWords.Caption:= 'Verbleibende Vokabeln: 3';
  L_RemainingWords.Font.Name:='Helvetica LT Std';
  L_RemainingWords.Font.Size:= 16;
  L_RemainingWords.Font.Color:= RGB(170,220,70);
  CenterTextRemainingWords_1:= Form1.Width div 2;
  CenterTextRemainingWords_2:= L_RemainingWords.Width div 2;
  L_RemainingWords.Left:= CenterTextRemainingWords_1 - CenterTextRemainingWords_2;

  jpgCorrect:= TJpegImage.Create;
  jpgCorrect.LoadFromFile('True.jpg');

  jpgWrong:= TJpegImage.Create;
  jpgWrong.LoadFromFile('False.jpg');

  StartTime := Now;

  Vocabulary:= TStringList.Create;
  Vocabulary.LoadFromFile('Vocabulary.txt');

  RandomNumber:= random(Vocabulary.Count);
  L_RemainingWords.Caption:= 'Verbleibende Vokabeln: '+IntToStr(Vocabulary.Count);

  If GermanEnglish = True

  Then
    try
      L_HeadingTrainerDescription.Caption:= 'Deutsch -> Englisch | '+IntToStr(Repititions)+' Abfragen | 10 Vokabeln';

      jpgFlag := TJpegImage.Create;
      jpgFlag.LoadFromFile('GermanFlag.jpg');
      Img_FlagQuestion.Picture.Bitmap.Assign(jpgFlag);
      jpgFlag.free;

      jpgFlag := TJpegImage.Create;
      jpgFlag.LoadFromFile('BritishFlag.jpg');
      Img_FlagAnswer.Picture.Bitmap.Assign(jpgFlag);
      jpgFlag.free;

      L_UnknownWord.Caption:= Vocabulary.ValueFromIndex[RandomNumber];
      L_CorrectAnswer.Caption:= '';
      finally
    end

  Else
    try
      L_HeadingTrainerDescription.Caption:='Englisch -> Deutsch | '+IntToStr(Repititions)+' Abfragen | 10 Vokabeln';

      jpgFlag := TJpegImage.Create;
      jpgFlag.LoadFromFile('BritishFlag.jpg');
      Img_FlagQuestion.Picture.Bitmap.Assign(jpgFlag);
      jpgFlag.free;

      jpgFlag := TJpegImage.Create;
      jpgFlag.LoadFromFile('GermanFlag.jpg');
      Img_FlagAnswer.Picture.Bitmap.Assign(jpgFlag);
      jpgFlag.free;

      L_UnknownWord.Caption:= Vocabulary.Names[RandomNumber];
      L_CorrectAnswer.Caption:= '';
      finally
    End;

end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  Vocabulary.Free;
  jpgWrong.free;
  jpgCorrect.free;

  Form3 := TForm3.Create(self);
      try
        Form3.ShowModal;
      finally
        Form3.Free;
      end;
end;

end.
Das letzte Fenster hat noch nicht viel Code. In Delphi ist der Code deutlich übersichtlicher, da um Begin und End noch eine Farbige Klammer ist.

Geändert von KetchUp (18. Nov 2018 um 13:17 Uhr)
  Mit Zitat antworten Zitat