Créer Questionnaire

Résolu
kopierreko Messages postés 139 Date d'inscription mercredi 12 mars 2008 Statut Membre Dernière intervention 10 juillet 2010 - 23 avril 2008 à 14:10
Sat83 Messages postés 166 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 13 octobre 2008 - 5 mai 2008 à 17:59
Bonjour tout le monde;
Je dois créer un questionnaire en deplhi, mais par étape: C-A-D
1ère question, je rempli le formulaire, et là, un message apparait me disant si j'ai bon ou si j'ai faux, et en même temps, là fenêtre se quitte et passe à la question d'après.
Comment procéder ?
Merci

23 réponses

kopierreko Messages postés 139 Date d'inscription mercredi 12 mars 2008 Statut Membre Dernière intervention 10 juillet 2010
4 mai 2008 à 20:54
ho dsl j'ai pas fait attention ^^
0
kopierreko Messages postés 139 Date d'inscription mercredi 12 mars 2008 Statut Membre Dernière intervention 10 juillet 2010
4 mai 2008 à 21:01
Ben j'ai réussi mais une chose curieuse, le si j'appuie sur entrée, ça ne marche pas sur le premier, mas tous les autres fonctionnent ><

Code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MPlayer, StdCtrls, jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    EdtReponse: TEdit;
    LblQuestion: TLabel;
    BtnValid: TButton;
    BtnQuit: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Image1: TImage;
    procedure BtnValidClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnQuitClick(Sender: TObject);
    procedure OnShow(Sender: TObject);
    procedure EdtReponseKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
      
  public
    { Public declarations }

  end;

  const

  myMp3Location : string ='templates\musique.mp3';

var
  media:TMediaPlayer;
  loop : Boolean {Global Variable};
  Form1: TForm1;
  listQuestion, listReponse : TStringList ;
       index : integer;
       cptBonneRep : integer ;
       fichier: textfile;
       Shift: TShiftState;
       Key: Word;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  listQuestion := TStringList.Create; //on creer la liste de questions
  listReponse := TStringList.Create;  // on creer la liste de reponses
  listQuestion.LoadFromFile('templates\chmnd.data');  //on charge depuis le fichier
  listReponse.LoadFromFile('templates\drxfm.data');    //on charge depuis le fichier
  index := 0;
  LblQuestion.caption := listQuestion.strings[index];
  cptBonneRep := 0 ;
end;

procedure TForm1.BtnValidClick(Sender: TObject);
var nbrQuestion   : integer ;
    messageTmp  : string ;
begin
    if ( uppercase( EdtReponse.Text ) = uppercase( listReponse.strings[index] )) then
    begin
       Application.MessageBox('FELICITATION, VOUS AVEZ LA BONNE REPONSE!','RESULTAT',MB_ICONINFORMATION + MB_OK) ;
       cptBonneRep := cptBonneRep + 1 ; // on compte les bonnes reponses
    end else
    begin
       messageTmp := 'DOMMAGE, VOUS N''AVEZ PAS LA BONNE REPONSE!'
                   + #10#13  //on saute une ligne
                   + 'La bonne reponse était : '
                   + listReponse.strings[index] ; //on construit le message

       Application.MessageBox(PChar(messageTmp),'RESULTAT SUR CETTE QUESTION',MB_ICONINFORMATION + MB_OK) ;

    end;

    index := index+1 ; //on passe a laquestion suivante

// recupere le nombre de question (<=> le nombre d'élement dans la liste)
    nbrQuestion := listQuestion.Count ;

    if index >= nbrQuestion then   // on verifie si il reste des questions
    begin
       messageTmp := 'Vous obtenez une note de : ' + IntToStr(cptBonneRep) + '/' + IntToStr(nbrQuestion) ;
       Application.MessageBox( PChar(messageTmp) ,'RESULTAT FINAL',MB_ICONINFORMATION + MB_OK) ;
       BtnValid.Enabled := false ; // on desactive le bouton car il n'y a plus de questions
    end else
    begin
       LblQuestion.caption := listQuestion.strings[index] ; //on l'affiche
       EdtReponse.Text := '' ; //on efface la reponse précedante
    end ;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  listQuestion.Free;  //on detruit les listes
  listReponse.Free;
end;

procedure TForm1.OnShow(Sender: TObject);
begin
  media := TMediaPlayer.Create(form1);
  media := TMediaPlayer.CreateParented(form1.Handle);
  media.FileName := myMp3Location;
  media.Open;
  media.Play;
end;

procedure TForm1.BtnQuitClick(Sender: TObject);
begin
media.Stop;
application.terminate;
end;

procedure TForm1.EdtReponseKeyPress(Sender: TObject; var Key: Char);
 var nbrQuestion   : integer ;
    messageTmp  : string ;
begin
if Key=#32 then //#32 = entrée - #27=echap etc (cherche par google)

 begin
    if ( uppercase( EdtReponse.Text ) = uppercase( listReponse.strings[index] )) then
    begin
       Application.MessageBox('FELICITATION, VOUS AVEZ LA BONNE REPONSE!','RESULTAT',MB_ICONINFORMATION + MB_OK) ;
       cptBonneRep := cptBonneRep + 1 ; // on compte les bonnes reponses
    end else
    begin
       messageTmp := 'DOMMAGE, VOUS N''AVEZ PAS LA BONNE REPONSE!'
                   + #10#13  //on saute une ligne
                   + 'La bonne reponse était : '
                   + listReponse.strings[index] ; //on construit le message

       Application.MessageBox(PChar(messageTmp),'RESULTAT SUR CETTE QUESTION',MB_ICONINFORMATION + MB_OK) ;

    end;

    index := index+1 ; //on passe a laquestion suivante

// recupere le nombre de question (<=> le nombre d'élement dans la liste)
    nbrQuestion := listQuestion.Count ;

    if index >= nbrQuestion then   // on verifie si il reste des questions
    begin
       messageTmp := 'Vous obtenez une note de : ' + IntToStr(cptBonneRep) + '/' + IntToStr(nbrQuestion) ;
       Application.MessageBox( PChar(messageTmp) ,'RESULTAT FINAL',MB_ICONINFORMATION + MB_OK) ;
       BtnValid.Enabled := false ; // on desactive le bouton car il n'y a plus de questions
    end else
    begin
       LblQuestion.caption := listQuestion.strings[index] ; //on l'affiche
       EdtReponse.Text := '' ; //on efface la reponse précedante
    end ;
end;

 end;

end.
0
Sat83 Messages postés 166 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 13 octobre 2008
5 mai 2008 à 17:59
Plusieurs remarques:
- tu peux creer des fonction/procedure pour les trucs qui se repetent.
-Pour tes variables globales
(TStringList...), tu peux les déclarer dans ta classe (je l'avais mis
dans mon exemple, je ne sais pas pourquoi tu les as enlevés?)
- petit conseil: essai
d'avoir un programme complet et qui marche avant d'ajouter des
fonctionnalités "superflu" (TMediaPlayer). C'est plus facile une fois
qu'on a une base qui marche bien d'ajouter des fonctionnalités pour
améliorer son programme et le finaliser, plutot que d'ajouté des trucs
pas necessaire dès le depart.

Voilà un exmeple qui marche avec la fonctionnalité lorsque tu appuis sur Enter :

----------------------------------------------------------------------------
unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    EdtReponse: TEdit;
    LblQuestion: TLabel;
    BtnValid: TButton;
    BtnQuit: TButton;
    procedure BtnValidClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnQuitClick(Sender: TObject);
    procedure EdtReponseKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
       listQuestion, listReponse : TStringList ;
       index : integer;
       cptBonneRep : integer ;

       procedure CheckQuestion;  // declaration de la procedure
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BtnValidClick(Sender: TObject);
begin
  CheckQuestion ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  listQuestion := TStringList.Create; //on creer la liste de questions
  listReponse := TStringList.Create;  // on creer la liste de reponses
  listQuestion.LoadFromFile('C:\question.txt');  //on charge depuis le fichier
  listReponse.LoadFromFile('C:\reponse.txt');    //on charge depuis le fichier
  index := 0;
  LblQuestion.caption := listQuestion.strings[index];
  cptBonneRep := 0 ;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  listQuestion.Free;  //on detruit les listes
  listReponse.Free;
end;

procedure TForm1.BtnQuitClick(Sender: TObject);
begin
  Close ; // Close c'est mieux que application.Terminate
end;

procedure TForm1.CheckQuestion;
var nbrQuestion   : integer ;
    messageTmp  : string ;
begin
    if ( uppercase( EdtReponse.Text ) = uppercase( listReponse.strings[index] )) then
    begin
       Application.MessageBox('FELICITATION, VOUS AVEZ LA BONNE REPONSE!','RESULTAT SUR CETTE QUESTION',MB_ICONINFORMATION + MB_OK) ;
       cptBonneRep := cptBonneRep + 1 ;
    end else
    begin
       messageTmp := 'DOMMAGE, VOUS N''AVEZ PAS LA BONNE REPONSE!'
                   + #10#13  //on saute une ligne
                   + 'La bonne reponse était : '
                   + listReponse.strings[index] ;

       Application.MessageBox(PChar(messageTmp),'RESULTAT SUR CETTE QUESTION',MB_ICONINFORMATION + MB_OK) ;

    end;

    index := index+1 ; //on passe a laquestion suivante

    nbrQuestion := listQuestion.Count ;

    if index >= nbrQuestion then   // on verifie qu'il reste des questions
    begin
       messageTmp := 'Vous obtenez une note de : ' + IntToStr(cptBonneRep) + '/' + IntToStr(nbrQuestion) ;
       Application.MessageBox( PChar(messageTmp) ,'RESULTAT FINAL',MB_ICONINFORMATION + MB_OK) ;
       BtnValid.Enabled := false ;
    end else
    begin
       LblQuestion.caption := listQuestion.strings[index] ; //on l'affiche
       EdtReponse.Text := '' ; //on efface la reponse précedante
    end ;

end;

procedure TForm1.EdtReponseKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = #13) and BtnValid.Enabled then
  begin
    CheckQuestion ;
  end ;
end;

end.
-----------------------------------------------------------------------------
0
Rejoignez-nous